My code works fine without interrupt (I tried polling the ADC value) but when I enabled the interrupt, it didn't work - while debugging in Keil, it never reaches the ADC_IRQHandler()
, Also I found that the EOC flag is always set(in the while loop)
This is my code:
#include "stm32f4xx.h" // Device header
volatile int16_t k=0;
int main()
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN | RCC_AHB1ENR_GPIOAEN ;
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
GPIOA->MODER |= GPIO_MODER_MODE0; //analog input
GPIOA->PUPDR &=~ GPIO_PUPDR_PUPD0;
GPIOD->MODER |= GPIO_MODER_MODE13_0;//ouput
GPIOD->MODER &=~ GPIO_MODER_MODE13_1;
GPIOD->OTYPER &=~ GPIO_OTYPER_OT13;
GPIOD->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR13_0 ;
GPIOD->PUPDR &=~ GPIO_PUPDR_PUPD13 ;
GPIOD->ODR &=~ GPIO_ODR_OD13;
ADC1->CR2 |= ADC_CR2_ADON | ADC_CR2_CONT;
ADC1->SMPR2 |= ADC_SMPR2_SMP0_1;
ADC1->SQR3 &=~ ADC_SQR3_SQ1_4; //CHANNEL 0 (PA0)
ADC1->CR1 |= ADC_CR1_EOCIE;
NVIC_EnableIRQ(ADC_IRQn);
NVIC_SetPriority(ADC_IRQn,0);
ADC1->CR2 |= ADC_CR2_SWSTART;
while(1)
{
}
}
void ADC_IRQHandler()
{
k= ADC1->DR;
if(k>2000)
{
GPIOD->ODR ^= GPIO_ODR_OD13;
}
}