#include #include void (*ptr_function) (void); void MyADC_Init(ADC_TypeDef *ADC, char channel){ if (ADC == ADC1) RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; else if (ADC == ADC2) RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; RCC->CFGR |= RCC_CFGR_ADCPRE_DIV6; // Passage de l'horloge a 12MHz ADC->CR2 |= ADC_CR2_ADON; // Démarrage ADC ADC->SQR1 &= ADC_SQR1_L; // Fixe nb de conversion à faire : 1 ADC->SQR3 = channel; // Fixe la voie à convertir } void start_conversion(ADC_TypeDef *ADC){ ADC->CR2 |= ADC_CR2_EXTSEL; ADC->CR2 |= ADC_CR2_EXTTRIG; // Permettre le démarage externe ADC->CR2 |= ADC_CR2_SWSTART; // demander une conversion externe //Si on veut faire par scrutation et non par intéruption, laisser les lignes suivantes //while(!(ADC->SR & ADC_SR_EOC)); // attendre la fin (EOC = 1) //ADC->SR &=~ ADC_SR_EOC; // clear EOC //return ADC->DR &~ (0xF << 12); // retourne 12 bits } void MyADC_ActiveIT(ADC_TypeDef *ADC, int prio, void (*IT_function) (void)){ ptr_function = IT_function; NVIC->ISER[0] |= 1<<18; NVIC->IPR[25] |= prio << 4; ADC->CR1 |= ADC_CR1_EOCIE; } void ADC1_2_IRQHandler(void){ ADC1->CR1 &=~ ADC_SR_EOC; ptr_function(); } //void Set_samp_ts(ADC_TypeDef *ADC, int Rain){ // float ts; // float cylcle; // ts = 9.010913347*(Rain +1000.00)*0.000000000008; // cycle = ts*12/0.000001; // ADC->SMPR1 |= //}