LCD1602两行右移显示
时间: 2023-12-12 12:04:20 浏览: 105
LCD1602显示
你可以使用以下代码将LCD1602右移两行显示:
```python
import time
import RPi.GPIO as GPIO
# Define GPIO to LCD mapping
LCD_RS = 7
LCD_E = 8
LCD_D4 = 25
LCD_D5 = 24
LCD_D6 = 23
LCD_D7 = 18
# Define some device constants
LCD_WIDTH = 16 # Maximum characters per line
# Define some device commands
LCD_CLEAR_DISPLAY = 0x01
LCD_RETURN_HOME = 0x02
LCD_ENTRY_MODE_SET = 0x04
LCD_DISPLAY_ON_OFF_CONTROL = 0x08
LCD_CURSOR_DISPLAY_SHIFT = 0x10
LCD_FUNCTION_SET = 0x20
LCD_SET_CGRAM_ADDRESS = 0x40
LCD_SET_DDRAM_ADDRESS = 0x80
# Define some LCD entry mode flags
LCD_ENTRY_RIGHT = 0x00
LCD_ENTRY_LEFT = 0x02
LCD_ENTRY_SHIFT_INCREMENT = 0x01
LCD_ENTRY_SHIFT_DECREMENT = 0x00
# Define some LCD display on/off control flags
LCD_DISPLAY_ON = 0x04
LCD_DISPLAY_OFF = 0x00
LCD_CURSOR_ON = 0x02
LCD_CURSOR_OFF = 0x00
LCD_BLINK_ON = 0x01
LCD_BLINK_OFF = 0x00
# Define some LCD function set flags
LCD_8BIT_MODE = 0x10
LCD_4BIT_MODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10_DOTS = 0x04
LCD_5x8_DOTS = 0x00
def lcd_init():
# Configure GPIO pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(LCD_E, GPIO.OUT)
GPIO.setup(LCD_RS, 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)
# Initialize display
lcd_write(0x33, False) # 110011 Initialise
lcd_write(0x32, False) # 110010 Initialise
lcd_write(LCD_FUNCTION_SET | LCD_4BIT_MODE | LCD_2LINE | LCD_5x8_DOTS, False) # 001010 Function set
lcd_write(LCD_DISPLAY_ON_OFF_CONTROL | LCD_DISPLAY_OFF, False) # 000010 Display off
lcd_write(LCD_CLEAR_DISPLAY, False) # 000001 Clear display
lcd_write(LCD_ENTRY_MODE_SET | LCD_ENTRY_LEFT | LCD_ENTRY_SHIFT_DECREMENT, False) # 000100 Entry mode set
lcd_write(LCD_DISPLAY_ON_OFF_CONTROL | LCD_DISPLAY_ON | LCD_CURSOR_OFF | LCD_BLINK_OFF, False) # 000111 Display on
def lcd_write(bits, char_mode=False):
# Set RS and D4-D7 pins
GPIO.output(LCD_RS, char_mode)
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x10==0x10:
GPIO.output(LCD_D4, True)
if bits&0x20==0x20:
GPIO.output(LCD_D5, True)
if bits&0x40==0x40:
GPIO.output(LCD_D6, True)
if bits&0x80==0x80:
GPIO.output(LCD_D7, True)
# Toggle 'Enable' pin
time.sleep(0.0001)
GPIO.output(LCD_E, True)
time.sleep(0.0001)
GPIO.output(LCD_E, False)
time.sleep(0.0001)
# Set D4-D7 pins
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x01==0x01:
GPIO.output(LCD_D4, True)
if bits&0x02==0x02:
GPIO.output(LCD_D5, True)
if bits&0x04==0x04:
GPIO.output(LCD_D6, True)
if bits&0x08==0x08:
GPIO.output(LCD_D7, True)
# Toggle 'Enable' pin
time.sleep(0.0001)
GPIO.output(LCD_E, True)
time.sleep(0.0001)
GPIO.output(LCD_E, False)
time.sleep(0.0001)
def lcd_display_string(message, line):
# Send string to display
message = message.ljust(LCD_WIDTH," ")
lcd_write(LCD_SET_DDRAM_ADDRESS + line*0x40, False)
for i in range(LCD_WIDTH):
lcd_write(ord(message[i]),True)
if __name__ == '__main__':
try:
lcd_init()
while True:
lcd_display_string("Hello, World!", 0)
lcd_display_string("Right Shifted", 1)
time.sleep(2)
for i in range(LCD_WIDTH):
lcd_display_string(" "*i + "Right Shifted", 1)
time.sleep(0.2)
lcd_write(LCD_CLEAR_DISPLAY, False)
time.sleep(1)
except KeyboardInterrupt:
pass
finally:
lcd_write(LCD_CLEAR_DISPLAY, False)
lcd_write(LCD_DISPLAY_ON_OFF_CONTROL | LCD_DISPLAY_OFF, False)
GPIO.cleanup()
```
这段代码使用了RPi.GPIO库来与树莓派的GPIO引脚进行交互,通过调用`lcd_display_string()`函数来在LCD1602上显示文本。在`lcd_display_string()`函数中,我们使用了`LCD_SET_DDRAM_ADDRESS`命令来设置LCD光标的位置,进而在指定的行上显示文本。
你可以将第15行的`LCD_D7`引脚连接到LCD1602的RS引脚上,第16行的`LCD_D6`引脚连接到LCD1602的E引脚上,第18至21行的`LCD_D4`至`LCD_D7`引脚分别连接到LCD1602的D4至D7引脚上。
阅读全文