Projet_voilier2/Projet_voile/Sources/Pwm_voile.c
2023-04-11 15:32:11 +02:00

44 lines
889 B
C

# include "Pwm_voile.h"
MyTimer_Struct_TypeDef Timer2, Timer3;
MyGPIO_Struct_TypeDef PB0;
void write_pwm(void)
{
int val_pwm;
float cnt;
cnt=Read_CNT();
if(cnt>=360)
{
cnt=cnt-360;
val_pwm=(Timer3.ARR/20)+((Timer3.ARR/20)*(cnt/360.0));
}
else
{
val_pwm=2*(Timer3.ARR/20)-((Timer3.ARR/20)*(cnt/360.0));
}
//Timer3.ARR/20 --> 1ms, pour controler le moteur c'est entre 1ms et 2ms
//calcul angle en pwm
MyPWM_Duty(Timer3.Timer,3,val_pwm);
}
void init_pwm_voile(void)
{
Timer2.Timer=TIM2;
Timer2.ARR=1800;
Timer2.PSC=1000;
MyTimer_Base_Init(&Timer2);
MyTimer_ActiveIT(Timer2.Timer,2,&write_pwm);
MyTimer_Base_Start(Timer2.Timer);
PB0.GPIO=GPIOB;
PB0.GPIO_Conf=AltOut_Ppull;
PB0.GPIO_Pin=0;
MyGPIO_Init(&PB0);
Timer3.Timer=TIM3;
Timer3.ARR=14400;
Timer3.PSC=100;
MyTimer_Base_Init(&Timer3);
MyPWM_init(Timer3.Timer,3);
MyTimer_Base_Start(Timer3.Timer);
}