from machine import Pin
import time
class Port:
def __init__(self, pin_nums):
self.pins = [Pin(num, Pin.OUT) for num in pin_nums]
def write(self, value):
for i, p in enumerate(self.pins):
p.value((value >> i) & 1)
def LCD_cmd(RS,E,LCD_Port,cmd):
RS.value(0)
LCD_Port.write(cmd)
E.value(1)
time.sleep(0.005)
E.value(0)
time.sleep(0.005)
def LCD_data(RS,E,LCD_Port,data):
RS.value(1)
LCD_Port.write(data)
E.value(1)
time.sleep(0.005)
E.value(0)
time.sleep(0.005)
def LCD_init(RS,E,LCD_Port):
LCD_cmd(RS,E,LCD_Port,0x38)
LCD_cmd(RS,E,LCD_Port,0x0C)
LCD_cmd(RS,E,LCD_Port,0x01)
LCD_cmd(RS,E,LCD_Port,0x06)
def LCD_Print(RS,E,LCD_Port,string):
for val in string:
LCD_data(RS,E,LCD_Port,ord(val))
def LCD_setCursor(RS,E,LCD_Port,x,y):
if(y==1):
LCD_cmd(RS,E,LCD_Port,0x80 | x)
elif(y==2):
LCD_cmd(RS,E,LCD_Port,0xC0 | x)
LCD_Port = Port([19, 18, 5, 17, 16, 4, 0, 2])
RS = Pin(23,Pin.OUT, value=0)
E = Pin(22,Pin.OUT, value=0)
LCD_init(RS,E,LCD_Port)
LCD_setCursor(RS,E,LCD_Port,0,1)
LCD_Print(RS,E,LCD_Port,f'Hello World')
for i in range(0,10):
LCD_setCursor(RS,E,LCD_Port,0,2)
LCD_Print(RS,E,LCD_Port,f'Counting: {i}')
time.sleep(1)