Periph-Voilier/Services/Moteur.c

72 lines
No EOL
1.5 KiB
C

#include "Moteur.h"
#include "MyPWM.h"
#include "MyTimer.h"
#include "stm32f1xx_ll_bus.h"
#include "stm32f1xx_ll_gpio.h"
//Fpwm = 10kHz
static int Arr = 0x1C1F;
static int Psc = 0x0;
void Moteur_Conf() {
//Activation horloge GPIO
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
//Config broche PA2 -> Sens
LL_GPIO_InitTypeDef* My_GPIO_Init_Struct;
LL_GPIO_StructInit(My_GPIO_Init_Struct);
My_GPIO_Init_Struct->Pin = PinSens;
My_GPIO_Init_Struct->Mode = LL_GPIO_MODE_OUTPUT;
My_GPIO_Init_Struct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
LL_GPIO_Init(GPIOPins, My_GPIO_Init_Struct);
//Config broche PA1 -> PWM
LL_GPIO_StructInit(My_GPIO_Init_Struct);
My_GPIO_Init_Struct->Pin = PinPWM;
My_GPIO_Init_Struct->Mode = LL_GPIO_MODE_ALTERNATE;
My_GPIO_Init_Struct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
LL_GPIO_Init(GPIOPins, My_GPIO_Init_Struct);
//Activation horloge Timer
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
//Configuration initiale du Timer
MyTimer_Conf(TimerPWM, Arr, Psc);
//Configuration du Timer en PWM Output
MyPWM_Conf_Output(TimerPWM, channelPWM);
MyPWM_Set_Impulse_Duration(TimerPWM, 0, channelPWM);
}
void Moteur_Speed(int speedPercentage) {
if(speedPercentage == 0) {
MyTimer_Stop(TimerPWM);
}
else {
MyTimer_Start(TimerPWM);
MyPWM_Set_Impulse_Duration(TimerPWM, Arr*speedPercentage/100, channelPWM);
}
}
void Moteur_Sens(int sens) {
if(sens == 0) {
LL_GPIO_ResetOutputPin(GPIOPins, PinSens);
}
else {
LL_GPIO_SetOutputPin(GPIOPins, PinSens);
}
}