PICAXE 18M2 Dual 7-Segment LED Up/Down Counter Version 2

radiosparks

Well-known member
A simple Deli Counter Take-a-Number display.

This code snippet was something I did a while ago. The original project used 3 inch LED displays and a high current driver IC.
Using the resistor values shown, shouldn't tax the PICAXE dissipation limits.

Quickly drawn using the FREE DigiKey Scheme-it Online Schematic drawing application.
Scheme-it-export-Two-Digit-7-Segment-Multiplexed-Display-2025-02-15-11-44.png
Program BASIC Code: Optimized to use a minimum of bytes with plenty of space to add other features (.. like a model rocket launcher count down timer).
Remember! adding code in the do/loop will cause more display flicker.

Rich (BB code):
#rem
    PICAXE 18M2 and Two 7-Segment LEDs as an Up/Down Counter Version 2
    
    Code: PICAXE-18M2 2-Digit UpDown Counter V2.bas
    
    Schematic: Scheme-it-export-Two-Digit-7-Segment-Multiplexed-Display-2025-02-15-11-44.png
    
    Purpose: Deli Counter Take-a-number 2-digit 7-Segment LED up/down 0 to 99 counter.
                   EEPROM used to save last count between power downs.
    
    Use two momentary push-buttons as up (increment) and down (decrement) switches.
    Use Common-Cathode 7-segment displays.
    
    BYTES USED: 141
#endrem

#picaxe 18M2
#no_data

symbol IncButton      = C.7   ' input for increment button
symbol DecButton      = C.6   ' input for decrement button

symbol Hundreds       = b4    ' hundreds digit count value - not used
symbol Tens           = b5    ' tens digit count value
symbol Units          = b6    ' units digit count value - used also to calculate the TENS value

symbol TensSegments   = b7    ' segment bit pattern
symbol UnitSegments   = b8    ' segment bit pattern
symbol NumberCount    = b9    ' count value

symbol IncState       = bit0  ' increment button value
symbol DecState       = bit1  ' decrement button value

symbol mask           = %10000000 ' use to select Tens Digit
                dirsB = %11111111 ' Set all pinsB as outputs.

' Retrieve last stored count value from EEPROM
read 1, NumberCount

' initial startup displays a single Zero digit
StoreSegmentsCount:

    ' Determine digits to display from Number Count
    bintoascii NumberCount, Hundreds, Tens, Units

    ' Get Units segments first, because variable is reused for TENS - saves 2 bytes
    gosub GetSegments

    if NumberCount >= 0 and NumberCount <= 9 then
        TensSegments = %00000000 ' leading zero blanking
    else
        Units = Tens
        gosub GetSegments
        'Set MSB (bit7) for tens display
        TensSegments = UnitSegments | mask
    endif
    
    ' Store count value in EEPROM
    write 1, NumberCount

    pause 500

Main:

    ' Initilaize INC and DEC button state
    IncState = 0
    DecState = 0

    do
        'Check button connected to pin C.5; if pressed, go to IncrementCounter.
        button IncButton, 1, 255, 255, IncState, 1, IncrementCounter
        
        'Check button connected to pin C.6; if pressed, go to DecrementCounter.
        button DecButton, 1, 255, 255, DecState, 1, DecrementCounter
        
        pinsB = UnitSegments 'Output units digit segment data to units 7-segment LED.
        pause 5 
        
        pinsB = TensSegments 'Output tens digit segment data to tens 7-segment LED.
        pause 5 
        
    loop ' forever infinite

' /*** FUNCTIONS and SUBROUTINES **/

IncrementCounter:

    inc NumberCount
    if NumberCount > 99 then
        NumberCount = 0
    endif
    
goto StoreSegmentsCount

DecrementCounter:
    
    dec NumberCount
    if NumberCount = 255 then
        NumberCount = 99
    endif

goto StoreSegmentsCount

GetSegments:

    ' The MSB (bit7) determines units(0) or tens(1);
    ' The 0 to 6 bits represent segments A, B, C, D, E, F, and G.
    
    ' Convert to Binary number to index into lookup
    Units = Units - $30
    
    lookup Units, (%00111111, %00000110, %01011011, %01001111, %01100110, %01101101, %01111101, %00000111, %01111111, %01101111), UnitSegments

return
 
Back
Top