Important links
Other project pages
Report
![](http://www.weebly.com/weebly/images/file_icons/rtf.png)
sd_card__3_.docx | |
File Size: | 331 kb |
File Type: | docx |
Important links
import RPi.GPIO as GPIO
import time
import os
import subprocess
import glob
class HD44780 :
"""initializing the lcd"""
def __init__(self, pin_rs = 22,pin_e = 27, pins_db = [17,4,3,2]) :
"""initialization"""
self.pin_rs = pin_rs
self.pin_e = pin_e
self.pins_db = pins_db
"""setting the pins for lcd"""
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.pin_e,GPIO.OUT) #enable pin
GPIO.setup(self.pin_rs,GPIO.OUT) #rs pin
for pin in self.pins_db:
GPIO.setup(pin,GPIO.OUT) #data pins
"""clearing the lcd"""
self.clear()
"""function for clearing the lcd"""
def clear(self):
""" lcd blank code """
self.cmd(0x33)
self.cmd(0x32)
self.cmd(0x28)
self.cmd(0x0C)
self.cmd(0x06)
self.cmd(0x01)
def cmd(self, bits, char_mode = False) :
"""sending command to lcd"""
time.sleep(0.01)
bits = bin(bits)[2:].zfill(8)
GPIO.output(self.pin_rs,char_mode)
for pin in self.pins_db:
GPIO.output(pin, False)
for i in range(4):
if bits[i] == "1" :
GPIO.output(self.pins_db[::-1][i], True)
GPIO.output(self.pin_e,True)
GPIO.output(self.pin_e, False)
for pin in self.pins_db:
GPIO.output(pin,False)
for i in range(4,8):
if bits[i]=="1":
GPIO.output(self.pins_db[::-1][i-4], True)
GPIO.output(self.pin_e,True)
GPIO.output(self.pin_e, False)
def message(self,text) :
"""send string to lcd. New line wraps to second line"""
for char in text:
if char == '\n':
self.cmd(0xC0) #next line
else:
self.cmd(ord(char), True)
"""here the main program starts"""
if __name__ == '__main__' :
lcd = HD44780()
lcd.message("HELLO") #printing hello
time.sleep(3) #time delay is in seconds
loopi=0 #this variable controls how many times our program will run
"""if you want it to run infinitely you just keep the while loop below as true"""
while(loopi<10):
loopi=loopi+1
lcd.clear()
lcd.message("Welcome")
time.sleep(2)
"""setting up pins for buttons"""
GPIO.setmode(GPIO.BCM)
one = 9
two = 11
three = 5
four = 10
GPIO.setup(one,GPIO.IN,pull_up_down=GPIO.PUD_UP) #PUD_UP will return false when button is pressed
GPIO.setup(two,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(three,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(four,GPIO.IN,pull_up_down=GPIO.PUD_UP)
import time
import os
import subprocess
import glob
class HD44780 :
"""initializing the lcd"""
def __init__(self, pin_rs = 22,pin_e = 27, pins_db = [17,4,3,2]) :
"""initialization"""
self.pin_rs = pin_rs
self.pin_e = pin_e
self.pins_db = pins_db
"""setting the pins for lcd"""
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.pin_e,GPIO.OUT) #enable pin
GPIO.setup(self.pin_rs,GPIO.OUT) #rs pin
for pin in self.pins_db:
GPIO.setup(pin,GPIO.OUT) #data pins
"""clearing the lcd"""
self.clear()
"""function for clearing the lcd"""
def clear(self):
""" lcd blank code """
self.cmd(0x33)
self.cmd(0x32)
self.cmd(0x28)
self.cmd(0x0C)
self.cmd(0x06)
self.cmd(0x01)
def cmd(self, bits, char_mode = False) :
"""sending command to lcd"""
time.sleep(0.01)
bits = bin(bits)[2:].zfill(8)
GPIO.output(self.pin_rs,char_mode)
for pin in self.pins_db:
GPIO.output(pin, False)
for i in range(4):
if bits[i] == "1" :
GPIO.output(self.pins_db[::-1][i], True)
GPIO.output(self.pin_e,True)
GPIO.output(self.pin_e, False)
for pin in self.pins_db:
GPIO.output(pin,False)
for i in range(4,8):
if bits[i]=="1":
GPIO.output(self.pins_db[::-1][i-4], True)
GPIO.output(self.pin_e,True)
GPIO.output(self.pin_e, False)
def message(self,text) :
"""send string to lcd. New line wraps to second line"""
for char in text:
if char == '\n':
self.cmd(0xC0) #next line
else:
self.cmd(ord(char), True)
"""here the main program starts"""
if __name__ == '__main__' :
lcd = HD44780()
lcd.message("HELLO") #printing hello
time.sleep(3) #time delay is in seconds
loopi=0 #this variable controls how many times our program will run
"""if you want it to run infinitely you just keep the while loop below as true"""
while(loopi<10):
loopi=loopi+1
lcd.clear()
lcd.message("Welcome")
time.sleep(2)
"""setting up pins for buttons"""
GPIO.setmode(GPIO.BCM)
one = 9
two = 11
three = 5
four = 10
GPIO.setup(one,GPIO.IN,pull_up_down=GPIO.PUD_UP) #PUD_UP will return false when button is pressed
GPIO.setup(two,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(three,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(four,GPIO.IN,pull_up_down=GPIO.PUD_UP)