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
#11
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
spi=spidev.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)

        bus=0
        device=1
#the following is some information i gathered on SPI        
#   MODE CPOL CPHA
#     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

        spi.open(bus,device)
        spi.mode=0b00
        spi.max_speed_hz=61000
#Refer to MAX7219/7221 datasheet for commands
#Initialize the MAX7219        
        dataout=bytes.fromhex("0F00") #Turn Display Test Off
        spi.writebytes(dataout)
        
        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
        spi.writebytes(dataout)
       
        dataout=bytes.fromhex("0B03") #Scan Limit 0x0BX3 digits 0-3 
        spi.writebytes(dataout)

        dataout=bytes.fromhex("0C01") #Set Normal Operation
        spi.writebytes(dataout)
#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:
            timenow=datetime.datetime.now()
            minute=timenow.minute
            second = timenow.second
            minutet=int(minute/10)
            minuteu=int(minute-minutet*10)
            secondt=int(second/10)
            secondu=int(second-secondt*10)
            
            spi.open(bus,device)
            spi.mode=0b00
            spi.max_speed_hz=61000               
            k=("010"+str(minutet)) #[-4:]
            dataout=bytes.fromhex(k) #write minutes tens to digit 0
            spi.writebytes(dataout)

            
            m=("020"+str(minuteu)) #[-4:]
            dataout=bytes.fromhex(m) #write minutes unitss to digit 1
            spi.writebytes(dataout)
            
            n=("030"+str(secondt)) #[-4:]
            dataout=bytes.fromhex(n) #write seconds tens to digit 2
            spi.writebytes(dataout)            
            
            o=("040"+str(secondu)) #[-4:]
            dataout=bytes.fromhex(o) #write seconds units to digit 3
            spi.writebytes(dataout)
            spi.close()
            time.sleep(1)            
           #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:]  
           #spidataout=bytes.fromhex(spidataout)
           #spi.writebytes(spidataout)
 
#-----------------------------------------------------    
# Ref 68000  KEYBOARD CTRL-C INTERRUPT HANDLING-------
#-----------------------------------------------------       
    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]
    errortime=datetime.datetime.now().strftime('%m-%d-%Y %I:%M%p')
    print("error occurred: " , exceptiontypestr, " at linenumber = ",line_number)
    print("program name = ",filename)
      
finally:       
        GPIO.cleanup()
Reply
#12
Glad to hear. Thamks for the update.
Reply


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

Forum Jump:

User Panel Messages

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