print " Distance : %4.1f cm" % distance
Total of 4 characters with one decimal. The dots and the decimal are parts of the total.
Daniel
Code: Select all
pi@Pi2 ~ $ cat MyLCD.py
import time
import serial
class MyLCD():
def __init__(self):
self.ser = serial.Serial(port='/dev/ttyAMA0',baudrate=9600)
def write(self,val):
self.ser.write(val)
def ClearScreen(self):
self.ser.write('\xFE\x51')
def DisplayOn(self):
self.ser.write('\xFE\x41')
def DisplayOff(self):
self.ser.write('\xFE\x42')
def SetCursor(self,val):
self.ser.write('\xFE\x45')
self.ser.write(chr(val))
def CursorHome(self):
self.ser.write('\xFE\x46')
def UnderlineCursorOn(self):
self.ser.write('\xFE\x47')
def UnderlineCursorOff(self):
self.ser.write('\xFE\x48')
def MoveCursorLeft(self):
self.ser.write('\xFE\x49')
def MoveCursorRight(self):
self.ser.write('\xFE\x4A')
def BlinkingCursorOn(self):
self.ser.write('\xFE\x4B')
def BlinkingCursorOff(self):
self.ser.write('\xFE\x4C')
def Backspace(self):
self.ser.write('\xFE\x4E')
def SetContrast(self,val):
self.ser.write('\xFe\x52')
self.ser.write(chr(val))
def SetBacklight(self,val):
self.ser.write('\xFE\x53')
self.ser.write(chr(val))
def LoadCustomCharacter(self, customChar, val):
self.ser.write('\xFE\x54')
self.ser.write(chr(customChar))
Tlen = len(val)
if Tlen>8:
Tlen=8
for i in range(Tlen):
self.ser.write(chr(val[i]))
for i in range(Tlen,8,1):
self.ser.write(chr(0))
def MoveDisplayLeft(self):
self.ser.write('\xFE\x55')
def MoveDisplayRight(self):
self.ser.write('\xFE\x56')
def ChangeBaudRate(self,val):
self.ser.write('\xFE\x61')
self.ser.write(chr(val))
def ChangeI2CAddress(self,val):
self.ser.write('\xFE\x42')
self.ser.write(chr(val))
def DisplayVersion(self):
self.ser.write('\xFE\x70')
def DisplayBaudRate(self):
self.ser.write('\xFE\x71')
def DisplayI2CAddress(self):
self.ser.write('\xFE\x72')Code: Select all
pi@Pi2 ~ $ cat TestLCD.py
import time
import MyLCD
import signal
import sys
ser = MyLCD.MyLCD()
def signal_handler(signal, frame):
ser.ClearScreen()
ser.ClearScreen()
ser.write('Bye!')
sys.exit(0)
def DisplayCounter(counter):
#specify First Line position 8
ser.SetCursor(8)
ser.write('%-6dcycles' % (counter))
def DisplayTime():
#sepcify third line position 5
ser.SetCursor(0x19)
ser.write(time.strftime("%H:%M:%S"))
#insert control-C handler
signal.signal(signal.SIGINT, signal_handler)
#display start message
ser.ClearScreen()
ser.DisplayOn()
ser.UnderlineCursorOff()
ser.write('LCD DISPLAY Test.')
#move to second line
ser.SetCursor(0x40)
ser.write('From Raspberry Pi')
#create a custom character
#just a horizontal line in middle of character
ser.LoadCustomCharacter(1,[0,0,0,0xff,0xff,0,0,0])
#move to third line
ser.SetCursor(0x14)
for i in range(20):
ser.write(chr(1))
#move to fourth line
ser.SetCursor(0x54)
datestring = time.strftime("%d/%m/%y %H:%M:%S")
ser.write(datestring)
time.sleep(3.0);
ser.ClearScreen()
ser.write('Counter:')
ser.SetCursor(0x14)
ser.write('Time:')
counter = 0
while(True):
DisplayCounter(counter)
DisplayTime()
counter=counter+1
time.sleep(0.5)
Code: Select all
sudo chown .dialout /dev/ttyAMA0
Code: Select all
#!/usr/bin/python
#
# based on code from lrvick and LiquidCrystal
# lrvic - https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py
# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
#
# took from Adafruit github
#https://raw.github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/master/Adafruit_CharLCD/Adafruit_CharLCD.py
#modify By Daniel Perron, January 31, 2014 to implement 74HC595 version using SPI
from time import sleep
import spidev
class LCD_H595():
# commands
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
LCD_FUNCTIONSET = 0x20
LCD_SETCGRAMADDR = 0x40
LCD_SETDDRAMADDR = 0x80
# flags for display entry mode
LCD_ENTRYRIGHT = 0x00
LCD_ENTRYLEFT = 0x02
LCD_ENTRYSHIFTINCREMENT = 0x01
LCD_ENTRYSHIFTDECREMENT = 0x00
# flags for display on/off control
LCD_DISPLAYON = 0x04
LCD_DISPLAYOFF = 0x00
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_BLINKON = 0x01
LCD_BLINKOFF = 0x00
# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00
# flags for function set
LCD_8BITMODE = 0x10
LCD_4BITMODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10DOTS = 0x04
LCD_5x8DOTS = 0x00
def __init__(self, pin_rs=22, pin_e=18,GPIO = None):
# Emulate the old behavior of using RPi.GPIO if we haven't been given
# an explicit GPIO interface to use
if not GPIO:
import RPi.GPIO as GPIO
self.GPIO = GPIO
self.pin_rs = pin_rs
self.pin_e = pin_e
self.GPIO.setmode(GPIO.BOARD)
self.GPIO.setup(self.pin_e, GPIO.OUT)
self.GPIO.setup(self.pin_rs, GPIO.OUT)
self.spi = spidev.SpiDev()
self.spi.open(0,0)
self.write4bits(0x38) # initialization
self.write4bits(0x0E) # initialization
self.write4bits(0x06) # 2 line 5x7 matrix
self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor
self.write4bits(0x06) # shift cursor right
self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF
self.displayfunction = self.LCD_8BITMODE | self.LCD_2LINE | self.LCD_5x8DOTS
""" Initialize to default text direction (for romance languages) """
self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode
self.clear()
def begin(self, cols, lines):
if (lines > 1):
self.numlines = lines
self.displayfunction |= self.LCD_2LINE
self.currline = 0
def home(self):
self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
self.delayMicroseconds(3000) # this command takes a long time!
def clear(self):
self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
self.delayMicroseconds(3000) # 3000 microsecond sleep, clearing the display takes a long time
def setCursor(self, col, row):
self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ]
if ( row > self.numlines ):
row = self.numlines - 1 # we count rows starting w/0
self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
def noDisplay(self):
""" Turn the display off (quickly) """
self.displaycontrol &= ~self.LCD_DISPLAYON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def display(self):
""" Turn the display on (quickly) """
self.displaycontrol |= self.LCD_DISPLAYON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def noCursor(self):
""" Turns the underline cursor on/off """
self.displaycontrol &= ~self.LCD_CURSORON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def cursor(self):
""" Cursor On """
self.displaycontrol |= self.LCD_CURSORON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def noBlink(self):
""" Turn on and off the blinking cursor """
self.displaycontrol &= ~self.LCD_BLINKON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def noBlink(self):
""" Turn on and off the blinking cursor """
self.displaycontrol &= ~self.LCD_BLINKON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def DisplayLeft(self):
""" These commands scroll the display without changing the RAM """
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
def scrollDisplayRight(self):
""" These commands scroll the display without changing the RAM """
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT);
def leftToRight(self):
""" This is for text that flows Left to Right """
self.displaymode |= self.LCD_ENTRYLEFT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode);
def rightToLeft(self):
""" This is for text that flows Right to Left """
self.displaymode &= ~self.LCD_ENTRYLEFT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
def autoscroll(self):
""" This will 'right justify' text from the cursor """
self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
def noAutoscroll(self):
""" This will 'left justify' text from the cursor """
self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
def WriteByte(self, val, char_mode=False):
self.delayMicroseconds(50) # 50 microsecond sleep
self.GPIO.output(self.pin_rs, char_mode)
self.spi.xfer([val])
self.pulseEnable();
if not char_mode:
self.delayMicroseconds(2000) #command ? then 2000 microsecond sleep
def write4bits(self, bits, char_mode=False):
self.WriteByte(bits,char_mode)
def delayMicroseconds(self, microseconds):
seconds = microseconds / float(1000000) # divide microseconds by 1 million for seconds
sleep(seconds)
def pulseEnable(self):
self.GPIO.output(self.pin_e, False)
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
self.GPIO.output(self.pin_e, True)
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
self.GPIO.output(self.pin_e, False)
self.delayMicroseconds(1) # commands need > 37us to settle
def message(self, text):
""" Send string to LCD. Newline wraps to second line"""
for char in text:
if char == '\n':
self.write4bits(0xC0) # next line
else:
self.write4bits(ord(char),True)
if __name__ == '__main__':
lcd = LCD_H595()
lcd.clear()
lcd.message(" LCD 16x2\n SPI + 74HC595")