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
  • "Switch Pin is in Pull Up state". The LED's are connected to Micro controller pin and GND. When the switch is pressed, it should drive the LED Pin high, I however rectified this program, now I am facing trouble while employing switch in another program.

    < uint32_t switchStatus;

    LPC_PINCON->PINSEL3 = 0x000000;

    LPC_GPIO1->FIODIR = (0<<SwitchPinNumber));

    SystemInit();

    timer0_init();

    switchStatus = (LPC_GPIO1->FIOPIN>>SwitchPinNumber) & 0x01 ;

    if(switchStatus ==0 )

    {

    while(1)

    { for(i=0; i<=8; i++) {

    uart_init(Baudrate[i]);

    read();

    if(flag0==1)

    break;

    }

    if (flag0==1)

    Glow_GreenLED();

    else

    Glow_RedLED();

    }

    }

    }
    >

Reply
  • "Switch Pin is in Pull Up state". The LED's are connected to Micro controller pin and GND. When the switch is pressed, it should drive the LED Pin high, I however rectified this program, now I am facing trouble while employing switch in another program.

    < uint32_t switchStatus;

    LPC_PINCON->PINSEL3 = 0x000000;

    LPC_GPIO1->FIODIR = (0<<SwitchPinNumber));

    SystemInit();

    timer0_init();

    switchStatus = (LPC_GPIO1->FIOPIN>>SwitchPinNumber) & 0x01 ;

    if(switchStatus ==0 )

    {

    while(1)

    { for(i=0; i<=8; i++) {

    uart_init(Baudrate[i]);

    read();

    if(flag0==1)

    break;

    }

    if (flag0==1)

    Glow_GreenLED();

    else

    Glow_RedLED();

    }

    }

    }
    >

Children