Author Topic: Microcontroller 2x(7segment) counter issue  (Read 637 times)

0 Members and 2 Guests are viewing this topic.

Offline yalectTopic starter

  • Regular Contributor
  • *
  • Posts: 163
Microcontroller 2x(7segment) counter issue
« on: November 24, 2026, 12:38:56 pm »
Hi,
I have this program of the Microcontroller PIC16F84a but I think there was a probelm with its working, it's a timer based on TMR0 Counter on RA4 Displayed on 2-Digit Multiplexed 7-Segment Display (00-99) when I try to simulate it, it's not giving a good result:  ;====================================================================
; Processor: PIC16F84A
; Function:  TMR0 Counter on RA4 (Rising Edge, 1:1 rate via PSA=1)
;            Displayed on 2-Digit Multiplexed 7-Segment Display (00-99)
;====================================================================
        LIST    P=16F84A
        #include <P16F84A.inc>

        __CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_ON & _CP_OFF

;--------------------------------------------------------------------
; General Purpose Registers (Bank 0)
;--------------------------------------------------------------------
        CBLOCK  0x0C
            Tens_Digit          ; Tens place (0-9)
            Ones_Digit          ; Ones place (0-9)
            Temp_Count          ; Working copy for decimal conversion
            Delay_Cnt1          ; Multiplex / debounce delay counter
            Delay_Cnt2
        ENDC

;--------------------------------------------------------------------
; Reset Vector
;--------------------------------------------------------------------
        ORG     0x0000
        GOTO    Main

;--------------------------------------------------------------------
; Lookup Table (Aligned within first 256-word block for PCL safety)
;--------------------------------------------------------------------
        ORG     0x0100
Get_Seg_Pattern:
        ANDLW   0x0F
        ADDWF   PCL, F
        RETLW   b'00111111'     ; 0
        RETLW   b'00000110'     ; 1
        RETLW   b'01011011'     ; 2
        RETLW   b'01001111'     ; 3
        RETLW   b'01100110'     ; 4
        RETLW   b'01101101'     ; 5
        RETLW   b'01111101'     ; 6
        RETLW   b'00000111'     ; 7
        RETLW   b'01111111'     ; 8
        RETLW   b'01101111'     ; 9

;--------------------------------------------------------------------
; Main Program
;--------------------------------------------------------------------
Main:
        BANKSEL TRISA
        MOVLW   b'00010000'     ; RA4 = Input (T0CKI), RA0:RA1 = Outputs
        MOVWF   TRISA
        CLRF    TRISB           ; PORTB (RB0-RB6) = Outputs for 7-segment

        ; OPTION_REG:
        ; bit 7 (NOT_RBPU) = 1 : Disable PORTB weak pull-ups
        ; bit 5 (T0CS)     = 1 : Counter mode on RA4/T0CKI
        ; bit 4 (T0SE)     = 0 : Increment on rising edge
        ; bit 3 (PSA)      = 1 : Prescaler assigned to WDT -> TMR0 is 1:1
        ; bits 2:0         = 000
        MOVLW   b'10101000'
        MOVWF   OPTION_REG

        BANKSEL PORTA
        CLRF    PORTA
        CLRF    PORTB
        CLRF    TMR0

Loop:
        ; Keep 2-digit counter within 00-99 (roll over at 100)
        MOVF    TMR0, W
        SUBLW   .100            ; W = 100 - TMR0
        BTFSS   STATUS, C       ; Skip if TMR0 <= 100
        CLRF    TMR0

        ; Convert TMR0 (0-99) into Tens_Digit and Ones_Digit
        CALL    Split_Digits

        ; --- Display Tens Digit on RA0 ---
        BCF     PORTA, 1        ; Turn off Ones digit first (avoid ghosting)
        CLRF    PORTB
        MOVF    Tens_Digit, W
        CALL    Get_Seg_Pattern
        MOVWF   PORTB
        BSF     PORTA, 0        ; Enable Tens digit
        CALL    Mux_Delay

        ; --- Display Ones Digit on RA1 ---
        BCF     PORTA, 0        ; Turn off Tens digit first (avoid ghosting)
        CLRF    PORTB
        MOVF    Ones_Digit, W
        CALL    Get_Seg_Pattern
        MOVWF   PORTB
        BSF     PORTA, 1        ; Enable Ones digit
        CALL    Mux_Delay

        GOTO    Loop

;--------------------------------------------------------------------
; Split_Digits: Converts W (0-99) into Tens_Digit and Ones_Digit
;--------------------------------------------------------------------
Split_Digits:
        MOVF    TMR0, W
        MOVWF   Temp_Count
        CLRF    Tens_Digit

Div10_Loop:
        MOVLW   .10
        SUBWF   Temp_Count, W   ; W = Temp_Count - 10
        BTFSS   STATUS, C       ; Carry = 1 if Temp_Count >= 10
        GOTO    Div10_Done
        MOVWF   Temp_Count      ; Temp_Count = Temp_Count - 10
        INCF    Tens_Digit, F
        GOTO    Div10_Loop

Div10_Done:
        MOVF    Temp_Count, W
        MOVWF   Ones_Digit
        RETURN

;--------------------------------------------------------------------
; Mux_Delay: Short delay (~3-5 ms at 4 MHz) for flicker-free display
;--------------------------------------------------------------------
Mux_Delay:
        MOVLW   .5
        MOVWF   Delay_Cnt1
Outer_Dly:
        MOVLW   .250
        MOVWF   Delay_Cnt2
Inner_Dly:
        NOP
        DECFSZ  Delay_Cnt2, F
        GOTO    Inner_Dly
        DECFSZ  Delay_Cnt1, F
        GOTO    Outer_Dly
        RETURN

        END

-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
- Could you please let me know your comments and notes and corrections or maybe feedback that you see, they will make the counter work or fonction stable?

 Thank you.
« Last Edit: November 25, 2026, 12:44:00 pm by yalect »
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4674
  • Country: us
Re: Microcontroller 2x(7segment) counter issue
« Reply #1 on: November 25, 2026, 10:50:07 pm »
“Not good result” how, exactly?

 

Offline Renate

  • Super Contributor
  • ***
  • Posts: 1827
  • Country: de
    • Renate's Android Page
Re: Microcontroller 2x(7segment) counter issue
« Reply #2 on: Yesterday at 03:26:21 am »
I don't know PIC from a hole in the ground, but you should fix Get_Seg_Pattern.
If for some reason you get an 0xa to 0xf in there it will run off the end and into Main.
You should add the the hex A-F or at least fudge values for it that you can recognize it visually.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17853
  • Country: fr
Re: Microcontroller 2x(7segment) counter issue
« Reply #3 on: Yesterday at 04:50:17 am »
One probable issue is that it doesn't count to 99, but to 100 and so there is likely one step that appears skipped when rolling over.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->