Program a reversing light up LED

Thread Starter

changwen

Joined Apr 4, 2017
7
  1. A running light going 1 direction using three LEDs.
  2. Every single LED will be light up after 1ms.
  3. When SW1 is pressed, the running light will reverse direction.
I was struggle at this question, which it is program Using PIC16F877A by Mplab. Can anyone at there help me .
 

LesJones

Joined Jan 8, 2017
4,511
Have I understood what yo want correctly.
Start LED 1 comes on.
At T = 1/3 mS LED 1 switches off and LED 2 switches on
At T = 2/3 mS LED 2 switches off and LED 3 switches on
At T = 1mS all 3 LEDs switch on and stay on until SW1 is pressed
S1 is pressed LEDs 1 and 2 switch off.
1/3 mS after SW1 was pressed LED 3 turns off and LED 2 turns on.
2/3 mS after SW1 was pressed LED 2 turns off and LED 1 turns on.
You have not defined what you want to happen at this point.

I would suggest that you write a delay subroutine just counting instruction cycles to generate a 333 uS delay. (I am assuming the micro is not doing anything else so you do not use an interrupt routine using one of the timers to trigger the state changes.)
I would then either just use BSF/BCF indstructions to set and clear the bits in the I/O register (PORTx) that the LEDs are driven from or load the "W" register with a value (Using a MOVLW instruction) then copy the "W" register to the I/O port. (Using a MOVWF instruction.) It is not a very good programming technique. I don't see why you need a 40 pin PIC to do what you are doing. You could do it with a PIC12F629 (8 pin.)

Les.
 

absf

Joined Dec 29, 2010
1,968
And this would be the hardware that goes with LesJones' program...

pic with 3 LED.PNG
Allen
 
Last edited by a moderator:

LesJones

Joined Jan 8, 2017
4,511
Hi absf,
That is exactly what I had in mind. I don't think I will have interpreted the OP's requirement correctly as no one would be able to see 333 uS flashes of light but it may give him a guide to present his requirements more clearly.

Les.
 

absf

Joined Dec 29, 2010
1,968
We can't see changes if each LED is lite for 1 mS as well...

The O.P. has to clarify what he meant, before real help can be given.

Allen
 

Thread Starter

changwen

Joined Apr 4, 2017
7
Haha, ya, Les what u say is true, is because our lecturer call us to do using to 40pins.Haha sad case. Thank for you replying :). Nice to meet you and thank for the explanation.

Sorry for my question.
The question actually wants us to do so is, when first switch (RB0) is pressed.
LED 1 will light up at 1s.(001).
LED 1 and LED 2 will light up at 2s(011).
LED 1, LED 2 and LED 3 will light up at 3s.(111).

After, second switch(RB1) is pressed.
LED 3 will light up at 1s(100).
LED 3 and LED 2 will light up at 2s(110).
LED 3, LED 2 and LED 1 will light up at 3s (111).
 
Last edited by a moderator:

djsfantasi

Joined Apr 11, 2010
9,237
So now that you have described your requirements, you would like a program for your PIC to implement it?

Don't have the time to write one, but can show you some tips.

Code:
while (True) {
   int pattern=1;
   int direction=1;
   for(int loop; 0; loop+direction) {
         if(direction>0) {
             pattern=(pattern<<1)||1;
         } elseif {
             Pattern=(pattern>>1)||4;
         }
     }
    direction=-direction;
}
 

Thread Starter

changwen

Joined Apr 4, 2017
7
ok, i try and see but now my problem is i dont know how to use the delay in pic16f877 because my lecturer call us to do the research based on the website but i look through of the website, the information for pic16f877 is quite least.
 

absf

Joined Dec 29, 2010
1,968
sorry, u meant what website
ok, i try and see but now my problem is i dont know how to use the delay in pic16f877 because my lecturer call us to do the research based on the website but i look through of the website, the information for pic16f877 is quite least.
The websites that your lecturer gave you or the ones that you found....
 

absf

Joined Dec 29, 2010
1,968
There is a program call "PicLoops" on the net and you can download and use it freely.

Code:
;PIC Time Delay = 1.0000020 s with Osc = 4.000000 MHz

delay1s    movlw    D'6'
    movwf    CounterC
    movlw    D'24'
    movwf    CounterB
    movlw    D'168'
    movwf    CounterA
loop    decfsz    CounterA,1
    goto    loop
    decfsz    CounterB,1
    goto    loop
    decfsz    CounterC,1
    goto    loop
    retlw
Here is one generated by the program to get a one-second delay. Assuming the 877A is running at 4.00 MHz. 3 variables are defined at the beginning of the program.

Allen
 

Thread Starter

changwen

Joined Apr 4, 2017
7
thank dude! u help me to save a lot of time to think haha

Oh! Actually is our lecturer set for the question and she wants us to do the research based on internet to make this project and she didn't guide us how to do the delay function. So, we do the research on the internet and what i found is blur. And the what my problem now haha .
 
Last edited by a moderator:

Thread Starter

changwen

Joined Apr 4, 2017
7
Code:
; ***** Program Header ***********************************************
LIST    P=16F877a,
#include    <p16f877a.inc>
__CONFIG    0x3D72  ; HS MODE,WDT OFF, BOREN
; ***** Definitions **************************************************
BANK_0     EQU     0x00
BANK_1    EQU     0x80
BANK_2    EQU     0x100
BANK_3     EQU     0x180
PortB        equ    0x06        ; Port B
STATUS    equ    0x03        ; Status Register

; *****  Main Program  ***********************************************
                Org    0

Init     
                movlw   0x0F     ; prepare data for TRISB
                                ; "PIN_B0 to PIN_B3 are
                                ;  input pins and
                                ;  PIN_B4 and PIN_B7 are
                                ;  output pins"
                BANKSEL BANK_1     ; "activate" Bank 1
                Movwf   TRISB      ; copy W to TRISB
                BANKSEL BANK_0     ; "activate" Bank 0      
Start            clrf    PortB      
Set1            btfsc     PortB,0
goto            Set1
LEDIn            clrf    RB5
                clrf    RB6
                clrf    RB7
                movlw   01h
                movwf    RB5
                movlw    02h
                movwf    RB6
                movlw    04h
                movwf    RB7
              
Delay_0          
                decfsz    RB5,f
                goto    $+2
                decfsz    RB6,f
                goto    $+2
                decfsz    RB7,f
                goto    Delay_0
                                ;3 cycles
                goto    $+1
                nop
          

EndTest            goto    EndTest
END
I have finished doing my code but don't know why cannot work need some help to correct it :(
 
Last edited by a moderator:

absf

Joined Dec 29, 2010
1,968
; ***** Program Header ***********************************************
LIST P=16F877a,
#include <p16f877a.inc>
__CONFIG 0x3D72 ; HS MODE,WDT OFF, BOREN
; ***** Definitions **************************************************
BANK_0 EQU 0x00
BANK_1 EQU 0x80
BANK_2 EQU 0x100
BANK_3 EQU 0x180
PortB equ 0x06 ; Port B
STATUS equ 0x03 ; Status Register

; ***** Main Program ***********************************************
Org 0

Init
movlw 0x0F ; prepare data for TRISB
; "PIN_B0 to PIN_B3 are
; input pins and
; PIN_B4 and PIN_B7 are
; output pins"
BANKSEL BANK_1 ; "activate" Bank 1
Movwf TRISB ; copy W to TRISB
BANKSEL BANK_0 ; "activate" Bank 0
Start clrf PortB
Set1 btfsc PortB,0 ; NOT SURE WHAT YOU'RE
goto Set1 ; TRYING TO DO HERE...
LEDIn clrf RB5
clrf RB6
clrf RB7
movlw 01h
movwf RB5
;<- INSERT DELAY HERE
movlw 02h
movwf RB6
;<- INSERT DELAY HERE
movlw 04h
movwf RB7
;<- INSERT DELAY HERE
LOOP BACK TO MAIN PROGRAM.


Delay_0
decfsz RB5,f
goto $+2
decfsz RB6,f
goto $+2
decfsz RB7,f
goto Delay_0
;3 cycles
goto $+1
nop


EndTest goto EndTest
END
This program would not work because it got too many errors.

1. You need to call the delay subroutine from your main program and the subroutine has to end with a "retlw 0" OR just "return".

2. You have to define 3 new variables instead of using RB5 6 7 as variables.

expand the quotes and see more comments in the program.
If you put your program in CODE tags it would be easier to read.
There may be more mistakes but others would chip in after a while.

Allen

p/s If I put in colored text in code tagged program, the color would not work. Any ideas?
 
Last edited:
Top