serial LCD display circuit

Raspberry Pi 74164 Serial Shift Register Interface Liquid Crystal Display

by Lewis Loflin

Here we connect my serial LCD display circuit to the Raspberry Pi. This saves the use of GPIO pins for other uses. The routines are as follows:

pulseCLK() used to clock data into the Sn74164.

pulseE() shifts the parallel data in the display. The RS pin determines if the data is a command/address or ASCII code.

ssrWrite(int) uses a "for" loop with pulsCLK() to shift 8-data into the SN74164.

initLCD(), clearLCD(), and Home() are used to setup the operation of the LCD display. They set RS LOW for command mode, use ssrWrite() to clock byte into 74164, the use pulseE() to enter parallel data into display control circuits.

gotoLocation(address) inputs an address in the internal ram of the display at a select location for display. 0x80 is column 0, line 0 (first character line 1) and 0xC0 is column 0 line 1 (first character line 2).

wrtiteString(text-string) is used with the above functions to write an ASCII string to the display. This must proceeded by gotoLocation(address) to set the position pointer in the display for the text.

convBinary(value) is used to convert an 8-bit number to an ASCII string of 1s and 0s. It doesn't drop leading 0s like the python bin(x) function does.

Click for picture RPi 2 GPIO pin connections.


#!/usr/bin/env python
# File rpi_lcd.py

# http://www.bristolwatch.com/pport/index.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



YouTube Videos:
MCP4725 12-Bit DAC Interface to Raspberry Pi
ADS1115 4-Channel ADC Uses I2C with Raspberry Pi
Interface I2C LCD to Raspberry Pi in C
Pulse-Width-Modulation with Raspberry Pi
Using Geany Text editor C Programming
Raspberry Pi Blink Demo
MAX6675 Raspberry Pi Demo

Videos:
Raspberry Pi, Arduino, and Learning Linux
Raspberry PI Arduino Advanced Interface
Tkinter with Raspberry Pi and PCF8591 AD-DA Sensor

Linux Videos

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