from machine import Pin
import time
# Define LCD pin connections
RS = Pin(0, Pin.OUT)
E = Pin(1, Pin.OUT)
D4 = Pin(2, Pin.OUT)
D5 = Pin(3, Pin.OUT)
D6 = Pin(4, Pin.OUT)
D7 = Pin(5, Pin.OUT)
# LCD Commands
LCD_SET_CGRAM_ADDR = 0x40
LCD_SET_DDRAM_ADDR = 0x80
# Initialize LCD
def lcd_send_command(cmd):
RS.value(0) # Command mode
_lcd_send(cmd)
def lcd_send_data(data):
RS.value(1) # Data mode
_lcd_send(data)
def _lcd_send(value):
# Send high nibble
D4.value((value >> 4) & 1)
D5.value((value >> 5) & 1)
D6.value((value >> 6) & 1)
D7.value((value >> 7) & 1)
E.value(1)
time.sleep_us(1)
E.value(0)
# Send low nibble
D4.value(value & 1)
D5.value((value >> 1) & 1)
D6.value((value >> 2) & 1)
D7.value((value >> 3) & 1)
E.value(1)
time.sleep_us(1)
E.value(0)
time.sleep_ms(2)
def lcd_init():
time.sleep_ms(15)
lcd_send_command(0x28) # Function set: 4-bit mode, 2 lines, 5x8 dots
lcd_send_command(0x0C) # Display on, cursor off, blink off
lcd_send_command(0x06) # Entry mode: increment cursor, no shift
lcd_send_command(0x01) # Clear display
time.sleep_ms(2)
def lcd_define_char(location, char_map):
location &= 0x07 # Only 8 locations for custom chars
lcd_send_command(LCD_SET_CGRAM_ADDR | (location * 8))
for byte in char_map:
lcd_send_data(byte)
def lcd_putstr(string):
for char in string:
lcd_send_data(ord(char))
def lcd_set_cursor(line, position):
if line == 0:
lcd_send_command(LCD_SET_DDRAM_ADDR | position)
elif line == 1:
lcd_send_command(LCD_SET_DDRAM_ADDR | (0x40 + position))
# Define the custom character for 'N'
N_char = [
0b10001, # 16x8 pixel representation of the letter 'N'
0b11001,
0b10101,
0b10011,
0b10001,
0b10001,
0b10001,
0b10001
]
# Main program
lcd_init()
lcd_define_char(0, N_char) # Define custom character at location 0
lcd_set_cursor(0, 0) # Set cursor to the first line, first position
lcd_putstr(chr(0)) # Display the custom character
while True:
time.sleep(1)