我尝试在 stm32f091rc 中以中断模式配置 adc,但从中断请求处理程序调用的回调函数出现问题。我在下面附上了我的代码,我尝试遵循 stm32f4 上的教程,并为我的特定 MCU 移植了代码,但它不起作用。
#define ADCENABLE (1U<<9)
#define GPIOAENABLE (1U<<17)
#define ADC_CH1 (1U<<1)
#define CR_ADEN (1U<<0)
#define CFGR_CONT (1U<<13)
#define CR_EOCIE (1U<<2)
void pa1_adc_interrupt_init(void)
{
/* Configure the ADC GPIO Pin */
/* Enable Clock access to gpioa */
RCC->AHBENR |= GPIOAENABLE;
/* Set the mode of PA1 to analog mode */
GPIOA->MODER |= (1<<2);
GPIOA->MODER |= (1<<3);
/* Configure the ADC module */
/* Enable clock access to ADC */
RCC->APB2ENR |= ADCENABLE;
/* Enable ADC end-of-conversion interrupt */
ADC1->IER |= CR_EOCIE;
/* Setting Priority */
//NVIC_SetPriority(ADC1_COMP_IRQn, 0);
/* Enable ADC interrupt in NVIC */
NVIC_EnableIRQ(ADC1_COMP_IRQn);
/* Configure adc parameters */
/* Conversion sequence start */
ADC1->CHSELR = ADC_CH1;
/* Conversion sequence length */
/* Enable ADC module */
ADC1->CR |= CR_ADEN;
}
static void adc_callback(void)
{
sensor_value = ADC1->DR;
printf("the sensor value is %d \n\r", (int)sensor_value);
}
void ADC_COM_IRQHandler(void)
{
/* Check for eoc in SR */
/*
if(ADC1->ISR & ADC_EOC)
{
// Clear EOC
ADC1->ISR &= ~ADC_EOC;
// doing sth here
adc_callback();
}
*/
if(EXTI->PR & (1U<<21))
{
adc_callback();
val = 1;
EXTI->PR |= (1U<<21);
}
}
adc_callback 有点没有被调用,我尝试在挂起状态读取为 1 时调用回调,并用 1 清除它以指示挂起已清除。
任何人都可以帮助我解决我面临的问题吗?
prinf
ADC1_COMP_IRQHandler
。检查您的向量表定义。可能还有很多其他问题。
我建议你用手中的参考手册来编写代码,而不是遵循互联网教程。