# loops with C18 Compiler on mplabide for 18f4550

I am facing trouble with glowing multiple LED simultaneously on a 18f4550 micro-controller under the same " while(1) ". I am new programming. [C18 compiler with Mplab ide.]

I was able to alternate blink RB0 and RB1 led with a piece of code found on web by setting the RB pins to output and then making it high.

#include<pic18f4550>
// Chip config and setting for make RB pins output

void main(void) // Main block
{

While(1)
{

} // loop forever

}


That works Fine without any problem. I can do same with PortD pins as well., With no problems .

But I want RD0 and RD1 to blink alternatively , Simultaneously with RB0-RB1.

Means when I Power on the pic18f4550 I wish to get both RB0-RB1 and RD0-RD1 Flash alternatively and Simultaneously.

I tried this. But didn't work.

void main(void) // Main block
{

While(1)
{

}

}


The loop goes into " blink-RB-two-led-alternatively-forever(); " and keeps Flashing the RB0 and RB1 pins and it never gets to Second function Prototype.

If I change the Order Then RD0 -RD1 starts to flash alternatively But it never comes to PortB.

Please Suggest me a Solution. How to make both the things to Work Simultaneously together.

It sounds like the function "blink-Rx-two-led-alternatively-forever();" actually stays in the loop forever.

We don't have the code for this subroutine, but I'd assume that the code hits the first function (whichever one you place first, as you described), goes into it, and blinks the LED forever, just like it says in the prototype name.

If you want to blink both LED's, start by initializing your ports B and D to outputs (the TRISx register). Then, enter the while(1) loop [1], exclusive OR each bit, and add a delay to set the timing or it will blink so fast it will just look dim. A delay can be as simple as setting up a for loop with a big number and decrementing until you hit 0.

EDIT: The exclusive OR operation toggles a bit (a 1 becomes a 0 and a 0 becomes a 1), so, e.g., for variable uint8_t my_light, to toggle bit 0, you would write:

my_light ^= 0b00000001;

or you could write: my_light = my_light ^ 0b00000001;

The "^" is the exclusive OR operation.

Also, if you are going to use C, pick up a copy of Kernighan and Ritchie; I couldn't do without it.

[1] a for(;;) to create an infinite loop is often preferred to using while(1)

• Thanks You Exactly got my point sir :) Yes the Function here stays in loop forever. I got your point as you mentioned by settings Port D & B to output.and delays. But i Dont know How to Do the Ex-OR for bits. Can you provide some Example block of codings ? :) – Sanju Sep 17 '13 at 13:53
• I edited it for you, I hope that helps! – Bob Sep 17 '13 at 14:18
• ok something like . led1 = 1 led2 = !led1 ? – Sanju Sep 17 '13 at 14:40
• just: led1 ^= 1; led2 ^=1; would work. Alternatively, you could loop around: turn both on, wait, turn both off, wait, which is twice the code but would get the job done – Bob Sep 17 '13 at 16:39