• 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.

Need Help With 16F676 Counter

Status
Not open for further replies.
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:
        #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:

be80be

Well-Known Member
Code tags help alot
Code:
#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
 
Code tags help alot
Code:
#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
Thanks, I couldn't remember how to do it.
 

jpanhalt

Well-Known Member
Most Helpful Member
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
 

Pommie

Well-Known Member
Most Helpful Member
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.
 

Pommie

Well-Known Member
Most Helpful Member
Yes, all are I/O but RA3 is Input only. See page 21 of datasheet.
porta.png

Sorry,

Mike.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top