projet_voilier/Services/DCMotor.c
2020-11-16 18:42:10 +01:00

36 lines
1,008 B
C

#include "DCMotor.h"
#include "math.h"
void DCMotor_conf(TIM_TypeDef * timer, int channel, GPIO_TypeDef * gpio, int pin)
{
//On règle la vitesse en valeur absolue, ici à 0
Timer_pwmo_conf(timer, channel, 50, 0);
// Configuration du GPIO
GPIO_conf(GPIOA, LL_GPIO_PIN_1, LL_GPIO_MODE_ALTERNATE, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_UP);
//On règle le sens du moteur, ici sens direct (?)
GPIO_conf(gpio, pin, LL_GPIO_MODE_OUTPUT, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_DOWN);
GPIO_setPin(gpio, pin, 0);
Timer_start(timer);
}
void DCMotor_setSpeed(TIM_TypeDef * timer, int channel, GPIO_TypeDef * gpio, int pin, float speed)
{
const int dir = (speed > 0.) ? 1 : 0;
Timer_pwmo_setDutyCycle(timer, channel, fabs(speed));
GPIO_setPin(gpio, pin, dir);
}
float DCMotor_getSpeed(TIM_TypeDef * timer, int channel, GPIO_TypeDef * gpio, int pin)
{
const float speedAbs = Timer_pwmo_getDutyCycle(timer, channel);
const int dir = GPIO_readPin(gpio, pin);
return dir ? speedAbs : -speedAbs;
}