I am getting up-to-speed on the TI MSP430. I am attempting to write code in C to access the built-in comparator on MSP430G2553. I've got a simple program for the comparator but I must be doing something wrong and I'm hoping someone can spot my error.

For my setup I have a potentiometer across Vcc and Gnd with the wiper arm going to P1.1. The comparator is configured to use the internal Vcc/2 reference. My MSP430 is in the TI Launchpad board and I'm trying to drive the red and green LEDs there based on the state of the comparator. If the comparator output is low I want the green LED on and the red LED off; vice-versa if the comparator output is high. I also configured P1.7 to show the contents of the CAOUT register. My code is here:

#include <msp430.h>
#include <msp430g2553.h>

/* This program is for the Launchpad MSP430 development board.
* There is a potentiometer connected from Vcc to Gnd with the
* wiper arm going to CA1 (P1.1).  When the wiper voltage is
* below the threshold the green LED should be lit.  When it
* exceeds the threshold the red LED should be lit.
* For this setup Vcc = 3.53 V
*  reference = Vcc/2 = 1.77 V
*        Vin = 1.76 V and higher: green LED on?
*        Vin = 1.70 V and lower: red LED on?

#define GREEN    BIT0
#define RED      BIT6
#define SWITCH   BIT3

int main(void) {
    WDTCTL = WDTPW + WDTHOLD;  // disable watchdog
    P1DIR = GREEN + RED + BIT7;  // P1.0 and P1.6 and P1.7 are outputs
    CACTL2 = P2CA4;  // P1.1 = +comp
    CACTL1 = CARSEL + CAREF_2 + CAON;  //  -comp = 0.5*Vcc; comparator on
    P1SEL = BIT7;  // P1.7 updates based on CAOUT

    unsigned int led_mask = 0;

        led_mask = P1OUT;
        if (CACTL2 & 0x01) {
            led_mask &= ~GREEN;  // green LED off
            led_mask |= RED;     // red LED on
        } else {
            led_mask &= ~RED;    // red LED off
            led_mask |= GREEN;   // green LED on
        P1OUT = led_mask;

It compiles and uploads fine but it doesn't behave the way that I intended. When I provide an input voltage of 2 volts I exceed the threshold and CAOUT goes high (verified on P1.7) as expected. However my code is turning on LEDs as though CAOUT were low. Also, if I provide an input voltage of 1.4 volts I'm below the threshold and CAOUT is low (verified on P1.7) as expected. But my code is turning on LEDs as though CAOUT were high.

Why am I not getting the expected results?


2 Answers 2


The wiring was correct and the code was mostly correct. It came down to incorrect #define statements. To fix the problem all I had to do was change them to

#define GREEN    BIT6
#define RED      BIT0

I've tested your code on my Launchpad and it works well. I have connected 22k potentiometer to Vcc and GND pins (on Launchpad), and my Launchpad is powered from PC (via USB cable). I have measured that my Vcc = 3.57 V, and LEDs are switching at V = 1.77 V. P1.7 is also switching exactly at this moment.

So your code is guaranteed to be correct (proved on my setup). Therefore something isn't right with your hardware setup. Here is some guesses what you may want to check:

  • both J5 jumpers are installed (so that LEDs are connected to MSP430 pins)
  • try to just light on both of your LEDs in your code, to be sure they are connected properly and fully functional
  • check all of your wiring connections (all wires are connected to correct pins, connections are good, and there are no short circuits there)
  • try another multimeter/oscilloscope when measuring voltages

Also, try to build your project with my Makefile, maybe yours has some issues:

MCU = msp430g2553
BOARD = rf2500
XTAL = 1000000
SOURCES = src/main.c

CFLAGS = -mmcu=$(MCU) -mdisable-watchdog -Os -s -Wall -Iinclude/ \

all: $(SOURCES)
    $(CC) $(CFLAGS) $(SOURCES)

flash: a.out
    sudo mspdebug $(BOARD) "prog a.out"

    rm -f a.out

    mspdebug $(BOARD)

.PHONY: clean debug

Finally, some advices regarding your code:

  • do not include <msp430g2553.h>, it's being included automatically in <msp430.h>, using -mmcu=msp430g2553 parameter, which you are passing to GCC when building your project. This way you can make your code more reusable (for different MCUs). And anyway, it's just duplicating things, not bringing anything new.
  • move led_mask declaration inside of your while loop
  • return 0 explicitly in main()
  • SWITCH definition is unused in your code, consider removing
  • \$\begingroup\$ Both J5 jumpers are there. The problem isn't that my LEDs aren't lighting up - they do light up. The problem is that they light at the wrong time. The meter works. I get Vcc = 3.6. When I monitor CAOUT on P1.7 it takes on the correct high/low levels that I expect given the input. It seems to me that my code is somehow reading the inverse of CAOUT. \$\endgroup\$
    – user1721015
    Mar 14, 2015 at 18:37
  • \$\begingroup\$ So your LEDs are switching after P1.7? How much after? I mean, there is some transition zone, where you can actually see both LEDs are glowing simultaneously, and at this moment your comparator just switching back and forth with high frequency. But this zone is about 0.2V wide (for me). And I can confirm that LEDs are switching exactly at the same time when P1.7 does. So it's some hardware issue with your setup. It's hard to tell which is it. I'd propose you to replace all your wires with brand new ones, along with potentiometer, and also redo your wiring from scratch. \$\endgroup\$
    – Sam Protsenko
    Mar 14, 2015 at 19:02
  • \$\begingroup\$ LEDs are not switching after P1.7. They change right along with it. When comparator output is high I want green LED off and red LED on. What I'm getting is that comparator out high yields green on and red off. This is opposite of what I expected to see. When comparator is low I want green on and red off but I get green off and red on. P1.7 = CAOUT so I can see that comparator output itself is high/low as expected. I can't figure out why it is when I can see that CAOUT =1 (verified on P1.7) my test for (CACTL2 & 0x01) is apparently showing CAOUT = 0. \$\endgroup\$
    – user1721015
    Mar 14, 2015 at 19:14
  • \$\begingroup\$ Argh! I finally found the problem. I had my RED and GREEN definitions reversed! It should be RED = BIT0 and GREEN = BIT6. I had it the other way around. Thanks for taking the time to look into this. \$\endgroup\$
    – user1721015
    Mar 14, 2015 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.