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

Need Help With 16F676 Counter

Discussion in 'Microcontrollers' started by fastback86, Jul 16, 2017.

  1. fastback86

    fastback86 Member

    Joined:
    Apr 13, 2008
    Messages:
    89
    Likes:
    0
    I'm working on a project that will eventually be a 3 digit counter but I have a small issue I believe is in the code I wrote. The circuit is a PIC16F676 driving 3 common anode multiplexed 7 segment displays with the anodes driven by MOSFETs. Just to test things out I wrote some code just to alternate a "1" between the ONES place and TENS place. So far that works ok but the HUNDREDS digit wants to turn on randomly. It seems the gate of the MOSFET controlling that digit is "floating" even though it is connected to pin 4 (RA3/MCLR/Vpp) of the PIC. I have put a logic probe directly on pin 4 and verified a state that's neither H nor L. Have I done something wrong in the code or is it that I just can't wire the circuit like I have it?

    Sorry for the messy post, the "insert code" box doesn't work for some reason.

    Code:
    Code (text):

     
            #include <p16f676.inc>
           List   P=16F676
       
           __CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT

    COUNT1  equ 20h           ;First counter for our delay loops
    COUNT2    equ 21h           ;Second counter for our delay loops
    COUNT3   equ 22h           ;Third counter for delay loop.
                           ;20h, 21h, 22h, are addresses in the general register
                           ;that are used to store a variable

    Start
           BSF       STATUS, RP0                   ; Bank 1
           CLRF   TRISA                       ; PortA all output
           CLRF   TRISC                       ; PortC all output
           BCF       STATUS, RP0                   ; Bank 0
           movlw   b'00111111'       ;  
           movwf   PORTC           ;set all of PORTC high turning segments off
           movlw   b'00101101'  
           movwf   PORTA           ;set RA5 high to turn segment g off and
                                   ;set RA0,2, and 3 H to turn digit off  

    Main
           movlw   b'00010111'       ;set RA3 and RA5 low to turn on segments b and c
           movwf   PORTC
           movlw   b'00101001'       ;select digit 1 (LSD)
           movwf   PORTA
           call   Delay
           movlw   b'00101100'       ;select digit 1 (LSD)
           movwf   PORTA
           call   Delay
           goto   Main

       
    Delay
           movlw   D'200'           ;load decimal 200 into w
           movwf   COUNT1           ;move 200 to f then to variable COUNT1
           movlw   D'200'           ;COUNT1=200, COUNT2=200, COUNT3=4 gives a nice flash rate
           movwf   COUNT2
           movlw   D'4'
           movwf   COUNT3
    Loop1      decfsz   COUNT1,1     ;decrement variable COUNT1 by one
               goto     Loop1        ;goto Loop1 if COUNT1 is not zero, skip this step if it is zero
               decfsz   COUNT2,1     ;next decremented counter...
               goto     Loop1        
             decfsz   COUNT3,1     ;next decremented counter...
               goto     Loop1
             return
           end


    FullSizeRender-2.jpg
     
    Last edited: Jul 16, 2017
  2. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,830
    Likes:
    138
    Location:
    morristown,tn
    ONLINE
    Code tags help alot
    Code (text):

    #include <p16f676.inc>
    List P=16F676

    __CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT

    COUNT1 equ 20h ;First counter for our delay loops
    COUNT2 equ 21h ;Second counter for our delay loops
    COUNT3 equ 22h ;Third counter for delay loop.
    ;20h, 21h, 22h, are addresses in the general register
    ;that are used to store a variable

    Start
    BSF STATUS, RP0 ; Bank 1
    CLRF TRISA ; PortA all output
    CLRF TRISC ; PortC all output
    BCF STATUS, RP0 ; Bank 0
    movlw b'00111111' ;
    movwf PORTC ;set all of PORTC high turning segments off
    movlw b'00101101'
    movwf PORTA ;set RA5 high to turn segment g off and
    ;set RA0,2, and 3 H to turn digit off

    Main
    movlw b'00010111' ;set RA3 and RA5 low to turn on segments b and c
    movwf PORTC
    movlw b'00101001' ;select digit 1 (LSD)
    movwf PORTA
    call Delay
    movlw b'00101100' ;select digit 1 (LSD)
    movwf PORTA
    call Delay
    goto Main


    Delay
    movlw D'200' ;load decimal 200 into w
    movwf COUNT1 ;move 200 to f then to variable COUNT1
    movlw D'200' ;COUNT1=200, COUNT2=200, COUNT3=4 gives a nice flash rate
    movwf COUNT2
    movlw D'4'
    movwf COUNT3
    Loop1 decfsz COUNT1,1 ;decrement variable COUNT1 by one
    goto Loop1 ;goto Loop1 if COUNT1 is not zero, skip this step if it is zero
    decfsz COUNT2,1 ;next decremented counter...
    goto Loop1
    decfsz COUNT3,1 ;next decremented counter...
    goto Loop1
    return
    end
     
     
  3. fastback86

    fastback86 Member

    Joined:
    Apr 13, 2008
    Messages:
    89
    Likes:
    0
    Thanks, I couldn't remember how to do it.
     
  4. dave

    Dave New Member

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


     
  5. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,986
    Likes:
    511
    Location:
    Cleveland, OH, USA
    ONLINE

    If you use [code=MPASM] code [/code] it works even better in "technicolor."

    Not sure of the problem. Will have to look at it in the morning.

    John
     
  6. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,085
    Likes:
    326
    Location:
    Brisbane Australia
    ONLINE
    Unfortunately RA3 is input only. Some of the newer chips are pic compatible and RA3 is I/O.
    Edit, Actually, I'm not sure if even the newer chips have RA3 as output.

    Mike.
     
  7. fastback86

    fastback86 Member

    Joined:
    Apr 13, 2008
    Messages:
    89
    Likes:
    0
    Really? Data sheet says "12 I/O Pins with Individual Direction Control" on page 3 under "Peripheral Features."

    http://ww1.microchip.com/downloads/en/DeviceDoc/40039F.pdf
     
  8. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,085
    Likes:
    326
    Location:
    Brisbane Australia
    ONLINE
    Yes, all are I/O but RA3 is Input only. See page 21 of datasheet.
    porta.png

    Sorry,

    Mike.
     
  9. fastback86

    fastback86 Member

    Joined:
    Apr 13, 2008
    Messages:
    89
    Likes:
    0
    Well that's a pi$$er! That's the answer then, thanks for that. Maybe they should update the info on the other pages.
     

Share This Page