This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123GH6PM: Analog comparator not working

Part Number: TM4C123GH6PM


Tool/software:

Hello,

I'm trying to write a code that measures the voltage through PC6 and see if it exceeds the reference voltage that I set in the code. My code is shown below and the reference voltage is 0.1375V. When I adjust voltage through the DC power supply (+ terminal connected to PC6 and ground terminal is connected to TM4C's ground), nothing really happens or changes from the initial state. How should I fix my code so that whenever the voltage is below the reference voltage, the RED LED turns off, and when it is above, the RED LED turns on? Also, I think I've correctly updated the vector table for the interrupt.

#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/ssi.h"
#include "driverlib/adc.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "driverlib/interrupt.h"
#include "driverlib/comp.h"


void Comparator0IntHandler(void);
void Comparator_Init(void);

void Comparator_Init(void)
{
    // Enable the peripherals for the comparator and GPIO
    SysCtlPeripheralEnable(SYSCTL_PERIPH_COMP0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);

    // Configure the comparator input pin (e.g., C0+ on PC6)
    GPIOPinTypeComparator(GPIO_PORTC_BASE, GPIO_PIN_6);

    // Configure the internal reference for the comparator
    ComparatorRefSet(COMP_BASE, COMP_REF_0_1375V); // Use the closest available reference voltage

    // Configure the comparator
    ComparatorConfigure(COMP_BASE, 0, COMP_TRIG_NONE | COMP_INT_BOTH | COMP_ASRCP_REF);

    SysCtlDelay(100);


    // Clear any pending comparator interrupts
    ComparatorIntClear(COMP_BASE, 0);

    // Enable comparator interrupt
    ComparatorIntEnable(COMP_BASE, 0);

    // Enable the interrupt in the NVIC (Nested Vectored Interrupt Controller)
    IntEnable(INT_COMP0);
    IntMasterEnable();
}

void Comparator0IntHandler(void)
{
    // Clear the comparator interrupt
    ComparatorIntClear(COMP_BASE, 0);

    // Handle the comparator output (you can add your logic here)
    if (ComparatorValueGet(COMP_BASE, 0))
    {
        // Voltage is above the threshold
        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);  // Turn on the RED LED
       // UARTprintf("high");
    }
    else
    {
        // Voltage is below the threshold
        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);  // Turn off the RED LED
      //  UARTprintf("low");
    }
}


void main(void)
{
    // Set the system clock to 80 MHz
    SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

    // Enable the GPIO port for an LED (for demonstration purposes)
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);

    // Initialize the comparator
    Comparator_Init();

    while (1)
    {

    }
}