Python Programming Controlling PC Parallel Port Data Bits
If one has installed pyparallel from my page Programming the PC Printer Port in Python we saw the 8 LEDs counting in binary. Unfortunately pyparallel has no function to cut on-off individual bits on the data port. I wrote my own as illustrated below.
There are eight functions writeD0(x) - writeD7(x). Simply use "1" to turn on a LED or "0" to turn off. The example will turn on LEDs D0,D1, and D7.
This is useful for connecting differing pieces of hardware to the data port and controlling them individually.
Also see Additional Commands for Py-Parallel.
#!/usr/bin/env python # File count.py # http://www.bristolwatch.com/pport/index.htm # By Lewis Loflin - firstname.lastname@example.org # For use with pyparallel import parallel import time p = parallel.Parallel() # Count 0-255 binary on 8 LEDs for var in range(0,256): p.setData(var) print var time.sleep(.5) #delay 500 mSec. p.setData(0x00) # turn LEDs off print "Good by!" exit
Data Port Bit Functions
#!/usr/bin/env python # File pportbits.py # http://www.bristolwatch.com/pport/index.htm # By Lewis Loflin - email@example.com # Example code to turn ON-OFF individual bits on PC # printer port Db25 pins 2-9. # Must use my version of pyparallel on website for p.data(). # Bitwise AND is used to clear a bit while bitwise OR used to set bit. import parallel p = parallel.Parallel() def writeD0(bit_val): if bit_val == 1: p.setData(p.data() | 1) # set bit 0 else: # clear bit 0 p.setData(p.data() & (255 - 1)) return def writeD1(bit_val): if bit_val == 1: p.setData(p.data() | 2) #set bit 1 else: # clear bit 1 p.setData(p.data() & (255 - 2)) return def writeD2(bit_val): if bit_val == 1: p.setData(p.data() | 4) #set bit 2 else: # clear bit 2 p.setData(p.data() & (255 - 4)) return def writeD3(bit_val): if bit_val == 1: p.setData(p.data() | 8) #set bit 3 else: # clear bit 3 p.setData(p.data() & (255 - 8)) return def writeD4(bit_val): if bit_val == 1: p.setData(p.data() | 16) #set bit 4 else: # clear bit 4 p.setData(p.data() & (255 - 16)) return def writeD5(bit_val): if bit_val == 1: p.setData(p.data() | 32) #set bit 5 else: # clear bit 5 p.setData(p.data() & (255 - 32)) return def writeD6(bit_val): if bit_val == 1: p.setData(p.data() | 64) #set bit 6 else: # clear bit 6 p.setData(p.data() & (255 - 64)) return def writeD7(bit_val): if bit_val == 1: p.setData(p.data() | 128) #set bit 7 else: # clear bit 7 p.setData(p.data() & (255 - 128)) return # convert a 8-bit number (integer) to a binary. # Returns string. # unlike python bin() this doesn't drop leading zeros def convBinary(value): binaryValue = 'b' for x in range(0, 8): temp = value & 0x80 if temp == 0x80: binaryValue = binaryValue + '1' else: binaryValue = binaryValue + '0' value = value << 1 return binaryValue # Set all data port bits to 0 p.setData(0) # LEDs off print "Port data latches =", p.data() # read port data latches - should be 0 # use differing combinations # set bits D0, D1, D7 writeD0(1) writeD1(1) writeD7(1) # Read and print data port: xp = p.data() print "Value of data port =", convBinary(xp), " ", hex(xp) # should be Value of data port = b10000011 0x83 # LEDs connected to port will show 10000011 print 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.
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 firstname.lastname@example.org.
- Introduction to Python Bitwise Operations
- Python Bitwise Operations by Example
- Using the PC Printer Port series:
- Programming the PC Printer Port in Python
- Additional Commands for Py-Parallel
- Controlling Data Bits on the PC Parallel Port
- Connecting Switches to the PC Printer Port with Python
- Reading an Analog Voltage Through the PC Printer Port Part 1
- Reading an Analog Voltage Through the PC Printer Port Part 2
- Controlling a Serial LCD Display on a PC Printer Port with Python
- Serial ADC and LCD Display with PC Printer Port with Python
- Controlling MAX7219 LED Display with PC Printer Port with Python
- MAX7219 8-Digit LED Display and Serial ADC in Python
- Project pages:
- Part 1: Read Arduino with PC Printer Port
- Part 2: Better way to Read Arduino Through the PC Printer Port
- Part 3: Read-Write an Arduino Through a PC Printer Port
- Part 4: Control LCD Display and Arduino from the PC Printer Port
- Arduino sketches needed by programs:
- pportArduino1.ino read only after reset.
- pportArduino2.ino reset once and multiple reads.
- pportArduino3.ino reset once read write Arduino infinite times - multiple commands.
- Related Raspberry Pi projects:
- Connect Serial LCD to Raspberry Pi
- Serial Read from Arduino to Raspberry Pi
- Arduino Raspberry Pi Interface with LCD Display
- Live Linux Distro for Using Printer Port with Electronics
- Using the powerful Rox-Filer system in Linux
- Use FEH under Linux for a Wallpaper Setter
- How to create Symbolic links in Linux
- Printer Port Interfacing Videos:
- Connect Electronics to PC printer Port with Python
- Setup PC Printer Port with Python-Linux
- Use PC Printer Port to Read Analog Voltage
- Read-Write Arduino ADC PWN with Printer Port
- Printer Port to Serial LCD Display
- Connect Arduino to PC Printer Port for advanced control
Printer Port in C
- Exploring Digital Computer Electronics
- Hardware Review Connecting PC Parallel Ports
- Operation TB6600 Stepper Controller with PC Parallel Port
- Build or Buy Parallel Port Breakout Board?
- Build Serial HD44780 LCD Display Connect to Parallel Port
- Presario 1999 CM1001 Gaming Computer Salvage
- Live Test 2002 VIA EPIA-800 Mini ITX Motherboard
- Salvage, Test 2012 AAEON EMB-B75A Industrial Motherboard
- Web Master
- Gen. Electronics
- YouTube Channel
- Arduino Projects
- Raspberry Pi & Linux
- PIC18F2550 in C
- PIC16F628A Assembly
- PICAXE Projects
Web site Copyright Lewis Loflin, All rights reserved.
If using this material on another site, please provide a link back to my site.