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);
} >
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.
"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));
timer0_init();
switchStatus = (LPC_GPIO1->FIOPIN>>SwitchPinNumber) & 0x01 ;
if(switchStatus ==0 )
{ for(i=0; i<=8; i++) {
uart_init(Baudrate[i]);
read();
if(flag0==1)
break;
if (flag0==1)
Glow_GreenLED();
Glow_RedLED();
No surprise. Look at what your program is trying to do. Not very smart :(
Can you please tell me where I am going wrong?
Sorry but I'm not here to let people cheat with their first year school projects.
Instead of your profound sarcasm, I would have really appreciated your guidance. I was not asking you to write the source code, if that's what you thinking mate!
OK.
Firstly:
Look at what you've posted. Look at how you've posted it.
Did you read the information under http://www.keil.com/forum/tips.asp ?
It's terrible.
Then:
Did you read my previous post? did you actually understand it? Your second code snippet (aka mess) indicates that you didn't do at least one of those things
The code snippet is incomplete and has obvious things missing. Example, wtf is flag0? where do you define it? where do you initialise it?
Why are you initialising the UART within a loop like that?
In summary:
It's really not very good. My pre-teen daughter recognised some of the faults in what you had posted!