voilier-team-1/driver/adc.c

50 lines
1.9 KiB
C
Raw Normal View History

#include "adc.h"
2023-03-31 09:37:30 +02:00
void (* pFncADC) (uint32_t); /* d<>claration d<>un pointeur de fonction */
2023-03-31 09:37:30 +02:00
void MyADC_Init_Periph (void (* ptrFonction)(uint32_t))
2023-03-31 09:37:30 +02:00
{
pFncADC = ptrFonction; /* affectation du pointeur */
}
void MyADC_Init(MyADC_Struct_TypeDef * ADCStructPtr)
{
RCC->CFGR |= RCC_CFGR_ADCPRE_1; // ADC Prescaler : divided by 6 -> 72MHz to 12MHz
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; //We activate the clock first
if(ADCStructPtr->channel < 10) //Cycle and channel selection -> permet de regler en fonction de la r<>sistance donn<6E>e
2023-03-31 09:37:30 +02:00
{
ADCStructPtr->ADC->SMPR2 |= (ADCStructPtr->cycle<<(ADCStructPtr->channel*3)); //Channel < 10
}
else {
ADCStructPtr->ADC->SMPR1 |= (ADCStructPtr->cycle<<((ADCStructPtr->channel-10)*3)); //Channel > 10
}
ADCStructPtr->ADC->SQR1 &= ADC_SQR1_L; //Channel sequence length -> 1 conversion (0000)
ADCStructPtr->ADC->SQR3 |= ADCStructPtr->channel; //Sequence Reader, seulement le SQ1 est lu dans notre cas, nous associons un channel <20> ce dernier.
2023-03-31 09:37:30 +02:00
ADCStructPtr->ADC->CR2 |= ADC_CR2_EXTTRIG; //Activation du trigger externe
ADCStructPtr->ADC->CR2 |= ADC_CR2_EXTSEL; //Event externe choisie : SWSTART
MyADC_ActiveIT(ADCStructPtr->ADC,2);
2023-03-31 09:37:30 +02:00
ADCStructPtr->ADC->CR2 |= ADC_CR2_ADON; //Init l'ADC
MyADC_Base_Start(ADCStructPtr->ADC); //Debut du premier ADC
}
void MyADC_ActiveIT(ADC_TypeDef * ADC, uint8_t Prio)
{
ADC->CR1 |= ADC_CR1_EOCIE; //Interruption active
NVIC->IP[ADC1_2_IRQn] |= (Prio << 0x4); //Prio de l'interruption (p.197 manuel reference RM0008 pour ADC1_IRQn)
NVIC->ISER[0] |= (1<<ADC1_2_IRQn); //Active l'interruption au niveau NVIC (p.119 manuel programming pour ISER[0])
2023-03-31 09:37:30 +02:00
}
void ADC1_2_IRQHandler(void)
{
if (ADC1->SR & ADC_SR_EOC)
{
ADC1->SR &= ~ADC_SR_EOC; //Prochaine lecture pouvant <20>tre effectu<74>e.
if (pFncADC != 0)
{
(*pFncADC) (ADC1->DR); /* appel indirect de la fonction */
}
MyADC_Base_Start(ADC1);
}
}