B4R Tutorial MAX7219 7-Segment Driver

Similar threads

B4R Library rLedControl
B4R Library rMAX7219LedMatrix
B4R Question MAX7219 and 7-segment display
B4A Tutorial [B4X] Smart String Literal
B4R Question Drive MAX7219 cascaded modules

Mostez

Well-Known Member
Licensed User
this is a very simple example shows how to drive 4-digits 7-segment display with MAX7219 and rLedControl library, this code example displays 'HoLA' word, you can add more digits and MAX7219 devices.
each MAX device can drive up to 8 7-segment digits.
33K ohm resistor should be connected between pin 18(ISET) and pin 19(V+) of MAX7219, not shown in provided datasheet.
datasheet and connection links below.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Private Sg As LedControl
    '33Kohm resistor should be connected between pin 18(ISET) and pin 19(V+) of MAX7219
    'CNQ-3641 4 digits 7-segment display module used(com cathode)
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Sg.Initialize(12,11,10,1)
    Sg.Shutdown(0,False)
    Sg.SetIntensity(0,8)
    Sg.SetScanLimit(0,4) '
    Sg.ClearDisplay(0)
    RunTest
End Sub

private Sub RunTest()
    Sg.SetChar(0,0,"H",False)
    Sg.SetChar(0,1,"o",False)
    Sg.SetChar(0,2,"L",False)
    Sg.SetChar(0,3,"A",False)
End Sub
7-Segment display datasheet
MAX7219 datasheet and connection
IMG_20190801_135515.jpg
 

Mostez

Well-Known Member
Licensed User
this is a modified example code, with string display and decimal point placement.
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Private Sg As LedControl
    Private bc As ByteConverter
    '33Kohm resistor should be connected between pin 18(ISET) and pin 19(V+) of MAX7219
    'CNQ-3641 4 digits 7-segment display module used(com cathode)
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Sg.Initialize(12,11,10,1)
    Sg.Shutdown(0,False)
    Sg.SetIntensity(0,8)
    Sg.SetScanLimit(0,4) '
    Sg.ClearDisplay(0)
    RunTest
End Sub

private Sub PrintMAX(Address As Int,Str() As Byte)
    
    Dim ArrayIndex As Byte
    Dim S As String
    Dim isDP As Int = bc.IndexOf(Str,".".GetBytes) 'check if DP in string and get index(place)
    If isDP > 0 Then
        Str = ReplaceString(Str,".".GetBytes,"".GetBytes ) 'remove DP from original string
    End If
    Do While ArrayIndex < Str.Length
        S = bc.StringFromBytes(Array As Byte(Str(ArrayIndex)))
        Sg.SetChar(Address,ArrayIndex,s,False)
        ArrayIndex = ArrayIndex + 1
    Loop
    Sg.SetLed(0,isDP-1,0,True) 'place DP
End Sub

private Sub RunTest()
    
    PrintMAX(0,"HoLA")
    Delay(1000)
    Sg.ClearDisplay(0)
    PrintMAX(0,"b4A")
    Delay(1000)
    Sg.ClearDisplay(0)
    PrintMAX(0,"12.34")
    Delay(1000)
    PrintMAX(0,"0.000")
    Delay(1000)
    PrintMAX(0,NumberFormat(55/16,2,2))
    Delay(1000)
    
End Sub

Public Sub ReplaceString(Original() As Byte, SearchFor() As Byte, ReplaceWith() As Byte) As Byte()
    'count number of occurrences
    Dim c As Int = 0
    Dim i As Int
    If SearchFor.Length <> ReplaceWith.Length Then
        i = bc.IndexOf(Original, SearchFor)
        Do While i > -1
            c = c + 1
            i = bc.IndexOf2(Original, SearchFor, i + SearchFor.Length)
        Loop
    End If
    Dim result(Original.Length + c * (ReplaceWith.Length - SearchFor.Length)) As Byte
    Dim prevIndex As Int = 0
    Dim targetIndex As Int = 0
    i = bc.IndexOf(Original, SearchFor)
    Do While i > -1
        bc.ArrayCopy2(Original, prevIndex, result, targetIndex, i - prevIndex)
        targetIndex = targetIndex + i - prevIndex
        bc.ArrayCopy2(ReplaceWith, 0, result, targetIndex, ReplaceWith.Length)
        targetIndex = targetIndex + ReplaceWith.Length
        prevIndex = i + SearchFor.Length
        i = bc.IndexOf2(Original, SearchFor, prevIndex)
    Loop
    If prevIndex < Original.Length Then
        bc.ArrayCopy2(Original, prevIndex, result, targetIndex, Original.Length - prevIndex)
    End If
    Return result
    
End Sub
 
Top