Count 0-9 on a seven-segment display

Drive a seven-segment display from PORTB on a PIC16F84A, using a RETLW lookup table to turn a digit into segment patterns.

  • PIC16F84A
  • Beginner
  • Watch the Seven Segment Display widget

Open the example in the simulator

Wiring

The Seven Segment Display widget is wired to PORTB: RB0 drives segment A, RB1 segment B, and so on up to RB6 for segment G; RB7 is the decimal point. A 1 on a pin lights its segment. The program makes the whole port an output with clrf TRISB in bank 1.

From digit to segments: a lookup table

Each digit needs a different set of segments, so the program keeps a table of eight-bit patterns. display_digit loads the digit (0-9) into W and adds it to PCL, the low byte of the program counter, with ADDWF. That jumps W instructions into a list of RETLW lines, and the one it lands on returns with the pattern in W.

For example, digit 2 needs segments A, B, D, E and G: b'01011011'.

The DT directive writes the same table more compactly: dt 0x3F, 0x06, 0x5B, ... assembles to one RETLW per value.

Counting from 0 to 9

After showing a digit the program increments digit_counter and checks whether it reached 10: SUBWF computes counter - 10 into W, and if the result is zero (Z set) CLRF starts again at 0. BTFSC skips the CLRF in every other case.

The one-second delay

Three nested DECFSZ loops of 8, 250 and 166 passes, at 3 cycles each, add up to about 1,000,000 instruction cycles: 1 s at 4 MHz.

A trap with computed jumps

ADDWF PCL, F only changes the low eight bits of the program counter; the high bits come from PCLATH. This table sits near the start of memory, so it works. A table placed across a 256-word boundary, or above address 0xFF with PCLATH still 0, jumps to the wrong place.

Try it

  1. Open the example, assemble and run it. The display counts 0 to 9 once a second.
  2. Expand Pin mapping under the display to see which PORTB bit drives which segment.
  3. Add a pattern for the letter A (b'01110111') as an eleventh table entry and change the limit from 10 to 11.

The complete program

This is examples/seven-segment-test.asm, the same file the simulator loads.

; Seven Segment Display Test
; Displays digits 0-9 on a seven-segment display connected to PORTB
; RB0-RB6 control segments A-G, RB7 controls decimal point

    list p=16f84a
    include <p16f84a.inc>
    
    ; Configuration bits
    __CONFIG _FOSC_HS & _WDTE_OFF & _PWRTE_ON & _CP_OFF
    
    ; Variables
    cblock 0x20
        digit_counter
        delay_counter1
        delay_counter2
        delay_counter3
    endc
    
    org 0x00
    goto start
    
    org 0x04
    ; Interrupt service routine (not used in this example)
    retfie
    
start:
    ; Configure PORTB as output
    bsf STATUS, RP0      ; Select Bank 1
    clrf TRISB           ; Set all PORTB pins as outputs
    bcf STATUS, RP0      ; Select Bank 0
    
    ; Initialize variables
    clrf digit_counter
    clrf PORTB           ; Clear PORTB initially
    
main_loop:
    ; Display current digit
    call display_digit   ; Returns pattern in W register
    movwf PORTB          ; Write the pattern to PORTB
    
    ; Increment digit counter (0-9)
    incf digit_counter, f
    movlw 10
    subwf digit_counter, w
    btfsc STATUS, Z
    clrf digit_counter   ; Reset to 0 when reaching 10
    
    ; Delay between digits
    call delay_1s
    
    goto main_loop
    
display_digit:
    ; Load digit counter and jump to appropriate pattern
    movf digit_counter, w
    addwf PCL, f         ; Jump to digit pattern
    
    ; Digit patterns (PORTB values for 0-9) using new b'xxxxxxxx' format
    retlw b'00111111'    ; 0: segments A,B,C,D,E,F
    retlw b'00000110'    ; 1: segments B,C
    retlw b'01011011'    ; 2: segments A,B,G,E,D
    retlw b'01001111'    ; 3: segments A,B,G,C,D
    retlw b'01100110'    ; 4: segments F,G,B,C
    retlw b'01101101'    ; 5: segments A,F,G,C,D
    retlw b'01111101'    ; 6: segments A,F,G,E,D,C
    retlw b'00000111'    ; 7: segments A,B,C
    retlw b'01111111'    ; 8: all segments
    retlw b'01101111'    ; 9: segments A,B,C,D,F,G
    
delay_1s:
    ; About 1 s at 4 MHz: 3 cycles per inner pass, 8 x 250 x 166 passes
    ; is about 1,000,000 instruction cycles of 1 us
    movlw 8
    movwf delay_counter1
delay_a:
    movlw 250
    movwf delay_counter2
delay_b:
    movlw 166
    movwf delay_counter3
delay_inner:
    decfsz delay_counter3, f
    goto delay_inner
    decfsz delay_counter2, f
    goto delay_b
    decfsz delay_counter1, f
    goto delay_a
    return
    
    end

Open it in the simulator

Next