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.

Analog comparator 1 on Tiva Launchpad

Other Parts Discussed in Thread: TM4C123GH6PM

Hi all,

I am trying to configure analog comparator 1 on tiva launchpad. Neither I am able to read the output of comparator not I am seeing any output on pin PF1, which is output of comparator 1 on launch pad. I have set internal reference voltage to 1.65 V. and I am giving input on pin PC4.

Below is my code:


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


#ifdef DEBUG
void__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif

int32_t Comp_out = 0 ;

int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);                            // enable port C
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);                             // '' '' F

SysCtlPeripheralEnable(SYSCTL_PERIPH_COMP1);                           // enable comparator 1

GPIOPinTypeComparator(GPIO_PORTC_BASE, GPIO_PIN_5);         // assign pin type
GPIOPinTypeComparator(GPIO_PORTC_BASE, GPIO_PIN_4);
GPIOPinTypeComparator(GPIO_PORTF_BASE, GPIO_PIN_1);


GPIOPinConfigure(GPIO_PC4_WT0CCP0);                                            // pin configure
GPIOPinConfigure(GPIO_PC5_WT0CCP1);
GPIOPinConfigure(GPIO_PF1_C1O);


ComparatorConfigure(COMP_BASE, 1, COMP_TRIG_NONE|COMP_ASRCP_REF|COMP_INT_HIGH| COMP_OUTPUT_NORMAL);                                                                             // comparator configure
ComparatorRefSet(COMP_BASE, COMP_REF_1_65V);                             // set internal reference voltage


SysCtlDelay(SysCtlClockGet() / 96);                                                                 // delay


while(1)
{
SysCtlDelay(2000);

Comp_out = ComparatorValueGet(COMP_BASE, 1);                                                     // read comparator output

SysCtlDelay(2000);
}

}

Please let me know what is missing in the code. and what do I need to get output on pin PF1.

Regards,

Ishan

  • Quickly - can these be correct?

    GPIOPinConfigure(GPIO_PC4_WT0CCP0);     // pin configure  

    GPIOPinConfigure(GPIO_PC5_WT0CCP1);

    Past  assisted you - related comparator issue.   Here's our "known good" Analog Comparator set-up/config: Note: Comp_0 - we feed external voltage signals into both Comp + and Comp -.  (internal too limiting {too coarse} - for our usage)  Note that earlier, elsewhere - we liberated PF0 from its restrictive, NMI fate...  And this code via StellarisWare 9453 - used w/LX4F...

            ROM_ComparatorConfigure(COMP_BASE, 0, (COMP_TRIG_NONE |
                                    COMP_ASRCP_PIN0 | COMP_OUTPUT_NORMAL));
     
            ROM_GPIOPinConfigure(GPIO_PF0_C0O); //  Comp Output

            ROM_GPIOPinTypeComparator(GPIO_PORTC_BASE, GPIO_PIN_6 | GPIO_PIN_7); 

    Note that you "read" the comparator - much as we do.   Here's our code - enabling scope-free (i.e. Led) Comp verify:

              while (1)
              {
                    k = ROM_ComparatorValueGet(COMP_BASE, 0);
               
                         if (k == 1)
                        {
                             HWREG(GPIO_PORTD_BASE + GPIO_O_DATA + (0x40 << 2 )) |= 0x40;  // Comp Led ON
                       }
                        else
                        {
                            HWREG(GPIO_PORTD_BASE + GPIO_O_DATA + (0x40 << 2)) &= ~(0x40);  // Comp Led OFF
                        }             
               
                  ROM_SysCtlDelay(40000);
              }

  • Hi,

    Thank you for assisting me again. 

    I removed below lines. Yes it was big mistake .

    GPIOPinConfigure(GPIO_PC4_WT0CCP0);     // pin configure  

    GPIOPinConfigure(GPIO_PC5_WT0CCP1);

    now I can read the comparator value but still no sign of output of PF1. 

    Code now is in sequence that you suggested.

    ComparatorConfigure(COMP_BASE, 1, COMP_TRIG_NONE|COMP_ASRCP_REF|COMP_INT_HIGH| COMP_OUTPUT_NORMAL);                                                                                              // comparator configure
    ComparatorRefSet(COMP_BASE, COMP_REF_1_65V);                                             // set internal reference voltage

    GPIOPinConfigure(GPIO_PF1_C1O);
    GPIOPinTypeComparator(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5);
    SysCtlDelay(SysCtlClockGet() / 96);                                                                                   // delay

    I am interested in output on pin PF1, because like your, I also like to feed it into another MCU.

    Regards,

    Ishan

  • Not to feel bad - Analog Comparator function is fairly new - and not much detailed - anywhere obvious - that we could find.  Much trial/error - yielded our findings.

    To insure an output - voltage level upon Comp + must exceed that upon Comp -.  (assumes you have set-up as our code shows - which avoids use of internal Ref V - for test simplicity.) 

    I past reported that Comp Out did not require pull-up - on our LX4F device.  (tested across multiple boards - multiple MCUs)  Cannot hurt for you to add pull-up R (4K7 - 10K) to your Comp Out - to see if that aids.  Again - I'd eliminate the internal V Ref (at least for early stage testing) - use code provided - and observe (and report) results...

    And - insure that your Comp Out is not tied to another pin - which may default into "output" - and "collide" w/ Comp Out!