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.

Issue with Turning LED ON-OFF with Switch input

Other Parts Discussed in Thread: MSP430F6638

Hello, This is my First Post to TI Community......I am a Beginner in the Field of Microcontroller and I am using MSP-TS430PZ100USB Launch Pad with MSP430F6638 Controller

I am Writing a Program that will Turn the LED ON until a Switch is Pressed.

My Problem is, I am not able to Turn the LED completely off. I mean LED Remain ON all Time and If i Press the Switch it only Increases the Intensity of LED and after I release the switch it step back to it initial condition (Dim the LED's Intensity)

I am using a Pull Up Network to Read the Switch Input

My Code is  :

#include <msp430.h> 


#define SW BIT1
#define LED BIT0

int main(void) {
    WDTCTL = WDTPW | WDTHOLD;           // Stop WatchDog timer
    P1OUT &= ~LED;                                        //Make LED off Initially
    P1DIR |= LED;                                             //Set Direction of LED as Output

    while(1){
    	while((P1IN & SW) == 1);                        //Wait Till the Switch is Pulled Up
    	P1OUT |= LED;                                        //Turn on the LED  (Active High)
    	while((P1IN & SW) == 0);                       //Wait Till the Switch is Pulled Down i.e; until Switch is Pressed
    	P1OUT &= ~LED;                                    //  Turn off the LED

    }
	return 0;
}

Please help me.

  • Sumir Kumar Jha said:
    while((P1IN & SW) == 1);

    Since SW is BIT1 (= 0x02), P1IN & SW can only be 0 or 2, but never 1. So the first while is a nop. The LED is always switched on. then. The next while will wait if the button is pressed, but if it is not pressed, the LED will be switched off again, resulting in a ~50% DC on-off cycle which you see as 'Dimmed' LED.

    Not to mention that you forgot about button bouncing (you might consider adding a 100nF pulldown capacitor i parallel to the switch, to suppress bouncing)

  • Thanks ...It Helped me 

**Attention** This is a public forum