Tool/software:
Hello,
I am trying to implement an ADC comparator interrupt so that the Red LED turns on if the measured voltage is higher than 0.11V and turns off when it is lower than 0.11V, which is about 136/4096 * 3.3V. Below is the code I have. I suspect that something is wrong with the interrupt conditions/configurations in the InitADC and ADC0Seq2Handler. I have correctly updated the vector table in the startup_ccs.c.


#define ADC_SEQUENCER3_LENGTH 1
#define ADC_SEQUENCER2_LENGTH 4
#define ADC_SEQUENCER1_LENGTH 4
#define ADC_SEQUENCER0_LENGTH 8
#define VOLTAGE_PRIORITY 0
#define VOLTAGE_CALIBRATION_FACTOR 1.008
uint32_t VoltBuf[ADC_SEQUENCER2_LENGTH]; // SS2 has the size of 4 samples
uint32_t voltage;
int32_t VoltIntegerPart;
int32_t VoltFractionPart;
void ADC0Seq2Handler(void){
IntMasterDisable();
uint32_t ulStatus = ADCIntStatus(ADC0_BASE, 2, true);
uint32_t comparatorStatus = ADCComparatorIntStatus(ADC0_BASE);
ADCIntClear(ADC0_BASE,2);
if (comparatorStatus & (1 << 0)) {
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
ADCComparatorIntClear(ADC0_BASE, 0);
}
if (comparatorStatus & (1 << 1)) {
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
ADCComparatorIntClear(ADC0_BASE, 1);
}
IntMasterEnable();
}
void InitADC(void){
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3); // Configure PE3 as an ADC input
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); // Enable the ADC0 peripheral
ADCHardwareOversampleConfigure(ADC0_BASE,32); // Set the auto average to 64
// Configure sequence 2 for voltage measurement on AIN0 (PE)
ADCSequenceDisable(ADC0_BASE, 2); // Disable sequence 2
ADCSequenceConfigure(ADC0_BASE, 2, ADC_TRIGGER_PROCESSOR, VOLTAGE_PRIORITY); // Configure sequence 2
ADCSequenceStepConfigure(ADC0_BASE, 2, 0, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE, 2, 1, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE, 2, 2, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE, 2, 3, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END); // Set up the last step and start an interrupt when the conversion is over
ADCSequenceEnable(ADC0_BASE, 2); // Enable the sequence again
ADCIntEnable(ADC0_BASE,0);
ADCIntClear(ADC0_BASE, 2); // Clear the interrupt flag
// Configure the ADC comparator
ADCComparatorConfigure(ADC0_BASE, 1, ADC_COMP_TRIG_NONE | ADC_COMP_INT_HIGH_HALWAYS); ////////////////////////////////////////////////////////////////////////////////// could be wrong
ADCComparatorRegionSet(ADC0_BASE, 0, 136, 4095); // Set the comparator region (136 corresponds to 0.11V)
ADCComparatorReset(ADC0_BASE, 0, true, true); // Reset the comparator
ADCComparatorIntEnable(ADC0_BASE, 0); // Enable the comparator interrupt
ADCComparatorConfigure(ADC0_BASE, 2, ADC_COMP_TRIG_NONE | ADC_COMP_INT_LOW_HALWAYS ); ////////////////////////////////////////////////////////////////////////////////// could be wrong
ADCComparatorRegionSet(ADC0_BASE, 1, 0, 136); // Set the comparator region (136 corresponds to 0.11V)
ADCComparatorReset(ADC0_BASE, 1, true, true); // Reset the comparator
ADCComparatorIntEnable(ADC0_BASE, 1); // Enable the comparator interrupt
IntEnable(INT_ADC0SS2); // Enable the ADC sequence 2 interrupt in the NVIC
IntMasterEnable();
}
void InitGPIO(void) {
// Enable the GPIO port for the LED (PF2)
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1); // Configure PF2 as an output
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0); // Initialize the LED to be off
}
void main(void) {
SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // System clock: 16*12.5/2.5 = 80MHz (MAX)
InitADC();
InitGPIO();
while(1){}
}
Right now, PE3 is collecting the voltage data and I verified that it's working fine. What's wrong?


