Voilier/Drivers/Sources/Driver_ADC.c

66 lines
1.8 KiB
C

#include "Driver_ADC.h"
#include "Driver_GPIO.h"
void (*PtrfctADC)(void); //Déclaration du pointeur de fonction ADC
//----------------------INIT--------------------//
void MyADC_Base_Init(MyADC_Struct_TypeDef * ADC, void (*fct)(void)){
MyGPIO_Struct_TypeDef * GPIO_ADC; //Déclaration du GPIO de l'ADC
PtrfctADC=fct; //Affectation du pointeur de fonction ADC
//Division par 6 de la clock (72MHz) pour l'ADC (14MHz max)
RCC->CFGR |= RCC_CFGR_ADCPRE_DIV6;
//Affectation du channel PC0 pour l'ADC
GPIO_ADC->GPIO = GPIOC;
GPIO_ADC->GPIO_Conf = In_Analog;
GPIO_ADC->GPIO_Pin = 0;
//Sart et choix de ADC1 ou ADC2
MyADC_Base_Start(ADC->ADC);
MyADC_Base_Interuption(ADC->ADC);
}
//-------------------------------------------------------------------//
//----------------------------- FONCTIONS ---------------------------//
//-------------------------------------------------------------------//
//---------------------START--------------------//
void MyADC_Base_Start(ADC_TypeDef * ADC){
if(ADC == ADC1){
ADC1->CR2 |= ADC_CR2_ADON; //Enable ADC1
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; //Clock enable ADC1
} else if (ADC == ADC2){
ADC2->CR2 |= ADC_CR2_ADON; //Enable ADC2
RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; //Clock enable ADC2
}
}
//---------------------STOP--------------------//
void MyADC_Base_Stop(ADC_TypeDef * ADC){
if(ADC == ADC1){
ADC1->CR2 &= ~ADC_CR2_ADON;
}else if(ADC == ADC2){
ADC2->CR2 &= ~ADC_CR2_ADON;
}
}
//-----------------INTERRUPTION-------------//
void MyADC_Base_Interuption(ADC_TypeDef * ADC){
ADC->CR1 |= ADC_CR1_EOCIE; //Interruption active
NVIC->ISER[0] |= (0x1<<ADC1_2_IRQn); //Interruption active au niveau du coeur
//GESTION PRIO ??????????????
}
//--------------------HANDLER---------------//
void ADC1_2_IRQHandler (void) {
PtrfctADC();
ADC1->SR &= ~(1<<1); //RAZ du flag end of conversion
}