News:

Let's find out together what makes a PIC Tick!

Main Menu

MAX7219

Started by Volker, Dec 03, 2021, 09:42 AM

Previous topic - Next topic

Volker

Hallo everyone

Hope everybody is keeping well and staying safe.
Just wanted to know if there is anybody on the forum who used the max7219 for Dot Matrix Display 32X8 application.
Currently I have a application where I am using a Toshiba Graphic display but want to consider a LED dot matrix display as an alternative.
Some sample code to get me going will be highly appreciated.

I have attached some info of the dot matrix device I want to use.

Regards

Volker

RGV250

Hi,
This is not for a matrix display but should give you enough for initialising it and driving the LED's. It is for an old device 18F452 but I don't think there is anything that will be a problem with the latest compiler.

' This Program demonstrates the use of the MAX7219, seven segment decoder driver
' It also incorporates placing of Decimal point.

        Device 18F452
' ** Set Xtal Value in MHz **
Xtal = 4 ' Set Xtal Frequency

' ** Declare Pins Used **
Symbol Clk = PORTB.0 ' Data is clocked on rising edge of this pin
Symbol Dta  = PORTB.1 ' Bits are shifted out of this pin
Symbol Load = PORTB.2 ' Transfers data to LEDs when Pulsed

' ** Declare Constants **
Symbol Decode_Reg 9 ' Decode register, a 1 turns on BCD decoding for each digit.
Symbol Lum_Reg 10 ' Intensity register.
Symbol Scan_Reg     11 ' Scan-limit register.
Symbol Switch_Reg   12 ' On/Off Register.
Symbol Test_Reg 15 ' Test mode register (all digits on, 100% bright)

' Max_Digit Con 5 ' Amount of LED Displays being used.

' ** Declare Variables **
  Dim Counts As Word ' Variable used for the Demo Counting routine
' Dim Max_Disp As Word ' 16-bit value to be displayed by the MAX7219
' Dim Max_Dp As Byte ' Digit number to place Decimal point (0-4)
Dim Register As Byte ' Pointer to the Internal Registers of the MAX7219
Dim R_Val As Byte ' Data placed in Each Register
Dim Digit As Byte ' Position of individual numbers within MAX_Disp (0-3)
; Dim Position As Byte ' Position of each LED display (1-4)

' ** INITIALISE THE MAX7219 **
' Each register address is sent along with its setting data.
' Because the MAX7219 expects to see a packet of 16 bits, then the LOAD pin is pulsed
' Set the scan limit to 3 (4 digits, numbered 0-3)
' Set the Brightness to 5
' BCD decoding to the lower 4 digits
' Switch the display on.
' Turn Off test mode

Register = Scan_Reg ' Point to the Scan Register
R_Val = 0 ' Send 7, (Eight LED Displays 0-7)
GoSub Transfer ' Transfer this 16-bit Word to the MAX7219

Register = Lum_Reg ' Point to the Luminance Register
R_Val = 5 ' Send 5, (Value for Brightness)
GoSub Transfer ' Transfer this 16-bit Word to the MAX7219

Register = Decode_Reg ' Point to BCD Decode Register
R_Val = %00000000 ' NO Decode
GoSub Transfer ' Transfer this 16-bit Word to the MAX7219

Register = Switch_Reg ' Point to the Switch Register
R_Val = 1 ' Set to One, (switches the display ON)
GoSub Transfer ' Transfer this 16-bit Word to the MAX7219

Register = Test_Reg ' Point to the Test Register
R_Val = 0 ' ?????Reset to Zero, (turns off Test mode)
GoSub Transfer ' Transfer this 16-bit Word to the MAX7219


' ***** MAIN PROGRAM *****
' This loop increments and then decrements a 16-bit number
' And displays it on Four LED Displays
' The value to be displayed is held in the variable "Max_Disp"
' The Position of the decimal point is held in Max_DP (0-4)

' Max_Dp = 3 ' Display number for Decimal Point

Again: For Counts = 1 To 8 ' Increment Counter
' R_Val = Counts ' Load Max_Disp with Value of Counter
GoSub Display ' Display the Value of Counter
DelayMS 10 ' Delay, so we can see whats happening
Next ' Close the Loop

For Counts = 8 To 1 Step -1 ' Decrement Counter
' R_Val = Counts ' Load Max_Disp with Value of Counter
GoSub Display ' Display the Value of Counter
DelayMS 10 ' Delay, so we can see whats happening
Next ' Close the Loop
GoTo Again ' Do it Indefinately


' ** Subroutines **
' Display the Value held in the Variable "MAX_DISP" on the four LED's
' The value held in "MAX_DP" places the decimal point on that LED (0-3)
' Sending the value 15 blanks the display, this allows Zero suppression
' By setting bit-7 of the value sent to the individual LED displays, the decimal point
' Is illuminated

Display:
Digit = 1 ' Start at Digit 0 of Max_Disp Variable
'for Position = 4 To 1 Step -1 ' Start at Farthest Right of Display
Register = Digit  ''Position ' Place Position into Register
R_Val = Counts ' Extract the individual numbers from Max_Disp
' If Max_Disp < 10 And Position = 3 Then R_Val = 15 ' Zero Suppression for the second digit
' If Max_Disp < 100 And Position = 2 Then R_Val = 15 ' Zero Suppression for the Third digit
' If Max_Disp < 1000 And Position = 1 Then R_Val = 15 ' Zero Suppression for the Forth digit
' If Max_Disp < 10000 And Position = 0 Then R_Val = 15 ' Zero Suppression for the Fifth digit
' If Digit = Max_Dp Then R_Val.7 = 1 ' Place the decimal point, held in Max_DP
GoSub Transfer ' Transfer the 16-bit Word to the MAX7219
' If Digit >= 3 Then Digit = 0 ' We only need the first four digits
' Digit = Digit + 1 ' Point to next Digit within Max_Disp
' Next ' Close the Loop
Return ' Exit from subroutine

' Send a 16-bit word to the MAX7219
Transfer:
  SHOut Dta,Clk,msbfirst,[Register,R_Val] ' Shift Out the Register first, then the data
High Load ' The data is now acted upon
DelayUS 2 ' A small delay to ensure correct clocking times
Low Load ' Disable the MAX7219
Return ' Exit from Subroutine
       
       

Bob

Volker

Thanks Bob, much appreciated. This will help me a Lot

Cheers

Volker

Mapo

Hi, Volker
this is my test on max7219
can show only number but is a good point of start

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

                Device = 18F26K80
               
                Config_Start
                  RETEN = OFF ;Ultra low-power regulator is Disabled (Controlled by REGSLP bit)
                  INTOSCSEL = High ;LF-INTOSC in High-power mode during Sleep
                  SOSCSEL = Dig ;Digital (SCLKI) mode
                  XINST = OFF ;Disabled
                  FOSC = HS2 ;HS oscillator (High power, 16 MHz - 25 MHz)
                  PLLCFG = On ;Enabled
                  FCMEN = OFF ;Disabled
                  IESO = OFF ;Disabled
                  PWRTEN = OFF ;Disabled
                  BOREN = OFF ;Disabled in hardware, SBOREN disabled
                  BORV = 3 ;1.8V
                  BORPWR = ZPBORMV ;ZPBORMV instead of BORMV is selected
                  WDTEN = OFF ;WDT disabled in hardware; SWDTEN bit disabled
                  WDTPS = 1048576 ;1:1048576
                  CANMX = PORTC ;ECAN TX and RX pins are located on RC6 and RC7, respectively
                  MSSPMSK = MSK7 ;7 Bit address masking mode
                  MCLRE = On ;MCLR Disabled, RG5 Enabled
                  STVREN = OFF ;Disabled
                  BBSIZ = BB2K ;2K word Boot Block size
                  Cp0 = OFF ;Disabled
                  CP1 = OFF ;Disabled
                  CP2 = OFF ;Disabled
                  CP3 = OFF ;Disabled
                  CPB = OFF ;Disabled
                  CPD = OFF ;Disabled
                  WRT0 = OFF ;Disabled
                  WRT1 = OFF ;Disabled
                  WRT2 = OFF ;Disabled
                  WRT3 = OFF ;Disabled
                  WRTC = OFF ;Disabled
                  WRTB = OFF ;Disabled
                  WRTD = OFF ;Disabled
                  EBTR0 = OFF ;Disabled
                  EBTR1 = OFF ;Disabled
                  EBTR2 = OFF ;Disabled
                  EBTR3 = OFF ;Disabled
                  EBTRB = OFF ;Disabled
                Config_End

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
       
                Declare Xtal 64   

                Declare All_Digital = TRUE


                Dim Display_riga1 As Byte
                Dim Display_riga2 As Byte
                Dim Display_riga3 As Byte
                Dim Display_riga4 As Byte
                Dim Display_riga5 As Byte
                Dim Display_riga6 As Byte
                Dim Display_riga7 As Byte
                Dim Display_riga8 As Byte
               
                Dim Display_riga1_Comp[40] As Byte
               
                Dim Ore_Dec As Byte
                Dim Ore_Uni As Byte
                Dim Min_Dec As Byte
                Dim Min_Uni As Byte
                Dim Sec_Dec As Byte
                Dim Sec_Uni As Byte
               
                Dim Riga As Byte               
                Dim Posizione As Byte
                Dim Ciclo As Byte
                Dim Numero As Byte



               
                Symbol Dato = PORTB.0
                Symbol Clock = PORTB.1
                Symbol Attiva_Led = PORTB.2
                Symbol Led = PORTC.7


               
                TRISA = %00000000
                TRISB = %00000000
                TRISC = %00000000
               
                Ore_Dec = 1
                Ore_Uni = 0
                Min_Dec = 4
                Min_Uni = 2
                Sec_Dec = 5
                Sec_Uni = 3
               
                High Attiva_Led
                DelayMS 500

                Clear Posizione
               
                High Led
               
                Low Attiva_Led
                DelayMS 1
                SHOut Dato, Clock, MsbFirst, [$0c,1,$0c,1,$0c,1]
                DelayMS 1
                High Attiva_Led
               
                DelayMS 200

                Low Attiva_Led
                DelayMS 1
                SHOut Dato, Clock, MsbFirst, [$0b,7,$0b,7,$0b,7]
                DelayMS 1
                High Attiva_Led
               
                DelayMS 300
'--------------------------------------------------------
' MAIN                               
Main:           
                Display_riga8 = 0
                GoSub Carica_riga

                Low Attiva_Led
                DelayMS 1
                SHOut Dato, Clock, MsbFirst, [1,Display_riga1,1,Display_riga1,1,Display_riga1,1,Display_riga1,1,Display_riga1]
                DelayMS 1
                High Attiva_Led
                DelayMS 1
                Low Attiva_Led
                DelayMS 1
                SHOut Dato, Clock, MsbFirst, [2,Display_riga2,2,Display_riga2,2,Display_riga2,2,Display_riga2,2,Display_riga2]
                DelayMS 1
                High Attiva_Led
                DelayMS 1
                Low Attiva_Led
                DelayMS 1
                SHOut Dato, Clock, MsbFirst, [3,Display_riga3,3,Display_riga3,3,Display_riga3,3,Display_riga3,3,Display_riga3]
                DelayMS 1
                High Attiva_Led
                DelayMS 1
                Low Attiva_Led
                DelayMS 1
                SHOut Dato, Clock, MsbFirst, [4,Display_riga4,4,Display_riga4,4,Display_riga4,4,Display_riga4,4,Display_riga4]
                DelayMS 1
                High Attiva_Led
                DelayMS 1
                Low Attiva_Led
                DelayMS 1
                SHOut Dato, Clock, MsbFirst, [5,Display_riga5,5,Display_riga5,5,Display_riga5,5,Display_riga5,5,Display_riga5]
                DelayMS 1
                High Attiva_Led
                DelayMS 1
                Low Attiva_Led
                DelayMS 1
                SHOut Dato, Clock, MsbFirst, [6,Display_riga6,6,Display_riga6,6,Display_riga6,6,Display_riga6,6,Display_riga6]
                DelayMS 1
                High Attiva_Led
                DelayMS 1
                Low Attiva_Led
                DelayMS 1
                SHOut Dato, Clock, MsbFirst, [7,Display_riga7,7,Display_riga7,7,Display_riga7,7,Display_riga7,7,Display_riga7]
                DelayMS 1
                High Attiva_Led
                DelayMS 1   
                Low Attiva_Led
                DelayMS 1
                SHOut Dato, Clock, MsbFirst, [8,Display_riga8,8,Display_riga8,8,Display_riga8,8,Display_riga8,8,Display_riga8]
                DelayMS 1
                High Attiva_Led

               
                Toggle Led
               
                DelayMS 1000
               
                Inc Numero

                If Numero = 10 Then Clear Numero
               
                GoTo Main           
               
               
Carica_riga:
                Select Numero             
                    Case 0   
                        Display_riga1 = 112
                        Display_riga2 = 136
                        Display_riga3 = 152
                        Display_riga4 = 168
                        Display_riga5 = 200               
                        Display_riga6 = 136               
                        Display_riga7 = 112
                    Case 1   
                        Display_riga1 = 4
                        Display_riga2 = 12
                        Display_riga3 = 20
                        Display_riga4 = 4
                        Display_riga5 = 4               
                        Display_riga6 = 4               
                        Display_riga7 = 14
                    Case 2   
                        Display_riga1 = 14
                        Display_riga2 = 17
                        Display_riga3 = 1
                        Display_riga4 = 2
                        Display_riga5 = 4               
                        Display_riga6 = 8               
                        Display_riga7 = 31
                    Case 3   
                        Display_riga1 = CRead Font_3
                        Display_riga2 = CRead Font_3 + 1
                        Display_riga3 = CRead Font_3 + 2
                        Display_riga4 = CRead Font_3 + 3
                        Display_riga5 = CRead Font_3 + 4               
                        Display_riga6 = CRead Font_3 + 5               
                        Display_riga7 = CRead Font_3 + 6
                    Case 4   
                        Display_riga1 = CRead Font_4
                        Display_riga2 = CRead Font_4 + 1
                        Display_riga3 = CRead Font_4 + 2
                        Display_riga4 = CRead Font_4 + 3
                        Display_riga5 = CRead Font_4 + 4               
                        Display_riga6 = CRead Font_4 + 5               
                        Display_riga7 = CRead Font_4 + 6
                    Case 5   
                        Display_riga1 = CRead Font_5
                        Display_riga2 = CRead Font_5 + 1
                        Display_riga3 = CRead Font_5 + 2
                        Display_riga4 = CRead Font_5 + 3
                        Display_riga5 = CRead Font_5 + 4               
                        Display_riga6 = CRead Font_5 + 5               
                        Display_riga7 = CRead Font_5 + 6
                    Case 6   
                        Display_riga1 = CRead Font_6
                        Display_riga2 = CRead Font_6 + 1
                        Display_riga3 = CRead Font_6 + 2
                        Display_riga4 = CRead Font_6 + 3
                        Display_riga5 = CRead Font_6 + 4               
                        Display_riga6 = CRead Font_6 + 5               
                        Display_riga7 = CRead Font_6 + 6
                    Case 7   
                        Display_riga1 = CRead Font_7
                        Display_riga2 = CRead Font_7 + 1
                        Display_riga3 = CRead Font_7 + 2
                        Display_riga4 = CRead Font_7 + 3
                        Display_riga5 = CRead Font_7 + 4               
                        Display_riga6 = CRead Font_7 + 5               
                        Display_riga7 = CRead Font_7 + 6
                    Case 8   
                        Display_riga1 = CRead Font_8
                        Display_riga2 = CRead Font_8 + 1
                        Display_riga3 = CRead Font_8 + 2
                        Display_riga4 = CRead Font_8 + 3
                        Display_riga5 = CRead Font_8 + 4               
                        Display_riga6 = CRead Font_8 + 5               
                        Display_riga7 = CRead Font_8 + 6
                    Case 9   
                        Display_riga1 = CRead Font_9
                        Display_riga2 = CRead Font_9 + 1
                        Display_riga3 = CRead Font_9 + 2
                        Display_riga4 = CRead Font_9 + 3
                        Display_riga5 = CRead Font_9 + 4               
                        Display_riga6 = CRead Font_9 + 5               
                        Display_riga7 = CRead Font_9 + 6
                    Case Else
                       
                   
                    EndSelect
               
                Return                 
             
'------------------------------------------------
'DATI PIXEL CARATTERI
Font_0:
        CData 14,17,19,21,25,17,14   '0
Font_1:
        CData 4,12,20,4,4,4,14     '1
Font_2:
        CData 14,17,1,2,4,8,31     '2
Font_3:
        CData 14,17,1,6,1,17,14     '3
Font_4:
        CData 2,6,10,18,31,2,2     '4
Font_5:
        CData 31,16,16,30,1,1,30    '5
Font_6:
        CData 14,17,16,30,17,17,14   '6
Font_7:
        CData 31,1,2,4,4,4,4     '7
Font_8:
        CData 14,17,17,14,17,17,14   '8
Font_9:
        CData 14,17,17,15,1,17,14   '9
Font_Duepunti:
        CData 0,6,6,0,6,6,0     ':
'------------------------------------------------   

       

Volker

Hallo Mapo, thanks very much. Appreciated

Regards

Volker
 

Penthode

#5
Here is my modest entry for using MAX7219 to Dot Matrix 4 in 1 for counting from 0 to 9999  ;)

'****************************************************************
'*  Name    : MCD MAX7219_04.BAS                                *
'*  Janvier 2022                                                *
'*  16F877A on MDC Board  to Dot Matrix 4 in 1                  *
'*  Penthode                                                    *
'*  MCD MAX7219_04.BAS                                          *
''***************************************************************




'                                                                                              Enjoy PIC
'         Output                                                 Input                      +---------------+
'                            Dot Matrix 4 in 1                                              |               |
'            +-----------------------------------------------------+                        |               |
'        <---|                                                 Clk |<-----------------------| B.0           |
'            |    +-------+   +-------+  +-------+  +-------+      |                        |               |
'        <---|    |       |   |       |  |       |  |       |  CS  |<-----------------------| B.1           |
'            |    |       |   |       |  |       |  |       |      |                        |               |
'        <---|    |  8x8  |   |  8x8  |  |  8x8  |  |  8x8  |  Din |<-----------------------| B.2           |
'            |    |       |   |       |  |       |  |       |      |                        |               |
'        <---|    |       |   |       |  |       |  |       |  Gnd |<-----------+           |    16F877A    |
'            |    +-------+   +-------+  +-------+  +-------+      |            |           +---------------+
'        <---|                                                 Vcc |<--- Vcc    |                           
'            +-----------------------------------------------------+            |                           
'                              4 matrix 8x8 leds                                |                           
'                                                                               |                           
'                                                                              ///                          
'       





Device = 16F877A     ' We'll use a PIC16F877 PICmicro
Xtal = 20 ' With a 20MHz crystal


'**** Added by Fuse Configurator ****
 Config CP_OFF, DEBUG_OFF, WRT_OFF, CPD_OFF, LVP_OFF, PWRTE_ON, WDTE_OFF, HS_OSC, BOREN_ON, CP_OFF, WDTE_OFF

 
' ***  Déclaration Ports

TRISA=%00000000     ' Analog input
        TRISB=%00000000    
TRISC=%00000000     
TRISD=%00000000      

    Declare All_Digital = TRUE        ' No analog input


'*********


        Symbol Clk  = PORTB.0               '  CLK  => Data is clocked on rising edge of this pin
        Symbol Load = PORTB.1               '  CS   => Transfers data to LEDs when Pulsed
        Symbol Dta  = PORTB.2               '  DIN  => Bits are shifted out of this pin

        Dim Led_Thou [9] As Byte            ' For loading Leds needs on for each number display
        Dim Led_Hund [9] As Byte            ' For loading Leds needs on for each number display
        Dim Led_Tens [9] As Byte            ' For loading Leds needs on for each number display
        Dim Led_Unit [9] As Byte            ' For loading Leds needs on for each number display
 
        Dim Thou As Byte                    ' Thousands to display
        Dim Hund As Byte                    ' Hundreds to display
        Dim Tens As Byte                    ' Tens to display
        Dim Unit As Byte                    ' Units to display
       
        Dim Dig_1 As Byte   : Dim Dig_2 As Byte : Dim Dig_3 As Byte : Dim Dig_4 As Byte

        Dim My_Count As Word          ' 0 to 9999

        Dim My_Cdata As 5999          ' Adress for storage bytes for screen numbers
       
        Dim I       As Byte
        Dim REG     As Byte           ' For loading to MAX7219
        Dim Value   As Byte           ' For loading to MAX7219




'****************************************************
'****************    Main                 *********
'****************************************************
 Main:
 

    '*******************************************
    '*********        Init 7219         ********
    '*******************************************

    REG         = 9                         ; BCD
    Value       = 0                         ; no BCD
    GoSub       TRANSFERT

    REG         = 10                        ;light display
    Value       = 2                         ;15/32 = 7    - Minimum = 0  -  Maximum = 15
    GoSub       TRANSFERT

    REG         = 11                        ;number of digits use (scan limit)
    Value       = 7                         ;all actif if 7
    GoSub       TRANSFERT
       
    REG         = 12                        ;shut down
    Value       = 1                         ;normal run
    GoSub       TRANSFERT
   
    REG         = 15                        ;test
    Value       = 0                         ;normal mode
    GoSub       TRANSFERT
       
    For REG = 1 To 8                        ' loading "0" anyware display before use
      Value = 0
      GoSub TRANSFERT
    Next

 
    My_Count = 0           ' display "0000" at prog starting

               
'****************************************************
'****************     Loop                  *********
'****************************************************
                          
Again: 

         '                           Example Count = 1024             
            Dig_1 = Dig My_Count , 3            '    x...     =>    Dig_1 = 1        thousand
            Dig_2 = Dig My_Count , 2            '    .x..     =>    Dig_2 = 0        hundred
            Dig_3 = Dig My_Count , 1            '    ..x.     =>    Dig_3 = 2        tens
            Dig_4 = Dig My_Count , 0            '    ...x     =>    Dig_4 = 4        unit


           For I = 1 To 8             ' Loading LEDS on for numbers needs to display
 
                 Led_Thou [I] = CRead ( My_Cdata + (Dig_1 * 8) + I )           
                 Led_Hund [I] = CRead ( My_Cdata + (Dig_2 * 8) + I )           
                 Led_Tens [I] = CRead ( My_Cdata + (Dig_3 * 8) + I )           
                 Led_Unit [I] = CRead ( My_Cdata + (Dig_4 * 8) + I )          

              
           Next I

 
           '*****************************************************************
           '****        Loading LEDS to light to 4 x MAX7219             ****
           '*****************************************************************

            For REG = 1 To 8               ;  loading 8 LEDS lines
           
                SHOut Dta, Clk, MsbFirst, [ REG,Led_Thou[REG] , REG,Led_Hund[REG] , REG,Led_Tens[REG] ,REG,Led_Unit [REG] ]  
                Load = 1
                DelayUS 100
                Load = 0
           
            Next  REG

           DelayMS 300        ' 1 seconde
              
            Inc My_Count

            If My_Count > 9999 Then  My_Count = 0
            
GoTo Again          
 
End:

  
'****************************************************
'***********    Subroutine                  *********
'****************************************************


TRANSFERT:

        SHOut Dta,Clk,MsbFirst,[REG,Value]
        Load = 1
        DelayUS 100
        Load = 0
   
Return



'***********************************************************************************
'******      ASCII bytes for to display numbers to MAX7219                    ******
'***********************************************************************************

Org My_Cdata + 1        ' 5999 + 1

CData   14,17,17,17,17,17,14,0            ' Code for char num 0
CData    4,12, 4, 4, 4, 4,14,0            ' Code for char num 1
CData   14,17, 1, 2, 4, 8,31,0            ' Code for char num 2
CData   31, 2, 4, 2, 1,17,14,0            ' Code for char num 3
CData    2, 6,10,18,31, 2, 2,0            ' Code for char num 4  
CData   31,16,30, 1, 1,17,14,0            ' Code for char num 5  
CData   14,17,16,30,17,17,14,0            ' Code for char num 6
CData   31, 1, 2, 2, 4, 4, 4,0            ' Code for char num 7  
CData   14,17,17,14,17,17,14,0            ' Code for char num 8
CData   14,17,17,15, 1,17,14,0,0          ' Code for char num 9  and one more byte otherwise blocks each time "9" is displayed
    

joesaliba

I wish I am able to convert this Arduino code to PDS for MAX7219: -

https://majicdesigns.github.io/MD_Parola/index.html

It is a great library with a lot of functionality. It would be a super plus for Proton users.

Mapo

"MD_PAROLA" is really beautiful library