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.

CCS/TM4C123GH6PM: analog comparator

Part Number: TM4C123GH6PM


Tool/software: Code Composer Studio

hello, I am writing the code for the analog comparator in which potentiometer is used as the external reference and in the handler of comparator I have to read the value of the comparator. please, anyone can help me to solve it out? my code is not going in the interrupt handler.

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/gpio.h"
#include "inc/hw_comp.h"
#include "driverlib/comp.h"
#include "driverlib/interrupt.h"
#include "inc/hw_ints.h"

uint32_t comp_value;

void compinthandler(void)
{
comp_value= ComparatorValueGet(COMP_BASE,0);
ComparatorIntClear(COMP_BASE,0);

GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1,GPIO_PIN_1);
SysCtlDelay(5000000);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1,0);
SysCtlDelay(5000000);
}

int main(void)
{
volatile uint32_t ui32Loop;
uint32_t ui32ADC0Value[4];

//set clock frequency.
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
//enable GPIO PORTF.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
//enable ADC0.
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
//enable comparator COMP0.
SysCtlPeripheralEnable(SYSCTL_PERIPH_COMP0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);

GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_3);
GPIOPinTypeComparator(GPIO_PORTC_BASE, GPIO_PIN_6);
GPIOPinTypeADC(GPIO_PORTE_BASE,GPIO_PIN_3);


ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceEnable(ADC0_BASE, 1);
ADCProcessorTrigger(ADC0_BASE, 1);
ADCSequenceDataGet(ADC0_BASE, 1, ui32ADC0Value);


ComparatorRefSet(COMP_BASE, COMP_REF_1_65V);
ComparatorConfigure(COMP_BASE, 0,(COMP_TRIG_NONE| COMP_INT_BOTH |COMP_ASRCP_REF| COMP_OUTPUT_NORMAL));
ComparatorIntEnable(COMP_BASE,0);
IntEnable(INT_COMP0);
IntMasterEnable();

while(1)
{
// comp_value= ComparatorValueGet(COMP_BASE,0);
//
// GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1,GPIO_PIN_1);
// SysCtlDelay(5000000);
// GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1,0);
// SysCtlDelay(5000000);
}

}

  • Did you edit the "startup_ccs.c" file and replace "IntDefaultHandler" with your interrupt routine "compinthandler"?
  • Hi,

    apeksha said:
    ComparatorRefSet(COMP_BASE, COMP_REF_1_65V);
    ComparatorConfigure(COMP_BASE, 0,(COMP_TRIG_NONE| COMP_INT_BOTH |COMP_ASRCP_REF| COMP_OUTPUT_NORMAL));

    Also note your comparator is configured for internal VREF, not an external potentiometer.

  • i am comparing voltage from potentiometer to internal VREF. now it going in handler for zero volt but when i increase voltage its value is not changed.
  • That would be a very strange configuration unless only to check (Cn+ ASRCP) threshold crossing. Perhaps pots to much noise in movement need to have hysteresis? Please post simple drawing of your test circuit. With ASRCP it seems we must use Cn- inputs to your test signal. This case/s being true invert output may produce better results.
  • When using the internal reference, it is attached to the + side of the comparator. Your external input needs to be attached to the - side. Change to GPIO_PIN_7 in your call to GPIOPinTypeComparator() and move your external input to PC7.