I created a program to glow LED once a flag is set. And until a switch is pressed, the LED should continue to glow. The switch is in pull up mode. But the LED is not glowing at all. Please guide.
while(1) { // Inside indefinite loop, another function sets a flag switch(flag) { case 0: Glow_RedLED(); break; case 1: Glow_GreenLED(); break; } //While Loop ends here } The Glow_GreenLED() is as below: void Glow_GreenLED() { LPC_PINCON->PINSEL4 &= ~((1<<26) | (1<<27)); LPC_GPIO2->FIODIR|= (1<<13); LPC_PINCON->PINMODE4 &= ~((1<<26) | (1<<27)); LPC_GPIO2->FIOSET = (1<<13); } The Glow_RedLED() is as below: void Glow_RedLED() { LPC_PINCON->PINSEL4 &= ~((1<<16) | (1<<17)); LPC_GPIO2->FIODIR|= (1<<8); LPC_PINCON->PINMODE4 &= ~((1<<16) | (1<<17)); LPC_GPIO2->FIOSET = (1<<8); }
Is flag volatile? Show a complete example
What micro, board, and pins?
When stuff isn't working it is helpful to provide a complete view of things. Could the problem be with code you don't show, or hardware not described.
If you just enable the RED and GREEN LED without the switch does that work?
Yes, the Led's are working independently when driving through switch or without switch. I am using LPC1768 Micro controller. The Switch input pin is in Pull-Up state. The flag is not volatile. I am using Port2 on COINELTECH Development board. Pins 8 and 13 are used for LED output, and Pin 11 is used for Switch Input. The problem I am facing is that the controller's pin is not retaining its state after its being set in if and else statement below for loop. It is instead jumping to execute the next statement, (break;). The entire main() is as follows. The Switch() doesn't set any flag,it only returns its input pin logic level. The flag is set only when read() sets it. The timer0_init() function is used just to execute the read() in predefined time. It has no use in glowing of LED's. Clear_LED() just resets the LED's pin. Device_On() blinks LED for 200ms. The execution is going fine till for loop . I don't have debugger to debug the code. Please help.
int main() { SystemInit(); timer0_init(); int Baudrate[8] = {2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400}; int i; while(1) { if(Switch()==0) { Clear_LED(); delay_ms(200); Device_On(); for(i=0; i<=8; i++) { uart_init(Baudrate[i]); read(); if(flag0==1) break; } if (flag0==1) { Glow_GreenLED(); break; } else if(flag0==0) { Glow_RedLED(); break; } continue; } } }