Shop OBEX P1 Docs P2 Docs Learn Events
MAX7219 LED Driver Sample Code: Hope This Is Helpful — Parallax Forums

MAX7219 LED Driver Sample Code: Hope This Is Helpful

ClaytronClaytron Posts: 6
edited 2014-02-17 08:37 in BASIC Stamp
I had a really hard time figuring out the sample code provided by Parallax for the MAX7219 LED Display Driver chip that they offer. I've written a more general purpose testing application that I hope is more easily understood to someone who is unfamiliar with the chip. I have decided to post it here so it might ease other's troubles. I have tried to comment as best I could to explain what is going on. Use this however you see fit. By using this code, you acknowledge that I am not liable for any damage that might result from the usage of this code or any variations on it. Please use in conjunction with the documentation Parallax already provides, which can be found here:

www.parallax.com/dl/docs/prod/appkit/max7298leddisplaydvr.pdf

' {$STAMP BS2}
' {$PBASIC 2.5}

' Program: MAXTest.bs2 (based on MAX7219.B2)
' Author: C. Richardson
' This program runs test patterns and checks specific LED combinations 
' for the MAX7219 display controller.

' Hardware interface with the 7219: 
DATA_n CON 7 ' Bits are shifted out this pin # to 7219. 
CLK CON 5 ' Data valid on rising edge of this clock pin. 
Load CON 6 ' Tells 7219 to transfer data to LEDs. 
' Register addresses for the MAX7219. To control a given attribute 
' of the display, for instance its brightness or the number shown 
' in a particular digit, you write the register address followed 
' by the data. For example, to set the brightness, you'd send 
' 'brite' followed by a number from 0 (off) to 15 (100% bright). 
decode CON 9 ' Decode register; a 1 turns on BCD decoding. 
brite CON 10 ' "       "   " intensity register.
scan CON 11 ' "       "   " scan-limit register. 
switch CON 12 ' "       "   " on/off register. 
test CON 15 ' Activates test mode (all digits on, 100% bright) 
' Variables used in the program. 
max_dat VAR WORD ' Word to be sent to MAX7219. 
index VAR NIB ' Index into setup table. 
scanNumber VAR NIB 'Number of Digits/Rows
temp VAR BYTE ' Temporary variable used in outputting digits. 
odd VAR index.BIT0 ' Lsb of index. 
' The program begins by setting up all pins to output low, matching 
' the state established by the pulldown resistors. 
OUTS = 0 ' All output latches low. 
DIRS = $FFFF ' All pins to output.

    scanNumber = 0 ' Number of digits/rows...

Main:
   GOSUB SetupCodeB
   GOSUB DisplayChar
   GOSUB SetupNoCode
   GOSUB DisplayEach
   GOSUB TestChar

GOTO Main 

SetupNoCode:
FOR index = 0 TO 7 ' Retrieve 8 setup bytes from table. 
    LOOKUP index,[noparse][[/noparse]scan,scanNumber,brite,3,decode,$00,switch,1],max_dat 'Get the byte...
    SHIFTOUT DATA_n,CLK,MSBFIRST,[noparse][[/noparse]max_dat]  'Send it...
    IF odd = 0  THEN NoLoad  ' If both bytes have been sent, pulse Load line. 
    PULSOUT Load,1
    NoLoad: ' Else, don't pulse. 
NEXT ' Get next item from table. 
RETURN

SetupCodeB:
FOR index = 0 TO 7 ' Retrieve 8 setup bytes from table. 
    LOOKUP index,[noparse][[/noparse]scan,scanNumber,brite,1,decode,$01,switch,1],max_dat 'Get the byte...
    SHIFTOUT DATA_n,CLK,MSBFIRST,[noparse][[/noparse]max_dat] 'Send it...
    IF odd = 0  THEN NoLoad2  ' If both bytes have been sent, pulse Load line.  
    PULSOUT Load,1
    NoLoad2: 
NEXT ' Get next item from table. 
RETURN

DisplayEach: 'Cycles through all possible led combinations on each row/digit...
FOR index = 1 TO (scanNumber+1)
    FOR temp = 1 TO 255
        SHIFTOUT DATA_n,CLK,MSBFIRST,[noparse][[/noparse]index] ' Send the row/digit...
        SHIFTOUT DATA_n,CLK,MSBFIRST,[noparse][[/noparse]temp] ' ...then send the segment id's. 
        PULSOUT Load,5 ' And load the display.
        DEBUG "Current Display: "
        DEBUG DEC index, CR
        DEBUG "Current Config: "
        DEBUG DEC temp, CR
        PAUSE 100
    NEXT
NEXT
RETURN

DisplayChar: 'Displays all possible CodeB characters...
FOR index = 1 TO (scanNumber+1)
    FOR temp = 0 TO 16
        SHIFTOUT DATA_n,CLK,MSBFIRST,[noparse][[/noparse]index] ' Send the row/digit...
        SHIFTOUT DATA_n,CLK,MSBFIRST,[noparse][[/noparse]temp] ' ...then send the CodeB character ID. 
        PULSOUT Load,5 ' And load the display.
        DEBUG "Current Display: "
        DEBUG DEC index, CR
        DEBUG "Current Character Code: "
        DEBUG DEC temp, CR
        PAUSE 100
    NEXT
NEXT
RETURN

TestChar: 'Displays specific LED segment combinations for testing..
        SHIFTOUT DATA_n,CLK,MSBFIRST, ' Send the row/digit...
        SHIFTOUT DATA_n,CLK,MSBFIRST,[noparse][[/noparse]255] ' Enter value to test and see if you've made a specific character... 
        PULSOUT Load,5 ' And load the display.

        PAUSE 20000 ' Pause for 20 seconds...
RETURN

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-23 15:33
    Claytron,

    Sorry to hear you had trouble getting things going with the MAX7219. There are actually several examples out there besides the one you used, including applications posted in the Completed Projects Forum, such as the following…Take care.

    http://forums.parallax.com/showthread.php?p=552892


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • ClaytronClaytron Posts: 6
    edited 2007-03-23 19:42
    Yes, I saw your clock. But I needed something just to explain the basic functionality outside of code that is being used for a specific purpose, which is why I wrote this and why I'm sharing it thusly. But that code was helpful for other reasons.
  • CuriousOneCuriousOne Posts: 931
    edited 2014-02-07 05:46
    A piece of code is missing here:
    SHIFTOUT DATA_n,CLK,MSBFIRST, ' Send the row/digit...
    
  • ercoerco Posts: 20,257
    edited 2014-02-07 08:57
    @C1: This is an ancient post and the OP hasn't checked in since 2007.
  • CuriousOneCuriousOne Posts: 931
    edited 2014-02-17 03:17
    But we need it anyways :)
  • ercoerco Posts: 20,257
    edited 2014-02-17 08:37
    Right you are and thanks for your contribution!

    Just wanted to make sure you didn't feel back if the OP didn't reply. :)

    Or as the media would report it:

    Phone calls to the original poster were not immediately returned ...
Sign In or Register to comment.