Python Forum
Python code a Max7219/7221 7 segment driver
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python code a Max7219/7221 7 segment driver
I found my issue with the MAX7219 and it was my not carefully reading the MAX7219/7221 documentation. I was using one single seven segment display for testing. I used a command byte of 0x00 to write to digit0. The correct command for digit0 is 0x01, not 0x00. A command of 0x00 is a NOP command

Posted below is a working Python program which write the time in minutes and seconds to four seven segment displays.
Earlier in this discussion I posted the schematic of my circuit.

Thanks to everyone who responded to my issue.

#!/usr/bin/env python3

import time
import datetime
import RPi.GPIO as GPIO
import os
import spidev
import sys

try:       #general Execeptions
    try:      #Keyboard execption
              #this is used with the except keyboard interrupt at the end of the program
              #this lets a ctrl-c from the keyboard stop the program
        SPI_CE0                           =  8    #  GPIO8 , pin 24 The SPI_CEO has to be defined
        SPI_CE1                           =  7    #  GPIO7 , pin 26 The SPI_CE1 has to be defined (SPITEST)
        GPIO.setmode(GPIO.BCM )    #use GPIO number, not the pin number. (Apparently the adafruit neopixel import already selected BCM mode instead of BOARD mode)
        GPIO.setup(SPI_CE0,GPIO.OUT)  #The SPI_CE0 has to be defined
        GPIO.setup(SPI_CE1,GPIO.OUT)  #The SPI_CE1 has to be defined  (SPITEST)

#the following is some information i gathered on SPI        
#     0    0    0  Clock low  in idle state and data sampled on rising  edge of clock and shifted out on falling edge of clock
#     1    0    1  Clock low  in idle state and data sampled on falling edge of clock and shifted out on rising  edge of clock
#     2    1    0  Clock high in idle state and data sampled on falling edge of clock and shifted out on rising  edge of clock
#     3    1    1  Clock high in idle state and data sampled on rising  edge of clock and shifted out on falling edge ogf clock

#   CPOL sets the idle state of the clock
#   CPHA selects the clock phase
#   A non-inverted clock is low  in idle state
#   An inverted    clock is high in idle state
#   A MAX7219/7221 uses mode = 0

#max_speed_hz value     Speed 
#      125000000        125.0 MHz
#      62500000          62.5 MHz
#      31200000          31.2 MHz
#      15600000          15.6 MHz
#      7800000            7.8 MHz
#      3900000            3.9 MHz
#      1953000         1953.0 KHz           
#      976000           976.0 KHz
#      488000           488.0 KHz
#      244000           244.0 KHz
#      122000           122.0 KHz
#      61000             61.0 KHz
#      30500             30.5 KHz
#      15200             15.2 KHz           
#      7629              7629 Hz,device)
#Refer to MAX7219/7221 datasheet for commands
#Initialize the MAX7219        
        dataout=bytes.fromhex("0F00") #Turn Display Test Off
        dataout=bytes.fromhex("090F") #Decode all Digits 0-3   decode converts decimal number 0-9 in to the appropriate  
        spi.writebytes(dataout)       #                        segments a-g for a given decimal number
        dataout=bytes.fromhex("0A07") #Set intensity level to 10
        dataout=bytes.fromhex("0B03") #Scan Limit 0x0BX3 digits 0-3 

        dataout=bytes.fromhex("0C01") #Set Normal Operation
#the following code writes the current time in minutes and seconds into four seven segment displays
#minutes tens  is digit 0
#minutes units is digit 1
#seconds tens  is digit 2
#seconds units is digit 3        
        while True:
            second = timenow.second
            k=("010"+str(minutet)) #[-4:]
            dataout=bytes.fromhex(k) #write minutes tens to digit 0

            m=("020"+str(minuteu)) #[-4:]
            dataout=bytes.fromhex(m) #write minutes unitss to digit 1
            n=("030"+str(secondt)) #[-4:]
            dataout=bytes.fromhex(n) #write seconds tens to digit 2
            o=("040"+str(secondu)) #[-4:]
            dataout=bytes.fromhex(o) #write seconds units to digit 3
           #The commented code below would be a great way to write all four digits at once instead of writing each digit separately.
           #This works as far as PCI sending 4 command/data pairs with no issue.
           #The issue is that by sending all four command/data byte pairs at once is that the LOAD(cs) strobe occurs
           #at the end of sending the four pairs of bytes and since the MAX7219/7221 is only a 16 bit register, only the last
           #16 bits are saved for display in the MAX7219/7221
           #Data to the MAX7219/7221 can only be sent one command/data byte pair for each SPI write.  This way
           #the strobe generated at the end of the SPI write loads the shifted data in to the internal registers of the MAX7219/721 for display of the data. 

           #spidataout=("010"+str(minutet)) +("020"+str(minuteu)) + ("030"+str(secondt))  +("040"+str(secondu)) [-4:]  
    except KeyboardInterrupt:
        print("Exiting Program because of keyboard termination")
# Ref 69000  ERROR HANDLING---------------------------
except Exception as e:
    exception_type, exception_object, exception_traceback = sys.exc_info() 
    filename = exception_traceback.tb_frame.f_code.co_filename
    line_number = exception_traceback.tb_lineno
    exceptiontypestr = str(exception_type)[8:-2]'%m-%d-%Y %I:%M%p')
    print("error occurred: " , exceptiontypestr, " at linenumber = ",line_number)
    print("program name = ",filename)
Glad to hear. Thamks for the update.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Stepper motor/Easy Driver/HELP Harney 1 1,184 Jul-31-2021, 08:05 AM
Last Post: Harney
Photo Raspberry PI writing to RS485 driver, DIR pin not holding state zazas321 2 1,409 Oct-30-2020, 06:23 AM
Last Post: zazas321
  How to extract MSS (maximum size segment) from a pcap file ? salwa17 0 1,215 Jun-29-2020, 09:06 AM
Last Post: salwa17
  Writing device driver to stop electric supply to any I/O port sumandas89 0 1,327 May-02-2019, 10:22 AM
Last Post: sumandas89
  how to upload a file to my gmail driver after login ?? evilcode1 5 3,222 Nov-06-2018, 07:33 AM
Last Post: evilcode1
  OpenCV, segment image Standard_user 6 6,499 Jan-01-2017, 08:29 PM
Last Post: Standard_user

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020