|
本帖最后由 scoopydoo 于 2025-1-21 17:57 编辑
一不做二不休,俺要把手里的屏全都点亮一次!
树莓派 Pico W 链连上 WiFi 可以有很多玩法,俺的另一块 Pico W 被用于遥控俺的工作台照明灯了,用的 MQTT 协议。
老规矩,上代码 main.py:
- import machine
- import network
- import time
- import lcd1602_4bit
- # Configure LED Pin as an output pin
- led = machine.Pin('LED', machine.Pin.OUT)
- # Initialize LCD pins
- RS = machine.Pin(17, machine.Pin.OUT)
- EN = machine.Pin(18, machine.Pin.OUT)
- D4 = machine.Pin(22, machine.Pin.OUT)
- D5 = machine.Pin(21, machine.Pin.OUT)
- D6 = machine.Pin(26, machine.Pin.OUT)
- D7 = machine.Pin(27, machine.Pin.OUT)
- disp = lcd1602_4bit.LCD1602(RS, EN, D4, D5, D6, D7)
- disp.Clear()
- disp.WriteLine('RPi Pico W DHCP', 1)
- disp.WriteLine('Connecting ...', 2)
- disp.CursorBlink()
- # Replace these with your Wi-Fi credentials
- SSID = "your_SSID"
- PASSWORD = "your_password"
- # Connect to Wi-Fi
- wlan = network.WLAN(network.STA_IF)
- wlan.active(True)
- wlan.connect(SSID, PASSWORD)
- # Wait for connection
- while not wlan.isconnected():
- led.toggle()
- time.sleep(0.5)
- # Connected
- led.value(True)
- disp.CursorOff()
- disp.SetCursor(1, 0)
- # Add extra spaces ensure correct display format
- disp.WriteLine(wlan.ifconfig()[0] + 9*" ", 2)
复制代码
手里这个 1602 屏是并口的,在网上找了个 4bit 的驱动代码,基本没改,就加了最后那一小段设置光标位置的代码 lcd1602_4bit.py:
- import utime
- class LCD1602:
-
- def __init__(self, RS, EN, D4, D5, D6, D7):
- self.RS = RS
- self.EN = EN
- self.D4 = D4
- self.D5 = D5
- self.D6 = D6
- self.D7 = D7
- self.Reset()
-
- def Reset(self):
- self.RS.value(0)
- self.WriteCommand(0x03)
- self.WriteCommand(0x03)
- self.WriteCommand(0x03)
-
- #Initialize LCD into 4 bit mode
- self.WriteCommand(0x02)
-
- #Enable 5x7 character mode
- self.WriteCommand(0x28)
-
- #Cursor off
- self.WriteCommand(0x0C)
-
- #Increment cursor
- self.WriteCommand(0x06)
-
- #Clear screen
- self.WriteCommand(0x01)
-
- #Sleep for two mSeconds
- utime.sleep_ms(2)
-
- # Generate EnablePulse
- def EnablePulse(self):
- self.EN.value(1)
- utime.sleep_us(40)
- self.EN.value(0)
- utime.sleep_us(40)
- # Write a byte to LCD
- # Separate into 2 nibbles and then write to LCD
- def WriteByte(self, data):
- self.D4.value((data & 0b00010000) >>4)
- self.D5.value((data & 0b00100000) >>5)
- self.D6.value((data & 0b01000000) >>6)
- self.D7.value((data & 0b10000000) >>7)
- self.EnablePulse()
-
- self.D4.value((data & 0b00000001) >>0)
- self.D5.value((data & 0b00000010) >>1)
- self.D6.value((data & 0b00000100) >>2)
- self.D7.value((data & 0b00001000) >>3)
- self.EnablePulse()
-
- # Write a command to LCD
- def WriteCommand(self, data):
- # Disable Register Select
- self.RS.value(0)
- # Write Command Byte to LCD
- self.WriteByte(data)
-
- # Write a data to LCD
- def WriteData(self, data):
- # Enable Register Select
- self.RS.value(1)
- # Write Command Byte to LCD
- self.WriteByte(data)
- # Disable Register Select
- self.RS.value(0)
-
- # Writes a string into Line 1 or Line2
- def WriteLine(self, string, line_number):
- if(line_number == 1):
- self.WriteCommand(0x80)
- for x in string:
- self.WriteData(ord(x))
- if(line_number == 2):
- self.WriteCommand(0xC0)
- for x in string:
- self.WriteData(ord(x))
- def Clear(self):
- self.WriteCommand(0x01)
- self.WriteCommand(0x02)
- # Clear screen and put the cursor into Home needs longer time
- # Introduce two mSeconds delay
- utime.sleep_ms(2)
- # Cursor On
- def CursorOn(self):
- self.WriteCommand(0x0E)
- # Cursor Blinking
- def CursorBlink(self):
- self.WriteCommand(0x0D)
-
- # Cursor Off
- def CursorOff(self):
- self.WriteCommand(0x0C)
-
- # Set cursor position
- def SetCursor(self, row, col):
- # Row 0 address starts at 0x00, Row 1 address starts at 0x40
- address = col + (0x40 if row else 0x00)
- # Set DDRAM address command
- self.WriteCommand(0x80 | address)
复制代码
|
-
评分
-
1
查看全部评分
-
|