'''
R1 - 16
R2 - 4
R3 - 0
R4 - 2
C1 - 17
C2 - 5
C3 - 18
C4 - 19
'''
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)
def ReadKey(R1,R2,R3,R4,C1,C2,C3,C4):
R1.value(0)
R2.value(1)
R3.value(1)
R4.value(1)
if C1.value() == 0:
print("Key Pressed: 1")
return '1'
elif C2.value() == 0:
print("Key Pressed: 2")
return '2'
elif C3.value() == 0:
print("Key Pressed: 3")
return '3'
elif C4.value() == 0:
print("Key Pressed: A")
return 'A'
R1.value(1)
R2.value(0)
R3.value(1)
R4.value(1)
if C1.value() == 0:
print("Key Pressed: 4")
return '4'
elif C2.value() == 0:
print("Key Pressed: 5")
return '5'
elif C3.value() == 0:
print("Key Pressed: 6")
return '6'
elif C4.value() == 0:
print("Key Pressed: B")
return 'B'
R1.value(1)
R2.value(1)
R3.value(0)
R4.value(1)
if C1.value() == 0:
print("Key Pressed: 7")
return '7'
elif C2.value() == 0:
print("Key Pressed: 8")
return '8'
elif C3.value() == 0:
print("Key Pressed: 9")
return '9'
elif C4.value() == 0:
print("Key Pressed: C")
return 'C'
R1.value(1)
R2.value(1)
R3.value(1)
R4.value(0)
if C1.value() == 0:
print("Key Pressed: *")
return '*'
elif C2.value() == 0:
print("Key Pressed: 0")
return '0'
elif C3.value() == 0:
print("Key Pressed: #")
return '#'
elif C4.value() == 0:
print("Key Pressed: D")
return 'D'
return None
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)
while True:
key = ReadKey(R1,R2,R3,R4,C1,C2,C3,C4)
LCD_setCursor(RS,E,LCD_Port,0,1)
if(not(key == None)):
LCD_Print(RS,E,LCD_Port,key)