How can I make my PIC16F84A code shorter?

  • Thread starter masteredison
  • Start date
  • Tags
    Led Running
In summary, the conversation discusses programming a PIC16F84A microcontroller to create a traffic light display using 7 output LEDs. The speaker expresses difficulty in understanding the existing code and asks for a shorter or more compressed version. They are advised to start with the basics of how the PIC works and use assemblers to get a better understanding of the hardware level. The conversation also includes a sample code for the traffic light display using call delay functions.
  • #1
masteredison
2
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
  • #2
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:
  • #3
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
 

1. How does a running LED circuit using PIC16F84A work?

The running LED circuit uses a microcontroller, specifically the PIC16F84A, to control the sequence of LEDs turning on and off. The microcontroller sends signals to a series of transistors, which in turn control the flow of current to the LEDs. By changing the timing of the signals, the LEDs appear to "run" or move along a line.

2. What components are needed to build a running LED circuit using PIC16F84A?

To build a running LED circuit, you will need a PIC16F84A microcontroller, a power supply, resistors, transistors, LEDs, and connecting wires. Depending on the specific design, you may also need a breadboard or PCB to connect the components.

3. Can I customize the LED sequence in a running LED circuit using PIC16F84A?

Yes, the sequence of LEDs can be customized by programming the microcontroller. The PIC16F84A can be programmed using a variety of coding languages, including C and assembly language. By changing the code, you can alter the timing and pattern of the LED sequence.

4. Are there any limitations to the number of LEDs that can be used in a running LED circuit using PIC16F84A?

The number of LEDs that can be used in a running LED circuit will depend on the specific design and the capabilities of the microcontroller. The PIC16F84A has 13 available pins for output, so you can control up to 13 LEDs. If you need more than 13, you can use additional microcontrollers or multiplexing techniques.

5. Can a PIC16F84A be used for other types of LED circuits besides a running LED circuit?

Yes, the PIC16F84A can be used for a variety of LED circuits, including blinking, fading, and color-changing LEDs. The microcontroller is versatile and can be programmed to control a wide range of LED patterns and effects.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
Replies
9
Views
2K
  • Electrical Engineering
Replies
10
Views
3K
  • Electrical Engineering
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
Replies
6
Views
3K
Back
Top