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.

LM4F120H5QR Analog Comparator



Hi, I'm trying to implement a very simple Analog Comparator using the comp.h file in the drivers library.

Currently, my code only has the setup and configuration of the comparator and the turning on and off of an LED after that. However, when running the program gets stuck in the configuration part. Commenting out the ComparatorRefSet() and ComparatorConfigure() functions results in the blinking of the LED as normal.

Can someone help me with this problem? Thanks.

#include "inc/hw_ints.h"

#include "inc/hw_memmap.h"

#include "inc/hw_types.h"

#include "driverlib/adc.h"

#include "driverlib/comp.h"

#include "driverlib/debug.h"

#include "driverlib/fpu.h"

#include "driverlib/gpio.h"

#include "driverlib/interrupt.h"

#include "driverlib/pin_map.h"

#include "driverlib/rom.h"

#include "driverlib/sysctl.h"

#include "utils/uartstdio.h"

 

//*****************************************************************************

//

// Define pin to LED color mapping.

//

//*****************************************************************************

#define BLUE_LED  GPIO_PIN_2

#define GREEN_LED GPIO_PIN_3

 

//*****************************************************************************

//

// The error routine that is called if the driver library encounters an error.

//

//*****************************************************************************

#ifdef DEBUG

void

__error__(char *pcFilename, unsigned long ulLine)

{

}

#endif

 

 

int main(void)

{

       //

           // Enable lazy stacking for interrupt handlers.  This allows floating-point

           // instructions to be used within interrupt handlers, but at the expense of

           // extra stack usage.

           //

           ROM_FPULazyStackingEnable();

 

           //

           // Set the clocking to run directly from the crystal, 16MHz.

           //

           ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |

                              SYSCTL_XTAL_16MHZ);

 

           //

           // Enable the GPIO port that is used for the on-board LED.

           // Enable the GPIO pins for the LEDs.

           //

           ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

           GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, BLUE_LED|GREEN_LED);

 

           //

           // Enable the GPIO port C that is used for Analog Comparator 1.

           // Set GPIO7 (-ve input) as analog comparator input.

           //

           SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);

           GPIOPinTypeComparator(GPIO_PORTC_BASE, GPIO_PIN_4);

 

           //

           // Enable Analog Comparator 1

           //

           SysCtlPeripheralEnable(SYSCTL_PERIPH_COMP1);

 

           //

           // Set internal voltage reference for comparator

           //

           ComparatorRefSet(COMP_BASE, COMP_REF_1_65V);

 

           //

           // Configure comparator

           //

           ComparatorConfigure(COMP_BASE, 1 , (COMP_TRIG_NONE | COMP_ASRCP_REF | COMP_OUTPUT_NONE));

 

           //

           // Delay for some time

           //

           SysCtlDelay(SysCtlClockGet() / 96);

 

              //

              // Turn on Blue LED

              //

           GPIOPinWrite(GPIO_PORTF_BASE, BLUE_LED|GREEN_LED, BLUE_LED);

 

           SysCtlDelay(4000000);

 

              //

              // Turn off Blue LED

              //

           GPIOPinWrite(GPIO_PORTF_BASE, BLUE_LED|GREEN_LED, BLUE_LED & GREEN_LED);

 

           //

           // Loop forever while the timers run.

           //

           while(1)

           {

           }

}

 

  • We're using the analog comparator with great success on a different LX4F device.  And - we inject signals into each comparator input (+ and -) and feed the comp. output to yet another MCU pin.

    Note that your code only configures one pin (Port_C, pin 4).  Yet the introductory code (above) signals that this is pin 7!  I'd review this.

    Our code configures both comparator inputs (via GPIOPinTypeComparator) which happen to be Port C, pins 6,7.  (again, better agrees w/your introductory - not executed code.)  We also configure our comparator output - as we use it.  (via GPIOPinConfigure)  This is Port F, pin 0 - which requires special treatment as it is NMI and must be unlocked - prior to diversion/re-use.

    Perhaps these extra set-ups/configs - which worked for us - will point the way toward your solution...

  • Hi, I've tried that and it works now, thanks. I've been trying to use Analog Comparator 1 instead of 0 as PF0 is used as an input for one of the push buttons on the launchpad and I might want to use that. In the LM4F120H5QR datasheet there are 2 sets of pins used for analog comparators.

    Anyway I tried the same code for both analog comparators again and they work now. I have no idea why....