How can I make my PIC16F84A code shorter?

  • Thread starter Thread starter masteredison
  • Start date Start date
  • Tags Tags
    Led Running
AI Thread Summary
The discussion focuses on programming the PIC16F84A microcontroller to create a traffic light simulation with three outputs. The user seeks to simplify their code, which currently relies on multiple calls to a delay function, making it lengthy and cumbersome. Suggestions emphasize understanding the internal workings of the PIC, including how to manipulate registers and control pins effectively. It is recommended to start with basic tutorials to grasp the fundamentals before attempting to compress the code. Ultimately, the goal is to achieve a more efficient program that maintains the desired functionality without excessive repetition.
masteredison
Messages
2
Reaction score
0
Hello,

can someone help me in programming our PIC16F84A, we need to make a program that will show only a three output like a traffic light,..but I am not quite familiar in programming this. I examined a 7 output LED program but i can't figure out what to changed.. I also want to changed the delay for each LED and then go back to the start of the program...Thnx





;****set up constans and variables****
INCLUDE "P16F84.INC" ;look up file for MPASM
LIST P=16F84
ERRORLEVEL -302 ;SUPPRESS BANK SELECTION MESSAGES
__config _RC_OSC&_PWRTE_ON & _WDT_OFF ;RC oscillator, power on timer
;and watchdog timer off

status equ h'03' ;status register
trisb equ h'86' ;set up register for port B
trisa equ h'85' ;set up register for port B
count1 equ h'0e' ;general purpose register
count2 equ h'0f' ;general purpose register
leds equ h'06' ;portB is 7seg display content
porta equ h'05' ;portA is not used

;****set up portA and portB****
bsf status,5 ;switch to bank1 to set up ports
movlw h'00'
movwf trisb ;portB is all output
movlw h'00'
movwf trisa ;portA is all output
bcf status,5 ;switch back to bank0 to use ports

;****main program****
start movlw b'00000001' ;load pattern into W
movwf leds ;load W into portB (switch on rightmost LED)
call delay ;wait for some time
movlw b'00000010' ;new pattern
movwf leds
call delay ;wait
movlw b'00000100' ;new pattern
movwf leds ;
call delay ;wait
movlw b'00001000' ;new pattern
movwf leds ;
call delay ;wait
goto start ;do it all over again
;****delay subroutine****
;****delay = wait (delay count value 1) x (delay count value 2) cycles****
delay movlw h'EE' ;setup delay count value 1
movwf count1 ;count1 is slow counter
loop1 decfsz count1,1 ;
goto label ;still > 0
return ;count1 < 0 so get out of the delay loop
label movlw h'EE' ;setup delay count value 2
movwf count2 ;count2 is fast counter inside count1 loop
loop2 decfsz count2,1 ;
goto loop2 ;still > 0 so stay in loop2
goto loop1 ;count1 < 0 go to loop1
end;
 
Physics news on Phys.org
Instead of trying to jump in with both feet and back-engineer an existing piece of code to meet your needs, which can be both daunting and confusing, you should really start at the beginning and understand how a PIC works internally, how pins are controlled and how the registers work. Start with this tutorial, which I found very instructive when I was first getting into them:

http://www.mstracey.btinternet.co.uk/pictutorial/picmain.htm

The important thing is to understand the internal workings of the uC and then you can use whatever language you feel comfortable with. There are assemblers and compilers for languages such as C and BASIC for the whole range of PIC chips. Starting off with assemblers is a good idea, however, because it forces you to get down to the hardware level and think about your logic and what it needs to do.
 
Last edited by a moderator:
Hello,

Im now able to make my own code for the Pic16f84A, but I'd use lots of call delay in it...is there a code that will make it short or more compress..

Thnx,.

this is the code:

processor 16f84a
include <p16f84a.inc>

__config _XT_OSC & _WDT_OFF & _PWRTE_ON

; Declare variables at 2 memory locations

J equ H'1F' ; J = address hex 1F
K equ H'1E' ; K = address hex 1E



org 0 ; start at address 0


; Set port B as output and initialize it

bsf STATUS,RP0 ; Select bank 1
movlw B'00000000' ; w := 00000000 binary
movwf TRISB ; port B data direction := w
movlw B'11111111' ; w := 00000000 binary
movwf TRISA ; port A data direction := w
bcf STATUS,RP0 ; Select Bank 0



main: movlw h'01'
movwf PORTB
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
movlw h'04'
movwf PORTB
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
movlw h'02'
movwf PORTB
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay
call delay

goto main

delay: movlw D'100'
movwf J
jlp: movwf K
klp: decfsz K,f
goto klp
decfsz J,f
goto jlp
return
end
 
Back
Top