Correction to MAX7219 Routines of Dec, '99

copyright, Peter H Anderson, Baltimore, MD, Feb 00


Recently while debugging some additional code for the MAX7219 7-seg Display Driver, I discovered that all segments of all 7-segment LEDs were on and my program was not functioning.

I traced the problem to MAX7219 register 15. When set to a 1, the 7219 goes into a test mode so as to light all segments on all displays.

Thus, in Sub MAX7219Init, a statement was added to write zero to register 15 so as to place it in normal (vs test) operation.

The following is a minor rework of MAX7219_1.Bas which includes this revision. Changes are denoted with a string of asterisks ******.


' MAX7219_1.Bas
'
' Illustrates the use of a MAX7219 Serially Interfaced, 8-Digit Display 
' Driver.  
'
' Example uses four MAN74 7-Segment LEDs (Common Cathode) as Digits 3, 2, 
' 1 and 0.
'
'   BX-24 			MAX7219
'
'  CS (term 12) ------------ LOAD (term 12)
'  CLK (term 11) ----------- CLK (term 13)
'  DAT (term 10) ----------- DIN (term 1)
'
' In initializing the MAX7219;
'
'    Decode Mode (Register 9) set for Code B on all digits.
'    Intensity (Register 10) set for 15/32
'    Scan Limit (Register 11) set for Digits 0, 1, 2, 3
'    DisplayTest Register 15) set for normal operation ******

' The numbers 0000 - 9999 are sequentially displayed.  No sign and no zero
' suppression.
'
' Note that in outputting the 16 bit quantity, the quantity is split into
' a high and low byte and each is then output.
'
' This was a part of a Senior Project by Kenrick David.
'
' copyright, Peter H. Anderson, Baltimore, MD, Dec, '99
' revised on Feb 25, '00 to initialize MAX7219 Test Register to 0,
' (That is, normal operation vs test operation).  Changes are noted
' with asterisks ******.

   Const DecodeModeCode as Integer = &H900
   Const IntensityCode as Integer = &Ha00
   Const ScanLimitCode as Integer = &Hb00
   Const TurnOnCode as Integer = &Hc00
   Const DisplayTestCode as Integer = &Hf00 '******

   Const CS as Byte = 12
   Const CLK as Byte = 11
   Const DAT as Byte = 10

Sub Main()

   Dim Q as Integer
   Dim N as Integer

   Call OpenSerialPort(1, 19200)	' used for debugging
   Do
      Call Max7219Init()
	
      For N = 0 to 9999
         Call Max7219PutUI(N)	' unsigned intger, no zero suppression
         Call Sleep(1.0)
      Next
   Loop
End Sub

Sub Max7219Init() 	
' intialize, Code B, 15/32 intensity, Digits 0, 1, 2, 3, 
  
   Call Max7219Out16 (DisplayTestCode) ' Normal Operation vs Test
   ' ******** the above statement added

   Call Max7219Out16 (DecodeModeCode OR &Hff) 'Sets Decode Mode
   Call Max7219Out16 (IntensityCode  OR &H07) 'Sets Intensity
   Call Max7219Out16 (ScanLimitCode OR &H03) 'Sets Scan Limit
   Call Max7219Out16 (TurnOnCode OR &H01) ' Turn it on
End Sub

Sub Max7219PutUI(ByVal Q as Integer)	' 4 Digits
   Dim D as Integer
 
   D = Q\1000
   Call Max7219Out16(4*256 + D)	' note 00000100 in high byte - Digit 3
   Q = Q Mod 1000
         	  
   D = Q\100
   Call Max7219Out16(3*256 + D)	' 0000 0011 in high byte - Digit 2
   Q = Q mod 100
	  
   D = Q\10
   Call Max7219Out16(2*256 + D)	' Digit 1
   Q = Q Mod 10 
          
   D = Q
   Call Max7219Out16(1*256 + D)	' Digit 0	  
End Sub  
            
Sub Max7219Out16(ByVal X as Integer)
' shifts out 16-bit quantity, most sig bit first
   Dim N as integer
   Dim Bit as Byte
   Dim H as Byte, L as Byte

   Call PutPin(CS, 1)

   Call Putpin(CLK, 0)
   Call Putpin(DAT, 0)
   Call Putpin(CS, 0)
   
   ' split the integer into high and low bytes
   H =  CByte(X\256)
   L = CByte(X - (CInt(H)*256))

   For N = 1 to 8	' output the high byte
      Bit = H AND bx10000000 
            
      If(Bit <> 0) Then
         Call PutB(1)   ' for debugging
         Call Putpin(DAT, 1)
      Else
         Call PutB(0)
         Call Putpin(DAT, 0)
      End if

      Call Putpin(CLK, 1)
      Call Putpin(CLK, 0)
      H = H * 2
   Next

   Call PutByte(Asc(" "))
         
   For N = 1 to 8	' output the low byte
      Bit = L AND bx10000000 
            
      If(Bit <> 0) then
    	    Call PutB(1)   
	    Call Putpin(DAT, 1)
      Else
         Call PutB(0)
         Call Putpin(DAT, 0)            
      End if

      Call Putpin(CLK, 1)
      Call Putpin(CLK, 0)
      L = L * 2		
   Next
   Call NewLine()
   Call Putpin(CS, 1)		
End Sub