#!/usr/bin/env python # File rpi_lcd.py # http://www.bristolwatch.com/rpi/rpi_arduino2.htm # By Lewis Loflin - lewis@bvu.net # access to GPIO must be through root import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # disable warnings Line1 = 0x80 # location LCD row 0 col 0 or line 1 Line2 = 0xC0 # location LCD row 1 col 0 or line 2 # Bit_out Pin 1-2 SN74164 lcdBit = 18 # CLK Pin 9 SN74164 - clock bit - LOW to HIGH to LOW lcdClk = 23 # RS Pin 4 LCD - LOW command; HIGH ASCII RS = 24 # E Pin 6 LCD - clock data into LCD - HIGH to LOW to HIGH E = 25 GPIO.setup(lcdBit, GPIO.OUT) GPIO.setup(lcdClk, GPIO.OUT) GPIO.setup(RS, GPIO.OUT) GPIO.setup(E, GPIO.OUT) # Setup IO GPIO.output(lcdBit, 0) GPIO.output(lcdClk, 0) GPIO.output(RS, 0) GPIO.output(E, 1) # GPIO.output(RS, 0) def pulseCLK(): GPIO.output(lcdClk, 1) # time.sleep(.01) GPIO.output(lcdClk, 0) return def pulseE(): GPIO.output(E, 0) # time.sleep(.01) GPIO.output(E, 1) return # MSB out first! def ssrWrite(value): for x in range(0,8): temp = value & 0x80 if temp == 0x80: GPIO.output(lcdBit, 1) # data bit HIGH else: GPIO.output(lcdBit, 0) # data bit LOW pulseCLK() value = value << 0x01 # shift left time.sleep(.001) GPIO.output(lcdClk, 0) GPIO.output(lcdBit, 0) return def initLCD(): GPIO.output(RS, 0) # RS LOW ssrWrite(0x38) # setup for 2 lines pulseE() ssrWrite(0x0F) # blinking cursor pulseE() clearLCD() Home() return def clearLCD(): GPIO.output(RS, 0) # RS LOW ssrWrite(0x01) # clear pulseE() return def Home(): GPIO.output(RS, 0) # RS LOW ssrWrite(0x02) # home pulseE() return def gotoLocation(val): GPIO.output(RS, 0) # RS LOW if val < 0x80: val = 0x80 ssrWrite(val) pulseE() return def writeString(myString): i = len(myString) GPIO.output(RS, 1) # RS - HIGH is ASCII for x in range(0,i): y = ord(myString[x]) ssrWrite(y) pulseE() GPIO.output(RS, 0) # RS LOW return # convert an 8-bit number to a binary string def convBinary(value): binaryValue = '0b' for x in range(0,8): temp = value & 0x80 if temp == 0x80: binaryValue = binaryValue + '1' else: binaryValue = binaryValue + '0' value = value << 1 return binaryValue initLCD() Home() # these are sample progems. # display 0-255 in binary on LCD for j in range(0, 256): myStr1 = convBinary(j) writeString(myStr1) time.sleep(.2) Home() # print j in HEX, OCT, base 10 for j in range(0, 256): writeString('J = ' + str(j)) # base 10 gotoLocation(Line1 + 8) writeString('OCT ' + oct(j)) # print octal gotoLocation(Line2) writeString('HEX ' + hex(j)) # print hex time.sleep(.3) Home() GPIO.cleanup() exit