User manual
6) PYTHON SCRIPT for PI LCD, PART 2:
########################################################################
#
# LCD2: Learning how to control an LCD module from Pi
#
#
#
########################################################################
import time #for timing delays
import RPi.GPIO as GPIO
#OUTPUTS: map GPIO to LCD lines
LCD_RS = 7 #GPIO7 = Pi pin 26
LCD_E = 8 #GPIO8 = Pi pin 24
LCD_D4 = 17 #GPIO17 = Pi pin 11
LCD_D5 = 18 #GPIO18 = Pi pin 12
LCD_D6 = 27 #GPIO21 = Pi pin 13 (Use 21 on Rev.1 Pi’s)
LCD_D7 = 22 #GPIO22 = Pi pin 15
OUTPUTS = [LCD_RS,LCD_E,LCD_D4,LCD_D5,LCD_D6,LCD_D7]
#INPUTS: map GPIO to Switches
SW1 = 4 #GPIO4 = Pi pin 7
SW2 = 23 #GPIO16 = Pi pin 16
SW3 = 10 #GPIO10 = Pi pin 19
SW4 = 9 #GPIO9 = Pi pin 21
INPUTS = [SW1,SW2,SW3,SW4]
#HD44780 Controller Commands
CLEARDISPLAY = 0x01
SETCURSOR = 0x80
#Line Addresses. (Pick one. Comment out whichever doesn't apply)
#LINE = [0x00,0x40,0x14,0x54] #for 20x4 display
LINE = [0x00,0x40] #for 16x2 display
########################################################################
#
# Low-level routines for configuring the LCD module.
# These routines contain GPIO read/write calls.
#
def InitIO():
#Sets GPIO pins to input & output, as required by LCD board
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
for lcdLine in OUTPUTS:
GPIO.setup(lcdLine, GPIO.OUT)
for switch in INPUTS:
GPIO.setup(switch, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def CheckSwitches():
#Check status of all four switches on the LCD board
#Returns four boolean values as a tuple.