' Make-it-with-Micromite (Part 12)

' Bit Banging MAX7219 8x8 LED Matrix



' (MIWM_LEDmatrixCount.txt)

' This program will continually count

' from 0 to 9 on an 8x8 LED Matrix





' SETUP

' -----



Option AUTORUN ON  ' ensure program starts on Power-Up



SDO=9              ' Serial Data Out = Pin 9 (to DIN)

CS=4               ' Chip Select = Pin 4     (to CS)

SCL=10             ' Serial CLock = Pin 10   (to CLK)



Pin(CS)=1          ' active low - so initially set high

SetPin SDO,dout

SetPin CS,dout

SetPin SCL,dout



Dim pattern(10,8)  ' 10 digits, 8 columns per character



' each DATA line represents dot-patterns for the 8-columns

' (left to right) msb=bottom dot, lsb=top dot



Data &h7E, &hFF, &hFF, &hC3, &hC3, &hFF, &hFF, &h7E ' 0

Data &h00, &h00, &h04, &h06, &hFF, &hFF, &hFF, &h00 ' 1

Data &hE6, &hE7, &hF7, &hD3, &hDB, &hCF, &hCF, &hC6 ' 2

Data &hDB, &hDB, &hDB, &hDB, &hDB, &hFF, &hFF, &h66 ' 3

Data &h1F, &h1F, &h1F, &h18, &hFF, &hFF, &hFF, &h18 ' 4

Data &h5F, &hDF, &hDF, &hDB, &hDB, &hFB, &hFB, &h73 ' 5

Data &h7E, &hFF, &hFF, &hDB, &hDB, &hFB, &hFB, &h72 ' 6

Data &h03, &h03, &hE3, &hF3, &hFB, &h1F, &h0F, &h07 ' 7

Data &h66, &hFF, &hFF, &hDB, &hDB, &hFF, &hFF, &h66 ' 8

Data &h4E, &hDF, &hDF, &hDB, &hDB, &hFF, &hFF, &h7E ' 9



' load the 8-byte dot-patterns into Pattern(i,x) array (i=digit reference 0-9)

For i = 0 To 9: For x = 1 To 8: Read Pattern(i,x): Next x: Next i



M7219_INIT         ' initialise the MAX7219 IC





' MAIN PROGRAM

' ------------



Do                        ' start of loop

  For DispCount = 0 To 9    ' load DispCount with digit (start with 0)

    LoadPattern(DispCount)    ' call SUB to display value in DispCount

    Pause 500                 ' delay a bit

  Next DispCount            ' increment DispCount with next digit (up to 9)

Loop                      ' loop back to repeat 0-9 again





' SUBROUTINES

' -----------



Sub M7219_INIT       ' SUB to initialise the MAX7219 IC (from Datasheet)

  MAXwrite(&H0900)     ' BCD DECODE ALL DIGITS

  MAXwrite(&H0A02)     ' BRIGHTNESS (0-15)

  MAXwrite(&H0B07)     ' 8 COLUMNS

  MAXwrite(&H0F00)     ' TESTS OFF

  MAXwrite(&H0C01)     ' OPERATING MODE (i.e. NOT SHUTDOWN MODE)

  ERASE_ALL            ' SUB to clear matrix (all LEDs Off)

End Sub



Sub ERASE_ALL        ' SUB to turn off all LEDs

  For column=1 To 8    ' load column with value (start with 1)

    MAXwrite(&H0000+column*256) ' writes 0's to all LEDs in column

  Next column          ' increment Column (up to 8)

End Sub



Sub LoadPattern(ww)  ' SUB to recall the eight 'dot-pattern' values for digit 'ww'

  For i = 1 To 8       ' work through all eight dot-patterns for the eight columns

    MAXwrite((256*i)+pattern(ww,i)) ' send to display module

  Next i

End Sub



Sub MAXwrite(value)     ' SUB to serially bit-bang the 16 bits of 'value'

  Pin(CS)=0

  For mask=15 To 0 Step -1   ' mask is used to extract relevant bit in 'value'

    Pin(SDO)=((2^mask) And value) '[(2^mask) AND value] will serially extract each bit

    Pulse SCL,0.05           ' pulse clk to shift relevant 'value' bit into MAX7219 DIN

  Next mask                  ' serially clk out all 16 bits of value

  Pin(CS)=1

End Sub