class LcdApi:
def __init__(self, num_lines, num_columns):
self.num_lines = num_lines
self.num_columns = num_columns
self.clear()
def clear(self):
self.buffer = [[' '] * self.num_columns for _ in range(self.num_lines)]
def move_to(self, line, column):
self.line = line
self.column = column
def putchar(self, char):
if char == '\n':
self.line = (self.line + 1) % self.num_lines
self.column = 0
else:
self.buffer[self.line][self.column] = char
self.column = (self.column + 1) % self.num_columns
def putstr(self, string):
for char in string:
self.putchar(char)
def show(self):
for line in self.buffer:
print(''.join(line))
lcd.mo1() # Wait for USB to become ready
print("Hello, Pi Pico!")
import time
from machine import Pin
class GpioLcd(LcdApi):
def __init__(self, rs, en, d4, d5, d6, d7, num_lines=2, num_columns=16):
self.rs = Pin(rs, Pin.OUT)
self.en = Pin(en, Pin.OUT)
self.data_pins = [Pin(d4, Pin.OUT), Pin(d5, Pin.OUT), Pin(d6, Pin.OUT), Pin(d7, Pin.OUT)]
super().__init__(num_lines, num_columns)
self.init_lcd()
def init_lcd(self):
self.send_command(0x33)
self.send_command(0x32)
self.send_command(0x28)
self.send_command(0x0C)
self.send_command(0x06)
self.send_command(0x01)
time.sleep_ms(5)
def pulse_enable(self):
self.en.value(0)
time.sleep_us(1)
self.en.value(1)
time.sleep_us(1)
self.en.value(0)
time.sleep_us(100)
def send_nibble(self, data):
for i in range(4):
self.data_pins[i].value((data >> i) & 1)
self.pulse_enable()
def send_command(self, cmd):
self.rs.value(0)
self.send_nibble(cmd >> 4)
self.send_nibble(cmd & 0x0F)
time.sleep_ms(5)
def send_data(self, data):
self.rs.value(1)
self.send_nibble(data >> 4)
self.send_nibble(data & 0x0F)
time.sleep_us(100)
def putchar(self, char):
self.send_data(ord(char))
super().putchar(char)
def clear(self):
self.send_command(0x01)
super().clear()
def move_to(self, line, column):
addr = 0x80 | (0x40 * line + column)
self.send_command(addr)
super().move_to(line, column)
def show(self):
self.clear()
for line in range(self.num_lines):
self.move_to(line, 0)
for char in self.buffer[line]:
self.putchar(char)
from machine import Pin
import time
from gpio_lcd import GpioLcd
# LCD1602 Pin Konfiguration
rs = 12
en = 11
d4 = 10
d5 = 9
d6 = 8
d7 = 7
# LCD1602 Initialisierung
lcd = GpioLcd(rs, en, d4, d5, d6, d7, 2, 16)
# Beispieltext anzeigen
lcd.putstr("Hallo, Welt!\nMicroPython")
lcd.show()
# Beispiel zum Clearen und Neu-Schreiben
time.sleep(2)
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Neuer Text")
lcd.show()