-1
\$\begingroup\$

I want to make a LED chaser from right to left in PIC18F4550 microcontroller. Here is a section of the code from a program which I am unable to understand:

LATB=1<<i;  
delay();
LATB=0x00;
delay();
  1. How this work in chasing from right to left?
  2. Why is the delay required?
\$\endgroup\$
2
  • \$\begingroup\$ Try replacing "less than" with "<". Two of them together make a shift operator. \$\endgroup\$
    – user16324
    Commented Oct 15, 2016 at 10:58
  • \$\begingroup\$ I did some rework on the question and title, not even sure that it's still what OP asks. Rajat, can you check that it's still what you want to know? \$\endgroup\$
    – pipe
    Commented Oct 15, 2016 at 11:28

1 Answer 1

1
\$\begingroup\$

The operator << is a left-shift. It shifts each bit a number of positions to the left, and fills the missing pieces with zero:

1<<0 equals 0b00000001
1<<1 equals 0b00000010
1<<2 equals 0b00000100
... etc ...

If i increments from 0 to 7 in a loop, you will get a pattern with a bit that appears to move through every position. The actual order of the LEDs will depend on your physical circuit. If you want it to move in a different direction, make i go from 7 to 0 instead.

The delay is simply required to let you see anything at all. Without the delay, it would scroll through every position too fast to see anything at all. A delay in between makes it turn on the LED, wait a little so that you have time to react, and then turn it off again and leave it off for a while.

The last delay is optional. If you remove it, the effect will be slightly different.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.