Using a MAX7219 to control a ton of LED's (3-wire serial)

DEF tic62_c
LIB fbasic.lib

DEF max7219_data pin_d2
DEF max7219_clk pin_d3
DEF max7219_load pin_d1

DEF max7219_dig0 0x0100w
DEF max7219_dig1 0x0200w
DEF max7219_dig2 0x0300w
DEF max7219_dig3 0x0400w
DEF max7219_dig4 0x0500w
DEF max7219_dig5 0x0600w
DEF max7219_dig6 0x0700w
DEF max7219_dig7 0x0800w
DEF max7219_decode 0x0900w
DEF max7219_intens 0x0A00w
DEF max7219_limit 0x0B00w
DEF max7219_shutdn 0x0C00w
DEF max7219_test 0x0F00w

GLOBAL word cur_row
GLOBAL byte cur_col

FUNC none max7219_send
    PARAM word max_comm
    PARAM byte max_data
    LOCAL word max_result
    LOCAL byte bit_counter 16b
BEGIN
    =( max_result, +( max_comm, max_data ))
    pin_low( max7219_clk )
    pin_low( max7219_load )
    REP
        IF >=( max_result, 0x8000w )
            pin_high( max7219_data )
        ELSE 
            pin_low( max7219_data )
        ENDIF

        pin_high( max7219_clk )
        --( bit_counter )
        pin_low( max7219_clk )
        =( max_result, <<( max_result ))
    UNTIL ==( bit_counter, 0b )

    pin_high( max7219_load )
ENDFUN
FUNC none main
BEGIN
    ; start by initializing the display
    max7219_send( max7219_decode, 0y00000000b ) ; numeric decode
    max7219_send( max7219_intens, 0y00001111b ) ; full brightness
    max7219_send( max7219_limit, 0y00000111b ) ; all rows (digits) on
    max7219_send( max7219_shutdn, 0y00000001b ) ; normal operation
    max7219_send( max7219_test, 0y00000001b ) ; test in progress
    delay( 500 ) ; one half second of LED test
    max7219_send( max7219_test, 0y00000000b ) ; no test in progress

    REP
        ; test rows independently
        =( cur_row, max7219_dig0 )
        REP
            max7219_send( cur_row, 0y11111111b ) ; All 8 LED's on
            delay( 250 )                         ; wait 1/4 second
            max7219_send( cur_row, 0y00000000b ) ; all 8 LED's off
            =( cur_row, +( cur_row, 0x0100w ))   ; next row
        UNTIL ==( cur_row, max7219_dig7 )

        ; test columns independently
        =( cur_col, 0y00000001b )
        REP
            max7219_send( max7219_dig0, cur_col )
            max7219_send( max7219_dig1, cur_col )
            max7219_send( max7219_dig2, cur_col )
            max7219_send( max7219_dig3, cur_col )
            max7219_send( max7219_dig4, cur_col )
            max7219_send( max7219_dig5, cur_col )
            max7219_send( max7219_dig6, cur_col )
            max7219_send( max7219_dig7, cur_col ) ; turn of col LED's

            delay( 250 )
            max7219_send( max7219_dig0, 0b )  ; turn off col LED's
            max7219_send( max7219_dig1, 0b )
            max7219_send( max7219_dig2, 0b )
            max7219_send( max7219_dig3, 0b )
            max7219_send( max7219_dig4, 0b )
            max7219_send( max7219_dig5, 0b )
            max7219_send( max7219_dig6, 0b )
            max7219_send( max7219_dig7, 0b )

            =( cur_col, <<( cur_col ))
        UNTIL ==( cur_col, 0b )
    LOOP
ENDFUN

 

Protean Logic Inc. Copyright 05/06/04        Top of Page