Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This the code I'm trying to run it with
- import RPi.GPIO as GPIO
- import time
- LCD_RS = 18
- LCD_E = 23
- LCD_D4 = 12
- LCD_D5 = 16
- LCD_D6 = 20
- LCD_D7 = 21
- LCD_WIDTH = 20
- LCD_CHR = True
- LCD_CMD = False
- LCD_LINE_1 = 0x80
- LCD_LINE_2 = 0xC0
- LCD_LINE_3 = 0x94
- LCD_LINE_4 = 0xD4 # Often the start of the fourth line
- E_PULSE = 0.0005
- E_DELAY = 0.0005
- def lcd_init():
- # Initialize GPIO
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(LCD_E, GPIO.OUT)
- GPIO.setup(LCD_RS, GPIO.OUT)
- GPIO.setup(LCD_D4, GPIO.OUT)
- GPIO.setup(LCD_D5, GPIO.OUT)
- GPIO.setup(LCD_D6, GPIO.OUT)
- GPIO.setup(LCD_D7, GPIO.OUT)
- # Power on delay
- time.sleep(0.1)
- # Initialization sequence
- lcd_byte(0x33, LCD_CMD)
- time.sleep(0.005)
- lcd_byte(0x33, LCD_CMD)
- time.sleep(0.005)
- lcd_byte(0x32, LCD_CMD)
- time.sleep(0.005)
- lcd_byte(0x28, LCD_CMD) # 4-bit mode, 2 lines, 5x7 font
- lcd_byte(0x08, LCD_CMD) # Display off, cursor off, blink off
- lcd_byte(0x01, LCD_CMD) # Clear display
- time.sleep(0.005)
- lcd_byte(0x06, LCD_CMD) # Entry mode: increment cursor, no shift
- lcd_byte(0x0C, LCD_CMD) # Display on, cursor off, blink off
- def lcd_byte(bits, mode):
- # Send byte to data pins
- GPIO.output(LCD_RS, mode)
- # High bits
- GPIO.output(LCD_D4, False)
- GPIO.output(LCD_D5, False)
- GPIO.output(LCD_D6, False)
- GPIO.output(LCD_D7, False)
- if bits & 0x10 == 0x10:
- GPIO.output(LCD_D4, True)
- if bits & 0x20 == 0x20:
- GPIO.output(LCD_D5, True)
- if bits & 0x40 == 0x40:
- GPIO.output(LCD_D6, True)
- if bits & 0x80 == 0x80:
- GPIO.output(LCD_D7, True)
- # Toggle Enable pin
- time.sleep(E_DELAY)
- GPIO.output(LCD_E, True)
- time.sleep(E_PULSE)
- GPIO.output(LCD_E, False)
- time.sleep(E_DELAY)
- # Low bits
- GPIO.output(LCD_D4, False)
- GPIO.output(LCD_D5, False)
- GPIO.output(LCD_D6, False)
- GPIO.output(LCD_D7, False)
- if bits & 0x01 == 0x01:
- GPIO.output(LCD_D4, True)
- if bits & 0x02 == 0x02:
- GPIO.output(LCD_D5, True)
- if bits & 0x04 == 0x04:
- GPIO.output(LCD_D6, True)
- if bits & 0x08 == 0x08:
- GPIO.output(LCD_D7, True)
- # Toggle Enable pin
- time.sleep(E_DELAY)
- GPIO.output(LCD_E, True)
- time.sleep(E_PULSE)
- GPIO.output(LCD_E, False)
- time.sleep(E_DELAY)
- def lcd_string(message, line):
- # Send string to LCD at specified line
- message = message.ljust(LCD_WIDTH, " ")
- lcd_byte(line, LCD_CMD)
- for i in range(LCD_WIDTH):
- lcd_byte(ord(message[i]), LCD_CHR)
- def main():
- # Main program block
- lcd_init()
- try:
- lcd_string("Line 1: Hello!", LCD_LINE_1)
- lcd_string("Line 2: From Pi", LCD_LINE_2)
- lcd_string("Line 3: 20x4 LCD", LCD_LINE_3)
- lcd_string("Line 4: Test!", LCD_LINE_4)
- time.sleep(5) # Keep the message displayed for longer
- finally:
- GPIO.cleanup()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement