from machine import Pin, Timer, RTC
import utime
from lcd_api import LcdApi
# Define LCD pin connections
RS = 15
E = 14
D4 = 13
D5 = 12
D6 = 11
D7 = 10
# Define LCD commands
LCD_CLEAR = 0x01
LCD_HOME = 0x02
LCD_ENTRY_MODE = 0x06
LCD_DISPLAY_ON = 0x0C
LCD_FUNCTION_SET = 0x28
#lcd.backlight_on() # Ensure this line is included to turn on the backlight
#lcd.putstr("Hello, World!")
# Initialize LCD pins
rs = Pin(RS, Pin.OUT)
e = Pin(E, Pin.OUT)
d4 = Pin(D4, Pin.OUT)
d5 = Pin(D5, Pin.OUT)
d6 = Pin(D6, Pin.OUT)
d7 = Pin(D7, Pin.OUT)
# Function to send a command to the LCD
def lcd_command(cmd):
rs.value(0)
lcd_write(cmd)
utime.sleep_ms(2)
# Function to send data to the LCD
def lcd_data(data):
rs.value(1)
lcd_write(data)
utime.sleep_ms(2)
# Function to write data to the LCD
def lcd_write(data):
e.value(1)
d4.value((data >> 4) & 1)
d5.value((data >> 3) & 1)
d6.value((data >> 2) & 1)
d7.value((data >> 1) & 1)
e.value(0)
e.value(1)
d4.value(data & 1)
d5.value((data >> 3) & 1)
d6.value((data >> 2) & 1)
d7.value((data >> 1) & 1)
e.value(0)
# Function to initialize the LCD
def lcd_init():
print("Initializing LCD")
lcd_command(LCD_FUNCTION_SET)
lcd_command(LCD_DISPLAY_ON)
lcd_command(LCD_CLEAR)
lcd_command(LCD_ENTRY_MODE)
print("LCD Initialized")
# Function to display a string on the LCD
def lcd_putstr(string):
for char in string:
lcd_data(ord(char))
# Function to update the LCD with date and time
def update_lcd(timer):
# Get current date and time
t = utime.localtime()
date_str = "{:02}/{:02}/{:04}".format(t[2], t[1], t[0])
time_str = "{:02}:{:02}:{:02}".format(t[3], t[4], t[5])
# Display date and time on LCD
print("Updating LCD")
lcd_command(LCD_CLEAR)
lcd_putstr("Date: " + date_str)
lcd_command(0xC0) # Move to second line
lcd_putstr("Time: " + time_str)
print("Updated Time: ",time_str)
# Initialize the LCD
lcd_init()
# Create a timer to update the LCD every second
timer = Timer()
timer.init(period=1000, mode=Timer.PERIODIC, callback=update_lcd)