1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Running LED using PIC16F84A

  1. Aug 13, 2009 #1
    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 Im not quite familiar in programming this. I examined a 7 output LED program but i cant 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;
     
  2. jcsd
  3. Aug 13, 2009 #2

    negitron

    User Avatar
    Science Advisor

    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: Apr 24, 2017
  4. Aug 19, 2009 #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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Running LED using PIC16F84A
  1. Silicon in LEDs (Replies: 1)

  2. LED Animation >< (Replies: 1)

  3. LED/Photodiode advice (Replies: 9)

Loading...