This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Reverse working of Switch

When I am executing this code, the LED is ON by default whereas it should only be ON when switch is pressed. And when I press the switch, it turns off. Can anyone please explain me, what I am doing wrong with my code.

</ #include <lpc17xx.h>

#define SwitchPinNumber 11

#define LedPinNumber 6

void main()

{

uint32_t switchStatus;

SystemInit();

LPC_PINCON->PINSEL4 = 0x000000; //Configure the Pins for GPIO;

/* Configure the LED pin as output and SwitchPin as input */

LPC_GPIO2->FIODIR = ((1<<LedPinNumber) | (0<<SwitchPinNumber));

while(1)

{

/* Turn On all the leds and wait for one second */

switchStatus = (LPC_GPIO2->FIOPIN>>SwitchPinNumber) & 0x01 ; //Read switch status

if(switchStatus == 1)

{

LPC_GPIO2->FIOPIN = (1<<LedPinNumber);

}

else

{

LPC_GPIO2->FIOPIN = (0<<LedPinNumber);

}

}

} >

Parents
  • Two two things spring to mind.

    How is the switch connected? Does the port pin have a pull up and a switch that takes the line to ground or does the port pin have a pull down and a switch that takes the line to +VE ?

    How are the LEDs connected? One end tied to ground and the other to the port pin that goes high to turn it on or one end tied to +VE and the other to a port pin that goes low to turn it on?

    Consider these and consider what your code is doing.

Reply
  • Two two things spring to mind.

    How is the switch connected? Does the port pin have a pull up and a switch that takes the line to ground or does the port pin have a pull down and a switch that takes the line to +VE ?

    How are the LEDs connected? One end tied to ground and the other to the port pin that goes high to turn it on or one end tied to +VE and the other to a port pin that goes low to turn it on?

    Consider these and consider what your code is doing.

Children