Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science

PIC Tutorials - An LED Sequencer
 Before reading this tutorial, make sure you have read the How to use our PIC Tutorials page!

This tutorial will demonstrate an LED sequencer. The LEDs will turn on one after another from right to left. With this tutorial you will become familiar with the bit rotate function.

In Action

The Circuit

One remark needs to be done for the above circuit. There is only one limiting resistor for all 8 LEDs. Normally this would be wrong. A limiting resistor should be added to each LED separately to ensure the same brightness for all the LEDs no matter how many are turned on. But because only one LED is turned on each time, there is no need for separated resistors.

The Code

Let's take a look at the code

```; Main Program ------------------------------------------------------------
Start
bank1					;Go to bank 1
movlw b'11111111'		        ;
movwf TRISA				;Set the port pin types of the RA

movlw b'00000000'		        ;
movwf TRISB				;Set the port pin types of the RB
bank0					;Go to bank 0
MainLoop
bcf Carry
call Wait500mSec
clrf PORTB
bsf PORTB,0				;Set RB0 output
call Wait500mSec
rlf PORTB,f
call Wait500mSec
rlf PORTB,f
call Wait500mSec
rlf PORTB,f
call Wait500mSec
rlf PORTB,f
call Wait500mSec
rlf PORTB,f
call Wait500mSec
rlf PORTB,f
call Wait500mSec
rlf PORTB,f
goto MainLoop
```

This is a rather simple code. First, we set all the RB pins as outputs. Then the MainLoop is continuously looped. 9 steps are within this loop:

• Step 1: The PORTB is cleared and all LEDs are turned off
• Step 2: PORTB bit 0 is set. The first LED (RED) is turned on
• Step 3-9: The PORTB register is rotated left (rlf) and the LEDs are turned on in sequense

When all steps are done, the loop will run again from the beginning.

Notice the 'call Wait500mSec' instruction between each step. This instruction will create a 500 mSec delay. The subroutine 'Wait500mSec' is located inside the header file 'mSecDelays4MHz.inc' that is included in the project. As always, all files needed for this project can be found at the bottom of the page.

The project Files

Following are the files for this project:

 PIC Tutorials - An LED Sequencer

Previous page ---- Next page

Go back to the book contents

Go to the discussion forum of this book

 At 26 October 2011, 20:14:18 user nodoubtman wrote:   [reply @ nodoubtman]There full bunch of errors in the codes, i corrected the code, and compiled it, if you want the code, contact me thanks!

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise without the prior written permission of the author.

All trademarks used are properties of their respective owners.

 HOT in heaven!

 Disclaimer Book Contents Discussion forum

 NEW in heaven! New Theory: AC electric motor working principle