1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

00-99 COUNT UP & DOWN USING PIC16f887a

Discussion in 'Microcontrollers' started by AnneMaj, Aug 27, 2013.

  1. AnneMaj

    AnneMaj New Member

    Joined:
    Aug 26, 2013
    Messages:
    23
    Likes:
    0
    Location:
    philippines
    i really need help here..

    i need an assembly program for my project..

    Make a program that will count up from 00->99 and can count down from 99->00
    Requirements:
    a. one push button for count up
    b. one push button for count down
    c. 2 7-segment display (common cathode)
    d. use multiplexing tech. (or use only PORTB as output)
    e. you may use PORT A,C,D or E as input.

    ex.
    the display is 00, when i press the button for count up once, it will change to 01. and by clicking again will change to 02 and so on.
    ex.
    the display is 09, when i press the button for count down, it will change to 08.


    help please..
     
  2. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,807
    Likes:
    135
    Location:
    morristown,tn
    Last edited: Aug 27, 2013
  3. AnneMaj

    AnneMaj New Member

    Joined:
    Aug 26, 2013
    Messages:
    23
    Likes:
    0
    Location:
    philippines
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. AnneMaj

    AnneMaj New Member

    Joined:
    Aug 26, 2013
    Messages:
    23
    Likes:
    0
    Location:
    philippines

    i couldn't find any counter that uses 16f877a.. :(
     
  6. AnneMaj

    AnneMaj New Member

    Joined:
    Aug 26, 2013
    Messages:
    23
    Likes:
    0
    Location:
    philippines
  7. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,807
    Likes:
    135
    Location:
    morristown,tn
    You have all you need to make one on the page I posted all you have to do is change the code Mike posted to work with your chip.

    Look at post4 its for a 16F88 it's still asm and it will work with some changes on your chip
    Now is your chip really a 16F887 or a 16F877a there is no 16F887a.
     
  8. AnneMaj

    AnneMaj New Member

    Joined:
    Aug 26, 2013
    Messages:
    23
    Likes:
    0
    Location:
    philippines


    i need help.. can you help me with these?

    00-99 COUNT UP & DOWN USING PIC16f877a

    Make a program that will count up from 00->99 and can count down from 99->00
    Requirements:
    a. one push button for count up
    b. one push button for count down
    c. 2 7-segment display (common cathode)
    d. use multiplexing tech. (or use only PORTB as output)
    e. you may use PORT A,C,D or E as input.

    ex.
    the display is 00, when i press the button for count up once, it will change to 01. and by clicking again will change to 02 and so on.
    ex.
    the display is 09, when i press the button for count down, it will change to 08.


    help please..
     
  9. AnneMaj

    AnneMaj New Member

    Joined:
    Aug 26, 2013
    Messages:
    23
    Likes:
    0
    Location:
    philippines

    16f877a..

    what do you mean on the changes on my chip?..

    won't i need to change the program?..
     
  10. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,807
    Likes:
    135
    Location:
    morristown,tn
    Lol all you had to do is this and test it.
    Look it over real good
    Code (text):
    ;******************************************************************
    ;  2-Digit Up/Dn Counter, Isochronous Loop Example                *
    ;******************************************************************
    ; change the chip name and configure
          list      p=16f877A   ; list directive to define processor
        #include    <p16f877A.inc>  ; processor specific variable definitions
       
        __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF

     
    ones    equ     0x20            ; 0..9
    tens    equ     0x21            ; 0..9
    number  equ     0x22            ; 00..99
    swlatch equ     0x23            ; switch state latch variable
    DelayHi equ     0x24            ; DelayCy() subsystem variable
     
    #define DnSw    3               ; RA3
     
    ;******************************************************************
    ;
    ;  K8LH DelayCy() subsystem macro generates four instructions
    ;
            radix   dec
    clock   equ     8               ; clock frequency in Megahertz
    usecs   equ     clock/4         ; cycles/microsecond multiplier
    msecs   equ     usecs*1000      ; cycles/millisecond multiplier
     
    DelayCy macro   delay           ; 11..327690 cycle range
            movlw   high((delay-11)/5)+1
            movwf   DelayHi
            movlw   low ((delay-11)/5)
            call    uDelay-((delay-11)%5)
            endm
     
    ;******************************************************************
    ;
    ;  init hardware and program variables
    ;
            org     0x000
    Init
    ; change the ADC stuff
            bsf     STATUS, RP0     ; Select Bank 1
            movlw   0x06            ; Configure all pins
            movwf   ADCON1          ; as digital inputs
    ;
            movlw   b'00001100'     ;                                 |B1
            movwf   TRISA           ; RA3-RA2 inputs, others outputs  |B1
            clrf    TRISB           ; portb all outputs               |B1
            bcf     STATUS,RP0      ; bank 0                          |B0
            clrf    PORTB           ; clear portb output latches      |B0
            movlw   b'00000001'     ; digit select bits (RA1-RA0)     |B0
            movwf   PORTA           ; select the 'ones' display       |B0
            clrf    swlatch         ; clear switch state latch        |B0
            clrf    ones            ; clear 'ones'                    |B0
            clrf    tens            ; clear 'tens'                    |B0
            clrf    number          ; number = 00                     |B0
    ;
    ;  isochronous 8 msec main program loop (62.5 Hz refresh rate)
    ;
    Main    clrf    PORTB           ; blank the display               |B0
            movf    PORTA,W         ;                                 |B0
            xorlw   b'00000011'     ; flip digit select bits          |B0
            movwf   PORTA           ;                                 |B0
            movf    tens,W          ; WREG = tens, 0..9               |B0
            btfss   PORTA,1         ; display tens? yes, skip, else   |B0
            movf    ones,W          ; WREG = ones, 0..9               |B0
            call    segtbl          ; get segment data                |B0
            movwf   PORTB           ; display new digit               |B0
    TstSw   comf    PORTA,W         ; sample active low switches      |B0
            andlw   b'00001100'     ; on RA3 and RA2 pins             |B0
            xorwf   swlatch,W       ; changes (press or release)      |B0
            xorwf   swlatch,F       ; update switch state latch       |B0
            andwf   swlatch,W       ; filter out "new release" bits   |B0
            bnz     Bump            ; branch on a "new press", else   |B0
            DelayCy(8*msecs-23)     ; precise 8 msec loop timing      |B0
            goto    Main            ;                                 |B0
    ;
    ;  bump 'number' up or down with limit checking
    ;
    Bump    andlw   1<<DnSw         ; the "Dn" switch?                |B0
            skpz                    ; no, skip (WREG=0), else         |B0
            movlw   -2              ; WREG = -2 (dn)                  |B0
            addlw   1               ; WREG = 1 (up) or -1 (dn)        |B0
            addwf   number,F        ; number++ or number--            |B0
            movf    number,W        ; WREG = number = -1..100         |B0
            xorlw   100             ; test upper limit                |B0
            skpnz                   ; upper limit? no, skip, else     |B0
            decf    number,F        ; reset to 99                     |B0
            btfsc   number,7        ; lower limit? no, skip, else     |B0
            incf    number,F        ; reset to 00                     |B0
            movf    number,W        ; WREG = number = 00..99          |B0
    ;
    ;  setup 'tens' and 'ones' for next loop
    ;
            clrf    tens            ; isochronous bin2bcd routine     |B0
            addlw   -80             ; W = W - 80                      |B0
            rlf     tens,F          ; shift in 2^3*10 bit             |B0
            btfss   tens,0          ; borrow? no, skip, else          |B0
            addlw   80              ; W = W + 80                      |B0
            addlw   -40             ; W = W - 40                      |B0
            rlf     tens,F          ; shift in 2^2*10 bit             |B0
            btfss   tens,0          ; borrow? no, skip, else          |B0
            addlw   40              ; W = W + 40                      |B0
            addlw   -20             ; W = W - 20                      |B0
            rlf     tens,F          ; shift in 2^1*10 bit             |B0
            btfss   tens,0          ; borrow? no, skip, else          |B0
            addlw   20              ; W = W + 20                      |B0
            addlw   -10             ; W = W - 10, now W = "ones"      |B0
            rlf     tens,F          ; shift in 2^0*10 bit             |B0
            btfss   tens,0          ; borrow? no, skip, else          |B0
            addlw   10              ; W = W + 10, now W = "ones"      |B0
            movwf   ones            ; save "ones"                     |B0
            DelayCy(8*msecs-54)     ; precise 8 msec loop timing      |B0
            goto    Main            ;                                 |B0
    ;
    ;  segment data table (caveat, non-boundary tolerant)
    ;
    segtbl
            addwf   PCL,F           ;                                 |B0
            dt      b'00111111'     ; "0"   -|-|F|E|D|C|B|A           |B0
            dt      b'00000110'     ; "1"   -|-|-|-|-|C|B|-           |B0
            dt      b'01011011'     ; "2"   -|G|-|E|D|-|B|A           |B0
            dt      b'01001111'     ; "3"   -|G|-|-|D|C|B|A           |B0
            dt      b'01100110'     ; "4"   -|G|F|-|-|C|B|-           |B0
            dt      b'01101101'     ; "5"   -|G|F|-|D|C|-|A           |B0
            dt      b'01111101'     ; "6"   -|G|F|E|D|C|-|A           |B0
            dt      b'00000111'     ; "7"   -|-|-|-|-|C|B|A           |B0
            dt      b'01111111'     ; "8"   -|G|F|E|D|C|B|A           |B0
            dt      b'01101111'     ; "9"   -|G|F|-|D|C|B|A           |B0
    ;
    ;  K8LH DelayCy() subsystem 16-bit timing subroutine
    ;
            nop                     ; entry for (delay-11)%5 == 4     |B0
            nop                     ; entry for (delay-11)%5 == 3     |B0
            nop                     ; entry for (delay-11)%5 == 2     |B0
            nop                     ; entry for (delay-11)%5 == 1     |B0
    uDelay  addlw   -1              ; subtract "loop" cycle time      |B0
            skpc                    ; borrow? no, skip, else          |B0
            decfsz  DelayHi,F       ; done?  yes, skip, else          |B0
            goto    uDelay          ; do another loop                 |B0
            return                  ;                                 |B0
     
            end
    Wire it up to the same pins as the picture
    2 digit up-down.PNG
    This is a 16F88 but your chip has the same ports PORTA and PORTB
    This Is Mike K8LH Work I just reposted it to help you out and changed code to work with your chip.
     
    Last edited: Aug 27, 2013
  11. AnneMaj

    AnneMaj New Member

    Joined:
    Aug 26, 2013
    Messages:
    23
    Likes:
    0
    Location:
    philippines
    thank you very much for helping.. i'll try this.. tnx..
     
  12. AnneMaj

    AnneMaj New Member

    Joined:
    Aug 26, 2013
    Messages:
    23
    Likes:
    0
    Location:
    philippines
    it's not working.. :(
     
  13. bryan1

    bryan1 Well-Known Member

    Joined:
    Aug 4, 2004
    Messages:
    1,679
    Likes:
    46
    Location:
    Adelaide Hills Sth Oz
    Hi AnnaMaj,
    Post the changes you have made to the code then we can see where you have gone wrong.
     
  14. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,897
    Likes:
    502
    Location:
    Cleveland, OH, USA
    ONLINE
    Can you describe in a little more detail what you have done to determine that?

    Here are some thoughts:

    1) Is that conclusion based on a breadboard circuit or simulation?
    2) What simulator are you using?
    3) Bypass the delays (comment out) and step through the rest of the program. What happens? Post the code from your simulation.
    4) If you are not using a simulator, such as MPLAB SIM, it is difficult to troubleshoot code.
    5) If breadboard, is anything happening? That is, is it totally dead, or are some led's coming on, but they are not quite right?
    6) Is the code you posted a copy and paste of your actual Assembly code or simply copied from what's been posted here? In other words, it is crucial to determine whether some unintentional changes have slipped into the code you are using.
    7) I haven't had breakfast yet, but I don't see the HS crystal resonator in your schematic. See Chapter 14.2 of the datasheet.

    The above list is not exhaustive.

    John
     
    Last edited: Aug 29, 2013
  15. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi AnnaMaj

    It works for me in the Oshonsoft simulator.:rolleyes:

    E
     
  16. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,807
    Likes:
    135
    Location:
    morristown,tn
    She or he may not be using the right crystal mikes code
    Was for a 16f88 at 8 MHz
    I changed the code to run on a 16f877a you need a crystal on that one
    May of left crystal out but I'm sure the code works
     
    Last edited: Aug 29, 2013
  17. AnneMaj

    AnneMaj New Member

    Joined:
    Aug 26, 2013
    Messages:
    23
    Likes:
    0
    Location:
    philippines

    this is based on simulation (ISIS)..
    sorry cause im new in this and i still dont know much.. thats y i need help..
    i dont still dont understand much.. :(
     
  18. AnneMaj

    AnneMaj New Member

    Joined:
    Aug 26, 2013
    Messages:
    23
    Likes:
    0
    Location:
    philippines

    do i still need to change the codes?
     
  19. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,807
    Likes:
    135
    Location:
    morristown,tn
    It works on hardware with a 8mhz crystal. That is how i tried it dont like ISIS.
     
  20. AnneMaj

    AnneMaj New Member

    Joined:
    Aug 26, 2013
    Messages:
    23
    Likes:
    0
    Location:
    philippines
    i use mplab ide v8.92 / v8.76 to write the codes? is that ok?
    my simulator is ISIS.
     
  21. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,807
    Likes:
    135
    Location:
    morristown,tn
    Yes that's ok
     

Share This Page