2022-03-09 04:39 AM
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
2022-03-09 05:30 AM
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.
2022-03-09 05:59 AM
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 .