1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| import time import smbus BUS = smbus.SMBus(1) LCD_ADDR = 0x27 BLEN = 1 def turn_light(key): global BLEN BLEN = key if key ==1 : BUS.write_byte(LCD_ADDR ,0x08) else: BUS.write_byte(LCD_ADDR ,0x00) def write_word(addr, data): global BLEN temp = data if BLEN == 1: temp |= 0x08 else: temp &= 0xF7 BUS.write_byte(addr ,temp) def send_command(comm): buf = comm & 0xF0 buf |= 0x04 write_word(LCD_ADDR ,buf) time.sleep(0.002) buf &= 0xFB write_word(LCD_ADDR ,buf) buf = (comm & 0x0F) << 4 buf |= 0x04 write_word(LCD_ADDR ,buf) time.sleep(0.002) buf &= 0xFB write_word(LCD_ADDR ,buf) def send_data(data): buf = data & 0xF0 buf |= 0x05 write_word(LCD_ADDR ,buf) time.sleep(0.002) buf &= 0xFB write_word(LCD_ADDR ,buf) buf = (data & 0x0F) << 4 buf |= 0x05 write_word(LCD_ADDR ,buf) time.sleep(0.002) buf &= 0xFB write_word(LCD_ADDR ,buf) def init_lcd(): try: send_command(0x33) time.sleep(0.005) send_command(0x32) time.sleep(0.005) send_command(0x28) time.sleep(0.005) send_command(0x0C) time.sleep(0.005) send_command(0x01) BUS.write_byte(LCD_ADDR ,0x08) except: return False else: return True def clear_lcd(): send_command(0x01) def print_lcd(x, y, str): if x < 0: x = 0 if x > 15: x = 15 if y <0: y = 0 if y > 1: y = 1 addr = 0x80 + 0x40 * y + x send_command(addr) for chr in str: send_data(ord(chr)) if __name__ == '__main__': init_lcd() print_lcd(0, 0, 'Hello, world!')
|