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)
MSN = (cmd & 0xF0) >> 4
LSN = cmd & 0x0F
LCD_Port.write(MSN)
E.value(1)
time.sleep(0.005)
E.value(0)
time.sleep(0.005)
LCD_Port.write(LSN)
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)
MSN = (data & 0xF0) >> 4
LSN = data & 0x0F
LCD_Port.write(MSN)
E.value(1)
time.sleep(0.005)
E.value(0)
time.sleep(0.005)
LCD_Port.write(LSN)
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,0x32)
LCD_cmd(RS,E,LCD_Port,0x0C)
LCD_cmd(RS,E,LCD_Port,0x01)
time.sleep(0.002) # זמן מחיקה ארוך יותר
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([16, 4, 0, 2]) # D4, D5, D6, D7
RS = Pin(23,Pin.OUT, value=0)
E = Pin(22,Pin.OUT, value=0)
R1 = Pin(16, Pin.OUT)
R2 = Pin(4, Pin.OUT)
R3 = Pin(0, Pin.OUT)
R4 = Pin(2, Pin.OUT)
C1 = Pin(17, Pin.IN)
C2 = Pin(5, Pin.IN)
C3 = Pin(18, Pin.IN)
C4 = Pin(19, Pin.IN)
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(1,10):
LCD_setCursor(RS,E,LCD_Port,0,2)
LCD_Print(RS,E,LCD_Port,f'Counting: {i}')
time.sleep(1)