import time
import RPi.GPIO as GPIO
# GPIO <-> HD44780 LCD ZUORDNUNG
DISPLAY_RS = 2
DISPLAY_E = 3
DISPLAY_DATA4 = 4
DISPLAY_DATA5 = 14
DISPLAY_DATA6 = 15
DISPLAY_DATA7 = 17
DISPLAY_LIGHT = 10
# HD44780 HARDWARE-FACTS
DISPLAY_WIDTH = 16
DISPLAY_LINE_1 = 0x80
DISPLAY_LINE_2 = 0xC0
DISPLAY_CHR = True
DISPLAY_CMD = False
E_PULSE = 0.00005
E_DELAY = 0.00005
# GPIO INIT
GPIO.setmode(GPIO.BCM) # Verwenden der BCM GPIO Zahlen
GPIO.setwarnings(False)
GPIO.setup(DISPLAY_E, GPIO.OUT)
GPIO.setup(DISPLAY_RS, GPIO.OUT)
GPIO.setup(DISPLAY_DATA4, GPIO.OUT)
GPIO.setup(DISPLAY_DATA5, GPIO.OUT)
GPIO.setup(DISPLAY_DATA6, GPIO.OUT)
GPIO.setup(DISPLAY_DATA7, GPIO.OUT)
GPIO.setup(DISPLAY_LIGHT, GPIO.OUT)
# HD44780 senden
def lcd_byte(bits, mode):
GPIO.output(DISPLAY_RS, mode)
GPIO.output(DISPLAY_DATA4, False)
GPIO.output(DISPLAY_DATA5, False)
GPIO.output(DISPLAY_DATA6, False)
GPIO.output(DISPLAY_DATA7, False)
if bits&0x10==0x10:
GPIO.output(DISPLAY_DATA4, True)
if bits&0x20==0x20:
GPIO.output(DISPLAY_DATA5, True)
if bits&0x40==0x40:
GPIO.output(DISPLAY_DATA6, True)
if bits&0x80==0x80:
GPIO.output(DISPLAY_DATA7, True)
time.sleep(E_DELAY)
GPIO.output(DISPLAY_E, True)
time.sleep(E_PULSE)
GPIO.output(DISPLAY_E, False)
time.sleep(E_DELAY)
GPIO.output(DISPLAY_DATA4, False)
GPIO.output(DISPLAY_DATA5, False)
GPIO.output(DISPLAY_DATA6, False)
GPIO.output(DISPLAY_DATA7, False)
if bits&0x01==0x01:
GPIO.output(DISPLAY_DATA4, True)
if bits&0x02==0x02:
GPIO.output(DISPLAY_DATA5, True)
if bits&0x04==0x04:
GPIO.output(DISPLAY_DATA6, True)
if bits&0x08==0x08:
GPIO.output(DISPLAY_DATA7, True)
time.sleep(E_DELAY)
GPIO.output(DISPLAY_E, True)
time.sleep(E_PULSE)
GPIO.output(DISPLAY_E, False)
time.sleep(E_DELAY)
# HD44780 Zeile schreiben
def lcd(zeile,text):
if zeile == 1:
lcd_byte(DISPLAY_LINE_1, DISPLAY_CMD)
if zeile == 2:
lcd_byte(DISPLAY_LINE_2, DISPLAY_CMD)
message = text.ljust(DISPLAY_WIDTH," ")
for i in range(DISPLAY_WIDTH):
lcd_byte(ord(message[i]),DISPLAY_CHR)
# HD44780 initialisiserung und loeschen
def lcd_init():
lcd_byte(0x33,DISPLAY_CMD)
lcd_byte(0x32,DISPLAY_CMD)
lcd_byte(0x28,DISPLAY_CMD)
lcd_byte(0x0C,DISPLAY_CMD)
lcd_byte(0x06,DISPLAY_CMD)
lcd_byte(0x01,DISPLAY_CMD)
# HD44780 Hintergrundbeleuchtung ein oder aus
def lcd_backlight(what):
GPIO.output(DISPLAY_LIGHT, what)
lcd_init()
lcd(1,'Powerdev.de')
lcd(2,'LCD HD44780')
lcd_backlight(True)