Skip to content

Instantly share code, notes, and snippets.

@tech2077
Last active October 6, 2015 08:07
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tech2077/2962884 to your computer and use it in GitHub Desktop.
PCA8574 Controlled HD44780 LCD library
'''
Copyright (C) 2012 Matthew Skolaut
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
import smbus
from time import *
# General i2c device class so that other devices can be added easily
class i2c_device:
def __init__(self, addr, port):
self.addr = addr
self.bus = smbus.SMBus(port)
def write(self, byte):
self.bus.write_byte(self.addr, byte)
def read(self):
return self.bus.read_byte(self.addr)
def read_nbytes_data(self, data, n): # For sequential reads > 1 byte
return self.bus.read_i2c_block_data(self.addr, data, n)
class lcd:
#initializes objects and lcd
'''
Reverse Codes:
0: lower 4 bits of expander are commands bits
1: top 4 bits of expander are commands bits AND P0-4 P1-5 P2-6
2: top 4 bits of expander are commands bits AND P0-6 P1-5 P2-4
'''
def __init__(self, addr, port, reverse=0):
self.reverse = reverse
self.lcd_device = i2c_device(addr, port)
if self.reverse:
self.lcd_device.write(0x30)
self.lcd_strobe()
sleep(0.0005)
self.lcd_strobe()
sleep(0.0005)
self.lcd_strobe()
sleep(0.0005)
self.lcd_device.write(0x20)
self.lcd_strobe()
sleep(0.0005)
else:
self.lcd_device.write(0x03)
self.lcd_strobe()
sleep(0.0005)
self.lcd_strobe()
sleep(0.0005)
self.lcd_strobe()
sleep(0.0005)
self.lcd_device.write(0x02)
self.lcd_strobe()
sleep(0.0005)
self.lcd_write(0x28)
self.lcd_write(0x08)
self.lcd_write(0x01)
self.lcd_write(0x06)
self.lcd_write(0x0C)
self.lcd_write(0x0F)
# clocks EN to latch command
def lcd_strobe(self):
if self.reverse == 1:
self.lcd_device.write((self.lcd_device.read() | 0x04))
self.lcd_device.write((self.lcd_device.read() & 0xFB))
elif self.reverse == 2:
self.lcd_device.write((self.lcd_device.read() | 0x01))
self.lcd_device.write((self.lcd_device.read() & 0xFE))
else:
self.lcd_device.write((self.lcd_device.read() | 0x10))
self.lcd_device.write((self.lcd_device.read() & 0xEF))
# write a command to lcd
def lcd_write(self, cmd):
if self.reverse:
self.lcd_device.write((cmd >> 4)<<4)
self.lcd_strobe()
self.lcd_device.write((cmd & 0x0F)<<4)
self.lcd_strobe()
self.lcd_device.write(0x0)
else:
self.lcd_device.write((cmd >> 4))
self.lcd_strobe()
self.lcd_device.write((cmd & 0x0F))
self.lcd_strobe()
self.lcd_device.write(0x0)
# write a character to lcd (or character rom)
def lcd_write_char(self, charvalue):
if self.reverse == 1:
self.lcd_device.write((0x01 | (charvalue >> 4)<<4))
self.lcd_strobe()
self.lcd_device.write((0x01 | (charvalue & 0x0F)<<4))
self.lcd_strobe()
self.lcd_device.write(0x0)
elif self.reverse == 2:
self.lcd_device.write((0x04 | (charvalue >> 4)<<4))
self.lcd_strobe()
self.lcd_device.write((0x04 | (charvalue & 0x0F)<<4))
self.lcd_strobe()
self.lcd_device.write(0x0)
else:
self.lcd_device.write((0x40 | (charvalue >> 4)))
self.lcd_strobe()
self.lcd_device.write((0x40 | (charvalue & 0x0F)))
self.lcd_strobe()
self.lcd_device.write(0x0)
# put char function
def lcd_putc(self, char):
self.lcd_write_char(ord(char))
# put string function
def lcd_puts(self, string, line):
if line == 1:
self.lcd_write(0x80)
if line == 2:
self.lcd_write(0xC0)
if line == 3:
self.lcd_write(0x94)
if line == 4:
self.lcd_write(0xD4)
for char in string:
self.lcd_putc(char)
# clear lcd and set to home
def lcd_clear(self):
self.lcd_write(0x1)
self.lcd_write(0x2)
# add custom characters (0 - 7)
def lcd_load_custon_chars(self, fontdata):
self.lcd_device.bus.write(0x40);
for char in fontdata:
for line in char:
self.lcd_write_char(line)
class tmp102:
def __init__(self, addr, port):
self.sensor = i2c_device(addr, port)
# read a register
def read_reg(self, reg):
return self.sensor.read_nbytes_data(reg, 2)
# read the current temp in celsius
def read_temp(self):
tempraw = self.read_reg(0)
return tempraw[0] + (tempraw[1] >> 4) * 0.0625
@cbaurtx
Copy link

cbaurtx commented Oct 11, 2012

Matthew,

Thank you for the code and also the blog on connecting an LCD to a Raspberry Pi.
For the hardware I mostly followed your blog but did the power supply a little different to
allow for voltage levels within the specification for all parts
With respect to software I took a different route: I installed the i2c drivers and lcdproc. I only use
LCDd and wrote my own client for LCDd.

Would you be interested in combining our work and to submit an article to the Raspberry Pi magazine ?

Christof

@pascalbianca
Copy link

Your script works fine but is it possible to tell us , how we can use own made characters?
The HD44780 lcd's can be programmed in CGRAM from 0 to 7.
In your code, if i read correct, you already have that part in it, but no where i can find a example for it! the code what i mean is.:

/ add custom characters (0 - 7)
def lcd_load_custon_chars(self, fontdata):
self.lcd_device.bus.write(0x40);
for char in fontdata:
for line in char:
self.lcd_write_char(line)

Best Regards.
Pascal.

@jemenake
Copy link

First off, you're not going to get very far with the code as it is in the repository, because there's a bug. Replace:

self.lcd_device.bus.write(0x40)

with

self.lcd_write(0x40)

After that, just keep in mind that characters (on my screen, anyway), are 8 pixels high and 5 pixels wide. That means that each character bitmap will be 8 bytes, with each byte being between 0x00 (bits 0-4 off) and 0x1F (bits 0-4 on).

Arrange your character bitmaps in a larger array and pass the entire array to load_custom_chars(). You can combine characters to make larger icons. It turns out that, if you want a square icon, you can combine 3 characters wide with 2 characters high to get a total of 16 vertical pixels and 15 horizontal pixels. Try the example code below (make sure to set your address, port, and reverse values in the constructor to what you need), and good luck!

mylcd = lcd(0x3F,0,1)
fontdata = [ 
        # Char 0 - Upper-left
        [ 0x00, 0x00, 0x03, 0x04, 0x08, 0x19, 0x11, 0x10 ],
        # Char 1 - Upper-middle
        [ 0x00, 0x1F, 0x00, 0x00, 0x00, 0x11, 0x11, 0x00 ],
        # Char 2 - Upper-right
        [ 0x00, 0x00, 0x18, 0x04, 0x02, 0x13, 0x11, 0x01 ],
        # Char 3 - Lower-left
        [ 0x12, 0x13, 0x1b, 0x09, 0x04, 0x03, 0x00, 0x00 ],
        # Char 4 - Lower-middle
        [ 0x00, 0x11, 0x1f, 0x1f, 0x0e, 0x00, 0x1F, 0x00 ],
        # Char 5 - Lower-right
        [ 0x09, 0x19, 0x1b, 0x12, 0x04, 0x18, 0x00, 0x00 ],
]
# Create logo chars
mylcd.lcd_load_custom_chars(fontdata)

# Write first three chars to row 1
mylcd.lcd_write(0x80)
mylcd.lcd_write_char(0)
mylcd.lcd_write_char(1)
mylcd.lcd_write_char(2)
# Write next three chars to row 2
# If you have a 4-line display, use 0xC0. If you have
# a 2-line display, I think you need 0x94
mylcd.lcd_write(0xC0)
mylcd.lcd_write_char(3)
mylcd.lcd_write_char(4)
mylcd.lcd_write_char(5)

Incidentally, I'm getting ready to submit some big revisions to Matthew. It's got things like thread-safety (so multiple threads can be sending things to the LCD or to other things on the same i2c bus without garbling up the data on the screen), backlight control, easy control of cursor position and blinking, etc. He might not like it, because I changed a lot of stuff around, but let's hope he likes it.

@DieterKoblenz
Copy link

Hi, I want to send stuff to the lcd screen from two threads! How can I do this without crashing the i2c bus?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment