B4R Question MAX7219 and 7-segment display

Mostez

Active Member
Licensed User
Hello,
I want to driver 4 multiplexed com. cathode 7-segment display with MAX7219, which library should I use, is there any code example? appreciated.
 

Mostez

Active Member
Licensed User
that's what I did before asking, code examples are for led matrix and I did not see examples for 7-segment!
 

emexes

Well-Known Member
Licensed User
that's what I did before asking, code examples are for led matrix and I did not see examples for 7-segment!
Each row of the matrix contains 8 LED's, which can just as easily be the 8 LED's of a 7-segment-plus-decimal-point display.

And you have 8 rows, thus you can drive up to 8 of these 7-segment displays.

You'll need a list of displayable characters eg: " " and "-" and "0" to "9" and maybe even "A", "b", "c", "d", "E", "F" for hexadecimal numbers

and you'll need a table to that tells you which segments to light up to draw each of those characters.

Then you just need to write something like:

Sub ShowNumber(N As String, OutputPort As Int)

that translates the characters in N to segments and outputs them to the MAX7219 using something like:

Sub SendByte(B as Byte, OutputPort as Int)

HAVING SAID ALL THAT,

Probably your first step is to just write a sub that can turn on or off all of the LED segments.

I know nothing about the MAX7219, other than it is a LED driver, but the first search result via Google was:

https://howtomechatronics.com/tutor...scrolling-text-android-control-via-bluetooth/

which looks promising. Let's have a read of that.
 

emexes

Well-Known Member
Licensed User
Righto, that page is pretty good. It's just a 3-wire serial connection (aka SPI) from the microcontroller to the LED driver. Wouldn't surprise me if your microcontroller had an SPI port that would do the trick, but from discovery and simplicity points-of-view, let's just bit-bang it ourselves.

Do you actually have a MAX7219 wired up to

1/ receive input from the B4R (Arduino?) microcontroller, and
2/ send output to the 7-segment LED's

If so, what are the three pins on the microcontroller that you are using to talk to the MAX7219? Eg here pins 8, 10 and 9:
B4X:
Sub Process_Globals
    Public ClockPin As Pin
    Public DataPin As Pin
    Public LoadPin As Pin
End Sub

Private Sub AppStart
    ClockPin.Initialize(8, ClockPin.MODE_OUTPUT)
    DataPin.Initialize(10, DataPin.MODE_OUTPUT)
    LoadPin.Initialize(9, LoadPin.MODE_OUTPUT)
End Sub
 
Last edited:

emexes

Well-Known Member
Licensed User
A simple 7seg led driver is easy enough to create with a few transistors and resistors...
Still, you only need 7 (a to f) output pins plus 1 per display in pull down mode. This way no need for drivers
Agreed, but the driver chip takes care of all the background refresh, all you need to do is give it the bit pattern once. No timers or background CPU drain. Via 3 pins, rather than 11.

And those same three pins can drive as many of these daisy-chained drivers as you like, so you've got future expansion covered as well.
 

Cableguy

Expert
Licensed User
I agree that it is the most efficient way to do it.
I am one of those to who "do it this way because it works" doesn't suffice. I need to know how and why it does work. This way I will be able to troubleshoot it if needed be.
It would be a great learning experience to the OP to start from basic, and then advance
 

emexes

Well-Known Member
Licensed User
Righto, once you've defined those pins, then we need to send it some data. I am assuming for these pins that the data pin is False/True = 0/1, the load pin is True/False = idle/load, and the clock pin doesn't really matter because we're going to do both edges within each data bit.

B4X:
Sub SendByte(B as Byte)

    Dim BitMask As Int = 0x01    'start at bit 0, shift each time around loop, to get data bits one-by-one

    For BitNumber = 0 to 7
        If Bit.And(B, BitMask) <> 0 Then
            DataPin.DigitalWrite(True)    'bit is 1
        Else
            DataPin.DigitalWrite(False)    'bit is 0
        End If
        Sleep(1)

        'cycle clock pin low to move bits in shift register(s) along by 1 bit
        ClockPin.DigitalWrite(False)
        Sleep(1)
        ClockPin.DigitalWrite(True)
        Sleep(1)

        BitMask = BitMask + BitMask    'shift left for next bit
    Next

End Sub

Sub SendBytes(B() As Byte)

    For I = 0 to B.Length - 1
        SendByte(B(I))
    Next

    'cycle load pin low to latch in shift register data
    LoadPin.DigitalWrite(False)
    Sleep(1)
    LoadPin.DigitalWrite(True)
    Sleep(1)

End Sub

Private Sub AppStart
    ClockPin.Initialize(8, ClockPin.MODE_OUTPUT)
    DataPin.Initialize(10, DataPin.MODE_OUTPUT)
    LoadPin.Initialize(9, LoadPin.MODE_OUTPUT)

    ClockPin.DigitalWrite(True)    'idle state
    LoadPin.DigitalWrite(True)    'idle state

    For I = 0x10 to 0xF0
        SendBytes(Array As Byte(0xAA, I, I, 0x55))    'test pattern - will look random, but if it is changing each second, then we are on the right track :-)
        Sleep(1000)
    Next

End Sub
I haven't tested it, but odds are good that it'll work first go. If not, use your oscilloscope to check the clock and load pins, maybe I've got the levels the wrong way around somewhere. Or if you only have a multimeter or logic probe, change the Sleep(1)s to Sleep(1000)s so that you can see the pin levels going up and down.
 
Last edited:

emexes

Well-Known Member
Licensed User
It would be a great learning experience to the OP to start from basic, and then advance
This is what I was aiming for. And you might be right about the starting point; I just assumed that because the OP used the phrase "multiplexed common cathode" that they already understood the principals behind it, but... perhaps not.

It's bedtime here anyway, so I'll hand the baton over to the dayshift (ie, you ;-) and am happy to continue on tomorrow when the MAXwhateveritwas is wired up and ready to go.
 

emexes

Well-Known Member
Licensed User
Nightshift reporting for duty :)

I've read the MAX datasheet, and it's not just a simple 64-bit shift register like I assumed, turns out it can: vary the intensity, not display unused digits, even does the number-to-seven-segment translation. So: more capable, but more complicated.

It'll be good to get a reference/tutorial implementation going, because MAX7219 seven-segment and dot-matrix displays are all over Ebay, and at almost-unbelievable prices too. In fact, I might just grab me one of each now to play with.
 

emexes

Well-Known Member
Licensed User
I want to driver 4 multiplexed com. cathode 7-segment display with MAX7219
Do you actually have a MAX7219 wired up to

1/ receive input from the B4R (Arduino?) microcontroller, and
2/ send output to the 7-segment LED's (or are you using a prebuilt module?)

If so, what are the three pins on the microcontroller that you are using to talk to the MAX7219?
 

Mostez

Active Member
Licensed User
I just recieved MAX7219 and 7-segment display today, i will build my own module. I think rLedControl 'setdigit' and 'setchar' methods will do the job. i will bread board the circuit soon, hope it works.
 

emexes

Well-Known Member
Licensed User
I think rLedControl 'setdigit' and 'setchar' methods will do the job.
Sounds good - Newton said something about standing on ye sholders of giants, ie not reinventing the wheel, so if rLedControl works, that's an extra hour or two you can spend at the pub instead :)

This is the best video I found about using the MAX7219, explains the various "commands", explains and writes code step-by-step and very clearly.

 

emexes

Well-Known Member
Licensed User
B4X:
Sg.SetChar(0,0,"6",False)
Sg.SetChar(0,1,"o",False)
Sg.SetChar(0,2,"o",False)
Sg.SetChar(0,3,"d",False)
:)
 

emexes

Well-Known Member
Licensed User
If you are flushed with success and have nothing to do ;-) you could flesh out the character set. To my eyes it is missing:

uppercase letters G I J O P S U Y Z
lowercase letters g i j q r t u y

I was a bit dubious about the uppercase B and D because they are (probably) identical to digits 8 and 0, but... I got over it.

Likewise, the lower case a and e had me mystified, until it clicked that they were probably out-of-scale full-height like O vs o.

Neat!
 
Last edited:
Top