Merge pull request #4 from MDHSweden/pwmDriver

Drivers approved by Lennart
This commit is contained in:
Lennart Eriksson 2016-09-19 09:08:55 +02:00 committed by GitHub
commit c0e4f39d86
2 changed files with 14 additions and 6 deletions

View File

@ -13,7 +13,7 @@
#include "drivers/pwm.h" #include "drivers/pwm.h"
#include "drivers/motors.h" #include "drivers/motors.h"
#define MOTOR_PWM_INIT_PERIODE 65535 #define MOTOR_PWM_INIT_PERIODE 2000
#define MOTOR_PWM_INIT_PULSE MOTOR_PWM_INIT_PERIODE/2 #define MOTOR_PWM_INIT_PULSE MOTOR_PWM_INIT_PERIODE/2
/* A struct of a pwm motor profile */ /* A struct of a pwm motor profile */
@ -25,6 +25,16 @@ typedef struct
uint32_t channel; //TIM_CHANNEL_1/TIM_CHANNEL_1/.. uint32_t channel; //TIM_CHANNEL_1/TIM_CHANNEL_1/..
}motorProfile; }motorProfile;
/**************************************************************************
* BRIEF: Returns the final pulse of a motor driver call
* INFORMATION: The pulse is not allowed to be higher then 94 % of the total periode of the pwm signal, otherwise the pwm won't perform correctly
* Example - pwmEnableMotor(MOTOR_1)
**************************************************************************/
uint16_t checkPulse(uint16_t pulse)
{
return ((pulse/MOTOR_PWM_INIT_PERIODE)*100 < 94)? pulse: MOTOR_PWM_INIT_PERIODE*0.94;
}
/************************************************************************** /**************************************************************************
* BRIEF: Returns a profile of a certain motor * BRIEF: Returns a profile of a certain motor
* INFORMATION: Each Motor has a certain profile which includes a pin, port, timer and a timer channel * INFORMATION: Each Motor has a certain profile which includes a pin, port, timer and a timer channel
@ -152,7 +162,6 @@ void pwmDeactivateMotor(uint8_t motor)
/************************************************************************** /**************************************************************************
* BRIEF: Activates all motors (Activates a pwm signal to all motor pins to it's last state)* * * BRIEF: Activates all motors (Activates a pwm signal to all motor pins to it's last state)* *
* INFORMATION: * * INFORMATION: *
* Example - pwmActivateAllMotors() *
**************************************************************************/ **************************************************************************/
void pwmActivateAllMotors(void) void pwmActivateAllMotors(void)
{ {
@ -178,7 +187,7 @@ void pwmAdjustSpeedOfMotor(uint8_t motor, uint16_t pulse)
{ {
motorProfile profile = getMotorProfile(motor); motorProfile profile = getMotorProfile(motor);
setPwmPulse(profile.channel, profile.tim, pulse); setPwmPulse(profile.channel, profile.tim, checkPulse(pulse));
} }
/************************************************************************** /**************************************************************************
@ -191,5 +200,5 @@ void pwmAdjustSpeedOfMotorDutyCycle(uint8_t motor, uint16_t DutyCycle)
uint16_t pulse = (DutyCycle*MOTOR_PWM_INIT_PERIODE)/100; /* Converts the DutyCycle to a pulse */ uint16_t pulse = (DutyCycle*MOTOR_PWM_INIT_PERIODE)/100; /* Converts the DutyCycle to a pulse */
motorProfile profile = getMotorProfile(motor); motorProfile profile = getMotorProfile(motor);
setPwmPulse(profile.channel, profile.tim, pulse); setPwmPulse(profile.channel, profile.tim, checkPulse(pulse));
} }

View File

@ -81,10 +81,9 @@ void pwmInit(GPIO_TypeDef * GPIO, uint16_t pin, TIM_TypeDef * tim, uint32_t Chan
TIM_HandleTypeDef TimHandle; TIM_HandleTypeDef TimHandle;
uint32_t uwPrescalerValue =2;
TimHandle.Instance = profile.tim; /* Sets timer */ TimHandle.Instance = profile.tim; /* Sets timer */
TimHandle.Init.Period = period; /* Sets period of pwm */ TimHandle.Init.Period = period; /* Sets period of pwm */
TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.Prescaler = 88; /* pwm prescaler of clk */
TimHandle.Init.ClockDivision = 0; TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;