Loops with C18 Compiler on mplabide for 18f4550

control-loopmicrocontrollerpic

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)
        {
          blink-RB-two-led-alternatively-forever(); // a function prototype for blinking RB0 and RB1 Alternatively. 

         } // 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)
        {
          blink-RB-two-led-alternatively-forever(); // a function prototype for blinking RB0 and RB1 Alternatively. 

          blink2-RD-two-led-alternatively-forever(); // a function prototype for blinking RD0 and RD1 Alternatively. 

         }

   }

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.

Thanks a lot in advance.

Best Answer

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)