Daisy chaining 7-segment MAX7219 modules.

Place code snippets and demo code here
Post Reply
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Daisy chaining 7-segment MAX7219 modules.

Post by Fernando Perez »

The Annex instructions MAXDISPLAY.SETUP and MAXDISPLAY.PRINT work without problems, but I have not been able to control more than one module at a time.
So I have written some functions to achieve it.
If they are useful to someone, I am satisfied.

Code: [Local Link Removed for Guests]

' MAX7219 7 segments
modules = 4                     ' total chained modules
bufferSize = (modules*8) - 1    ' define size output buffer
dim buffer(bufferSize)
dim dot(bufferSize)

' load character map for seven-segment display
dim pattern(63)
for i = 0 to 63
  READ n : pattern(i) = n
next i 

CS  = 15
pin.mode CS, output

' **********  DEMO  ***********
initMAX 0

printMAX "01234567", 1  ' load data into module one buffer
printMAX "89ABCDEF", 4  ' load data into module two buffer
printMAX "GHIJKLMN", 3
printMAX "OPQRSTUV", 2
refreshMAX                ' send buffer to display  
refreshMAX

pause 2000

timer0 1000, hour
wait
END

' ---------------------------------
SUB initMAX(brightness)
LOCAL module
  spi.setup 1000000
  pin(CS) = 1 
  for module = 1 to modules
    writeMAX &H09, 0, module            ' 6H09 = MAX7219DECODE
    writeMAX &H0A, brightness, module   ' &H0A = MAX7219INTENSITY
    writeMAX &H0B, 7, module            ' &H0B = MAX7219SCANLIMIT
    writeMAX &H0F, 0, module            ' &H0F = MAX7219TEST; 0 = TESTMODEOFF
    writeMAX &H0C, 1, module            ' &H0C = MAX7219SHUTDOWN; 1 = MAX7219ON
  next module
END SUB

' ---------------------------------
SUB clearMAX
LOCAL index
  for index = 0 to bufferSize
    buffer(index) = 0
    dot(index) = 0
  next index  
END SUB

' ---------------------------------
SUB writeMAX(opcode, value, module)
LOCAL i, r
  pin(CS) = 0
  for i = 1 to modules
    if i = module then  
      r = spi.byte(opcode)
      r = spi.byte(value)
    else
      r = spi.byte(0)  ' MAX7219NOOP
      r = spi.byte(0)
    endif
    pin(CS) = 1
  next i    
END SUB

' ---------------------------------
SUB refreshMAX
LOCAL module, digit, position, value
  for module = 1 to modules
    for digit = 1 to 8
      position = (8*module) - digit
      value = buffer(position)
      if dot(position) = 1 then value = value OR &H80 ' turn on the decimal point
      writeMAX digit, value, module    
    next digit
  next module      
END SUB

' ---------------------------------
SUB printMAX(text$, module)
LOCAL index, offset, value, dots

  text$ = uCase$(text$)
  offset = ( 8 * (module - 1)) - 1

' saves the position of the decimal points
' and removes them from the string
  dots = 0
  for index = 1 to len(text$)
    if mid$(text$, index, 1) = "." then
      dot(offset+index-1-dots) = 1
      dots = dots + 1
    endif  
  next index  

  text$ = replace$(text$, ".", "")

  for index = 1 to len(text$)
    value = asc(mid$(text$, index, 1)) - 32
    buffer(index+offset) = pattern(value)
  next index

END SUB

' ----------------------
SUB numInt(number, txt$)
  LOCAL temp$, dp1$, dp2$
  txt$ = "OUTRANGE"
  if (number > 99999999) OR (number < -9999999) then EXIT SUB
  temp$ = str$(cint(number))
  txt$ = space$(8-len(temp$)) + temp$
  dp1$ = "" : dp2$ = ""
  temp$ = mid$(txt$, 2, 1)
  if temp$ <> " " AND temp$ <> "-" then dp1$ = "."
  temp$ = mid$(txt$, 5, 1)
  if temp$ <> " " AND temp$ <> "-" then dp2$ = "."
  txt$ = left$(txt$, 2) + dp1$ + mid$(txt$, 3, 3) + dp2$ + right$(txt$, 3)
END SUB


' ========================================================================
DATA &H00,&HA0,&H22,&H36,&H4B,&H5A,&H6F,&H02,&H4E,&H78,&H63,&H07,&H18,&H01
DATA &H80,&H25,&H7E,&H30,&H6D,&H79,&H33,&H5B,&H5F,&H70,&H7F,&H7B,&H09,&H19
DATA &H43,&H41,&H61,&H65,&H7D,&H77,&H1F,&H4E,&H3D,&H4F,&H47,&H5E,&H37,&H30,
DATA &H38,&H57,&H0E,&H54,&H76,&H7E,&H67,&H73,&H66,&H5B,&H0F,&H3E,&H3A,&H2A
DATA &H37,&H3B,&H69,&H4E,&H13,&H78,&H62,&H08,&H02
' ========================================================================

' **********  DEMO  ***********
hour:
  clearMAX
  printMAX replace$(time$, ":", " "), 1
'  printMAX replace$(date$, "/", "-"), 2

  txt_RND$ = ""
  numInt rnd(99999999)/10, txt_RND$
  printMAX txt_RND$, 2

  txt_millis$ = ""
  numInt millis, txt_millis$
  printMAX txt_millis$, 4

  tExt = (rnd(600)/10) - 10
  tExt$ = str$(tExt, "%2.1f")
  if len(tExt$) < 4 then tExt$ = space$(1) + tExt$
  tInt = rnd(500) / 10
  tInt$ = str$(tInt, "%2.1f")
  if len(tInt$) < 4 then tInt$ = space$(1) + tInt$
  temperatures$ = tExt$ + "*" + tInt$ + "*"
  printMAX temperatures$, 3

  refreshMAX
  refreshMAX 
return

' NOTES
' -----
' Pin DIN = 13 (D7)
' Pin CLK = 14 (D5)
' Pin CS  = 15 (D8) 
' module1 = To ESP8266
' module2 = To module1
' etc. ....

' maximum consumption (eight eights) according to brightness
'  brightness    mA
'  ----------   ---
'     0          12
'     1          24
'     2          36
'     3          48
'     4          60
'     5          72
'     6          82
'     7          93
'     8         104
'     9         115
'    10         125
'    11         135
'    12         145
'    13         154
'    14         164
'    15         173 

https://youtu.be/Epf_NPBmx84
User avatar
cicciocb
Site Admin
Posts: 1899
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1269 times
Contact:

Re: Daisy chaining 7-segment MAX7219 modules.

Post by cicciocb »

Thanks Fernando for pushing forward Annex :idea:
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: Daisy chaining 7-segment MAX7219 modules.

Post by Fernando Perez »

Comme d'habitude, thank you for your work.
What I regret is that these days I am very lazy to finish my website in Spanish dedicated to Annex, which I am sure will attract at least 60 or 70 fans of the 548 million Spanish-speaking people to the forum. :? But cup by cup...
User avatar
Electroguard
Posts: 836
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 268 times
Been thanked: 317 times

Re: Daisy chaining 7-segment MAX7219 modules.

Post by Electroguard »

Hi Fernando, I just received a 4x8x8 module today (four 8x8 joined together).
I planned to use it for a smartclock which displays time normally for a few seconds before scrolling weather data.
But then I realised that the four 8x8 displays only allow 4 static numbers, without a separating colon or decimal point.
So remembering your post about joining several modules together, I've ordered another 4x8x8 to hopefully allow a display of eight 8x8 digits... should that work ok ?
User avatar
PeterN
Posts: 366
Joined: Mon Feb 08, 2021 7:56 pm
Location: Krefeld, Germany
Has thanked: 171 times
Been thanked: 203 times
Contact:

Re: Daisy chaining 7-segment MAX7219 modules.

Post by PeterN »

Hello Electroguard,
maybe I mistranslated something - but are you talking aboug the same modules? The MAX7219 is used in 7segment arrays on one hand and in 8*8 LED modules on the other. I have used the chained 4*8*8 modules for instance here:
[Local Link Removed for Guests]
User avatar
Electroguard
Posts: 836
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 268 times
Been thanked: 317 times

Re: Daisy chaining 7-segment MAX7219 modules.

Post by Electroguard »

Hi Peter, yes I was talking about the MAX7219 dot matrix modules ... and yes, your project with the flying banner is exactly the sort of display I am after - so well spotted and thanks, cos I hadn't seen noticed that project before.
Post Reply