MAX7219

Python PC Printer Port Control MAX7219 LED Display

by Lewis Loflin

This program shows how to connect a MAX7219 8-digit LED display to the IBM PC printer port. This program converts an integer to binary coded decimal (BCD) the displays the count on the right four digits of the 8-digit, 7-segment LED display.

The program is written in Python and runs under Linux. To use this one must setup a module pyparallel. How to set this up is on my webpage Programming the PC Printer Port in Python

Here I've used generic MAX7219 modules from Ebay and wire them as shown above.

View DB25 Printer port connector.

switch connections

This also requires that a switch wired like Fig. b above be connected to the DB25 connector pin 10.

The code below can be copy-pasted and saved with a text editor.

8-digit MAX7219

The program is written in Python and runs under Linux. To use this one must setup a module pyparallel. How to set this up is on my webpage Programming the PC Printer Port in Python

#!/usr/bin/env python
# File pport7219.py
# https://www.bristolwatch.com/pport/index.htm
# By Lewis Loflin - lewis@bvu.net
# Must use my version of pyparallel on website for p.data().

# Here we connect a MAX7219 8-digit module to display a 
# count from 0-9999 after each digit converted to BCD format.
# The loop can be ended before count is finished by pressing Sw1.

# Two bytes are shifted in fist being address, second being data.
# Works the same as two 74165 SSRs in series or 16-bits.
# LD "pulseCS()" clocks 16-bit address/data into working registers.

import parallel
import time

p = parallel.Parallel()

# init i/o pins
p.setDataStrobe(0) # Pin 1 use as CLK on MAX7219
p.setAutoFeed(0)   # Pin 14 use as data bit
p.getInError()	   # Pin 15 Input NC
p.setInitOut(1)    # pin 16 NC
p.setSelect(0)     # pin 17 CS connect to LD pin on 
			# MAX7219 LOW to HIGH to LOW


# clock bit into MAX7219
def pulseCLK():
    p.setDataStrobe(1)
   # time.sleep(.01) 
    p.setDataStrobe(0)
    return

# Clock data-location into MAX7219 registers.
def pulseCS():
    p.setSelect(1)
   # time.sleep(.01)
    p.setSelect(0)
    return

# shift byte into MAX7219
# MSB out first!
def ssrOut(value):
    for  x in range(0,8):
        temp = value & 0x80
        if temp == 0x80:
           p.setAutoFeed(1) # set data bit HIGH
        else:
            p.setAutoFeed(0)
        pulseCLK()
        value = value << 0x01 # shift left
        time.sleep(.001)
    p.setDataStrobe(0)
    p.setAutoFeed(0)
    return 

# initialize MAX7219 8 digits BCD
def initMAX7219():
    
    # set decode mode
    ssrOut(0x09) # address
    #	ssrOut(0x00); // no decode
    ssrOut(0xFF) # 4-bit BCD decode eight digits
    pulseCS();

    # set intensity
    ssrOut(0x0A) # address
    ssrOut(0x0D) # 5/32s
    pulseCS()

    # set scan limit 0-7
    ssrOut(0x0B); # address
    # ssrOut(0x07) # 8 digits
    ssrOut(0x03) # 4 digits
    pulseCS()


    # set for normal operation
    ssrOut(0x0C) # address
    # ssrOut(0x00); // Off
    ssrOut(0x01)  # On
    pulseCS()
	# clear to all 0s.
    for x in range(0,9):
        ssrOut(x)
        ssrOut(0)
        pulseCS()
    return


def writeMAX7219(data, location):
    ssrOut(location)
    ssrOut(data)
    pulseCS()
    return


# Sw1 is connected from ground to Db25 pin 10 and pulled high
# by a 10k resistor.
def Sw1():
	return p.getInAcknowledge()  
	# check pin 10 Db25 when pressed returns 0

	
initMAX7219()

# Converts four digits in i into 4 BCD bytes.
# The writes digits to MAX7219.

for i in range(0, 10000):
    j = i
    # get 1st digit j
    print j
    digit = j % 10
    writeMAX7219(digit, 1)
    j = j / 10
    
    digit = j % 10
    writeMAX7219(digit, 2)
    j = j / 10  

    digit = j % 10
    writeMAX7219(digit, 3)
    j = j / 10

    digit = j % 10
    writeMAX7219(digit, 4)
    j = j / 10
    # break "for" loop is Sw1 closed.
    if not Sw1(): # exit loop if pressed
		break 
	
print "Good by!"  

exit

Download pport-1.0.iso from Sourceforge.com then burn to DVD (file size 920 meg.), insert into DVD drive and reboot. Make sure PC is set to boot from DVD ROM.

This is pre-configured by myself to use Python to control the printer port. Python can be run from IDLE or Geany.

All of my PPORT electronics projects will work without installation to a PC.

Programs can be saved to thumb drive in LIVE mode.

Projects

Below are listed a series of projects using pyparallel and electronics. Starting with routines I wrote to aid students I'd advise walking through this in sequence. Have fun and send comments and/or corrections to lewis@bvu.net.

Linux Videos

Printer Port in C


Web site Copyright Lewis Loflin, All rights reserved.
If using this material on another site, please provide a link back to my site.