I am trying to use interrupts with the ADC module. I am using PA0 and PA1 as analog inputs for the ADC, and PC0 and PC1 as external LEDs. My goal is: when PA0 is triggered, PC0 should turn on. when PA1 is triggered, PC1 should turn on.
However, I am facing a problem: when I apply 3.3V to PA0, both LEDs turn on. And when I try to trigger PA1, nothing happens. I think there are two logical errors, but I cannot figure them out. Also there are no hardware or cable issues in the circuit.
#include "main.h"
volatile uint8_t adc_index = 0;
void SystemClock_Config(void);
void ADC_IRQHandler(void)
{
if(ADC1->SR & (0x1 << 1))
{
uint8_t val = (uint8_t)ADC1->DR;
if(adc_index == 0)
{
if(val > 127)
GPIOC->BSRR = (1 << 0);
else
GPIOC->BSRR = (1 << 16);
}
else
{
if(val > 127)
GPIOC->BSRR = (1 << 1);
else
GPIOC->BSRR = (1 << 17);
}
adc_index++;
if(adc_index >= 2) adc_index=0;
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
RCC->AHB1ENR |= (0x5 << 0);
RCC->APB2ENR |= (0x1 << 8);
// PA0 as Analog Input for ADC
GPIOA->MODER |= (0x3 << 0);
// PA1 as Analog Input for ADC
GPIOA->MODER |= (0x3 << 2);
// PC1 as General output for external Led
GPIOC->MODER &= ~(0x3 << 2);
GPIOC->MODER |= (0x1 << 2);
// PC0 as General output for external Led
GPIOC->MODER &= ~(0x3 << 0);
GPIOC->MODER |= (0x1 << 0);
// ADC Cont mode on
ADC1->CR2 |= (0x1 << 1);
// Interrupt enable for ADC
ADC1->CR1 |= (0x1 << 5);
// 8 bit Resolution
ADC1->CR1 &= ~(0x3 << 24);
ADC1->CR1 |= (0x2 << 24);
// 2 Channel conversion
ADC1->SQR3 |= (0x0 << 0);
ADC1->SQR3 |= (0x1 << 5);
// 2 CONVERSION Selected
ADC1->SQR1 |= (0x1 << 20);
// Sample Rate 84 CYCLES
ADC1->SMPR2 &= ~(0x7 << 0);
ADC1->SMPR2 |= (0x4 << 0);
// ADC_IRQHandler unmasked
NVIC->ISER[0] = (0x1 << 18);
// ADC On
ADC1->CR2 |= (0x1 << 0);
// Software Start
ADC1->CR2 |= (0x1 << 30);
while (1)
{
}
}