Periph-Voilier/Services/girouette.c
2020-11-24 19:52:32 +01:00

113 lines
2.3 KiB
C

#include "stm32f1xx_ll_gpio.h"
#include "girouette.h"
#include "MyTimer.h"
#include "stm32f1xx_ll_bus.h"
#include "stm32f1xx_ll_tim.h"
#include "stm32f1xx_ll_adc.h"
#include "stm32f1xx_ll_rcc.h"
//Configuration de l'interruption exterieure
void exti_ref(void){
LL_GPIO_AF_SetEXTISource(LL_GPIO_AF_EXTI_PORTA, LL_GPIO_AF_EXTI_LINE5);
AFIO->EXTICR[2] |= AFIO_EXTICR2_EXTI5_PA;
EXTI->IMR |= EXTI_IMR_MR5_Msk;
EXTI->RTSR |= EXTI_RTSR_TR5_Msk;
}
//Configuration de l'interruption du NVIC
void nvic_exti_conf(void){
NVIC->IP[23]=2<<4;
NVIC->ISER[0]=1<<23;
}
//redirection de l'interruption
void EXTI9_5_IRQHandler(void){
//CNT A ZERO
LL_TIM_SetCounter(TIM3, 0);
//enlever le pin de pending en le mettant a 1
EXTI->PR |= EXTI_PR_PR5_Msk;
}
//configuration interruption + timer 3 en mode encodeur et 4 en mode PWM
void conf_girouette(void){
//interruption
exti_ref();
nvic_exti_conf();
//Timer en mode encodeur
MyTimer_girouette_Conf();
//Timer en mode PWM
MyPWM_Conf_Output(TIM1,LL_TIM_CHANNEL_CH1);
//Activation Timer 1
LL_TIM_EnableAllOutputs(TIM1);
}
//Calcul position-angle
float pos_Min=0;
float pos_Max=135;
float pos_actuelle;
float duree_Min=1;
float duree_Max=2;
float duree_calc;
float Periode=20;
float Pourcentage_Min;
float Pourcentage_Max;
float Pourcentage_calc;
float Calcul_pourcentage_duree (void){
pos_actuelle=LL_TIM_GetCounter(TIM3);
//Calcul pourcentage min et max
Pourcentage_Min=duree_Min/Periode;
Pourcentage_Max=duree_Max/Periode;
//Calcul fctn affine
if (pos_actuelle <= 360){
if (pos_actuelle > 90 && pos_actuelle < 270) {
duree_calc=((duree_Max-duree_Min)/(pos_Max-pos_Min))*pos_actuelle+duree_Min;
}
else if (pos_actuelle > 270){
duree_calc=duree_Max;
}
else {
duree_calc=duree_Min;
}
}
/*if (pos_actuelle > 360 && pos_actuelle < 720){
if (pos_actuelle > 450 && pos_actuelle < 630) {
duree_calc=((duree_Max-duree_Min)/(pos_Max-pos_Min))*(720-pos_actuelle)+duree_Min;
}
else if (pos_actuelle < 450){
duree_calc=duree_Max;
}
else {
duree_calc=duree_Min;
}
}*/
//Calcul pourcentage
Pourcentage_calc=((Pourcentage_Max-Pourcentage_Min)/(duree_Max-duree_Min))*duree_calc;
return Pourcentage_calc;
}
float Angle_Vent(void) {
return pos_actuelle/2.0;
}