等待LCD1602准备好的代码
时间: 2023-12-20 15:29:55 浏览: 69
根据LCD1602的“忙”位状态,我们可以编写等待LCD1602准备好的代码。具体实现方法如下所示:
```python
import RPi.GPIO as GPIO
import time
# 定义LCD1602的引脚
LCD_RS = 37
LCD_RW = 35
LCD_E = 33
LCD_D4 = 31
LCD_D5 = 29
LCD_D6 = 23
LCD_D7 = 21
# 初始化GPIO口
def init():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LCD_RS, GPIO.OUT)
GPIO.setup(LCD_RW, GPIO.OUT)
GPIO.setup(LCD_E, GPIO.OUT)
GPIO.setup(LCD_D4, GPIO.OUT)
GPIO.setup(LCD_D5, GPIO.OUT)
GPIO.setup(LCD_D6, GPIO.OUT)
GPIO.setup(LCD_D7, GPIO.OUT)
# 发送指令到LCD1602
def send_command(command):
GPIO.output(LCD_RS, GPIO.LOW)
GPIO.output(LCD_RW, GPIO.LOW)
GPIO.output(LCD_E, GPIO.HIGH)
GPIO.output(LCD_D4, GPIO.HIGH if command & 0x10 else GPIO.LOW)
GPIO.output(LCD_D5, GPIO.HIGH if command & 0x20 else GPIO.LOW)
GPIO.output(LCD_D6, GPIO.HIGH if command & 0x40 else GPIO.LOW)
GPIO.output(LCD_D7, GPIO.HIGH if command & 0x80 else GPIO.LOW)
GPIO.output(LCD_E, GPIO.LOW)
GPIO.output(LCD_E, GPIO.HIGH)
GPIO.output(LCD_D4, GPIO.HIGH if command & 0x01 else GPIO.LOW)
GPIO.output(LCD_D5, GPIO.HIGH if command & 0x02 else GPIO.LOW)
GPIO.output(LCD_D6, GPIO.HIGH if command & 0x04 else GPIO.LOW)
GPIO.output(LCD_D7, GPIO.HIGH if command & 0x08 else GPIO.LOW)
GPIO.output(LCD_E, GPIO.LOW)
# 等待LCD1602准备好
def wait_for_ready():
GPIO.setup(LCD_D7, GPIO.IN)
GPIO.output(LCD_RS, GPIO.LOW)
GPIO.output(LCD_RW, GPIO.HIGH)
while GPIO.input(LCD_D7) == GPIO.HIGH:
time.sleep(0.001)
GPIO.setup(LCD_D7, GPIO.OUT)
# 初始化LCD1602
def init_lcd():
send_command(0x38) # 8位数据接口,2行显示,5x7点阵字符
send_command(0x08) # 关闭显示
send_command(0x01) # 清屏
send_command(0x06) # 光标右移,字符不移动
send_command(0x0C) # 开启显示,不显示光标,不闪烁
# 主函数
if __name__ == '__main__':
init()
init_lcd()
wait_for_ready()
```
阅读全文