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

101 lines
1.8 KiB
C

#include "ADC_antichavirement.h"
#include "Chrono.h"
#include "MyTimer.h"
#include "stm32f1xx_ll_bus.h"
#include "stm32f1xx_ll_tim.h"
#include "stm32f1xx_ll_adc.h"
#include "stm32f1xx_ll_rcc.h"
#define seuil_g_pos 1.0
#define seuil_g_neg -1.0
void conf_ADC (void) {
//ADC_REG
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1);
//Division pour etre < 14 MHz
LL_RCC_SetADCClockSource(LL_RCC_ADC_CLKSRC_PCLK2_DIV_6);
//Bit ADON à 1
LL_ADC_Enable(ADC1);
//Définition
LL_ADC_REG_InitTypeDef My_LL_ADC_REG_Init_Struct;
//Initialisation
LL_ADC_REG_StructInit(&My_LL_ADC_REG_Init_Struct);
My_LL_ADC_REG_Init_Struct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_1RANK ;
LL_ADC_StartCalibration(ADC1);
//Application a l'ADC1
LL_ADC_REG_Init(ADC1,&My_LL_ADC_REG_Init_Struct);
//ADC Sampling Time
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_10, LL_ADC_SAMPLINGTIME_239CYCLES_5);
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_11, LL_ADC_SAMPLINGTIME_239CYCLES_5);
}
uint32_t X;
uint32_t Y;
float rapport;
float start_ADC (void) {
//Conversion X
LL_ADC_REG_SetSequencerRanks(ADC1,LL_ADC_REG_RANK_1,LL_ADC_CHANNEL_10);
LL_ADC_Enable(ADC1);
LL_ADC_REG_StartConversionSWStart(ADC1);
while ( LL_ADC_IsActiveFlag_EOS(ADC1) == 0);
X=LL_ADC_REG_ReadConversionData32(ADC1);
//Conversion Y
LL_ADC_REG_SetSequencerRanks(ADC1,LL_ADC_REG_RANK_1,LL_ADC_CHANNEL_11);
LL_ADC_Enable(ADC1);
LL_ADC_REG_StartConversionSWStart(ADC1);
while ( LL_ADC_IsActiveFlag_EOS(ADC1) == 0);
Y=LL_ADC_REG_ReadConversionData32(ADC1);
//Calcul rapport
rapport = X/Y;
return rapport;
}
void antichavirement (float rapport) {
if (rapport >= seuil_g_pos || rapport <= seuil_g_neg){
//Voiles à 90°
MyPWM_Set_Impulse_Duration(TIM1,10000*10/100,LL_TIM_CHANNEL_CH1);
}
}