Skip to main content
Nick-
Associate II
March 9, 2022
Question

ADC interrupt never gets called

  • March 9, 2022
  • 2 replies
  • 1519 views

Hi, i'm struggling with this piece of code here:

int main(void){
	UART2_Tx_init();
	ADC_interrupt_init();
 
 
	while(1){
 
	}
 
}
 
/***********ADC***********/
 
void ADC_interrupt_init(void){
	RCC->AHB1ENR |= (1U<<0); //GPIOA clock enable
	GPIOA->MODER |= ((1U<<10) | (1U<<11)); //set PA5 analog mode
	RCC->APB2ENR |= (1U<<8);//ADC1 clock enable
	ADC1->SQR3 |= (0x05<<0); //Ch5 in 1st regular sequence
	ADC1->SQR1=0;//only 1 ch used
	ADC1->CR1 |= (1U<<5);//EOCIE: end of conversion interrupt
	NVIC_EnableIRQ(ADC_IRQn);//enable ADC interrupt in NVIC
	ADC1->CR2 |= (1U<<1); //CONT: continuous conversion
	ADC1->CR2 |= (1U<<0);//ADON: enable ADC
	ADC1->CR2 |= (1U<<30); //SWSTART: start conversion
}
 
void ADC_IRQHandler(void){
 
	if((ADC1->SR & ADC_SR_EOC)!=0) //Check if end-of-conversion interrupt occurred
	{
 
		ADC1->SR &= ~ADC_SR_EOC; //Clear EOC flag
		uint32_t adc_value = ADC1->DR;
		printf("%d \n\r", (int)adc_value);
	}
 
}

I'm trying to get an interrupt every time the ADC module complete a conversion and then print the result through an USART interface. I tried the USART interface separately and it seems work fine. Also the ADC1-DR register gets increased or decreased by the action of the potentiometer. The problem in my opinion is that the interrupt function "void ADC_IRQHandler(void)" never gets called so i'm trying to understand why. Someone have an idea? I'm using an STM32F401RE. Thanks in advance

This topic has been closed for replies.

2 replies

TDK
Super User
March 9, 2022

How are you inferring that ADC_IRQHanlder isn't called? Just no printf output or are you debugging with a breakpoint to verify?

Is this C or C++? If the latter, need correct linkage.

ADC and IRQ setup looks correct. Examine registers to verify they are as you have set them.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Nick-
Nick-Author
Associate II
March 9, 2022

First of all, thank you for your answer. This is a C program and I assume that printf and UART both works because I tried them in the while loop (whitout using any interupt) and no problem occurred. I said that ADC_IRQHanlder probably isn't called because I also tried to execute some other thing inside it rather than the UART printf stuff and none of them works. Also the EOC flag never gets cleared .