我想要一个micropython驱动st7920的程序
时间: 2023-09-10 13:14:16 浏览: 141
基于st7920菜单程序
4星 · 用户满意度95%
以下是一个基于Micropython的ST7920驱动程序:
```python
from machine import Pin, SPI
import time
class ST7920:
# Command list
LCD_FUNCTION_SET = 0x30
LCD_FUNCTION_SET_2 = 0x34
LCD_DISPLAY_ON_OFF = 0x08
LCD_ENTRY_MODE = 0x06
LCD_CLEAR_SCREEN = 0x01
LCD_SET_DDRAM_ADDRESS = 0x80
def __init__(self, spi_bus=1, rs=16, rw=17, en=18, cs=5):
self.spi_bus = spi_bus
self.rs = Pin(rs, Pin.OUT)
self.rw = Pin(rw, Pin.OUT)
self.en = Pin(en, Pin.OUT)
self.cs = Pin(cs, Pin.OUT)
self.spi = None
self.init_display()
def init_display(self):
self.spi = SPI(self.spi_bus, baudrate=1000000, polarity=1, phase=1)
self.send_command(self.LCD_FUNCTION_SET)
time.sleep_ms(5)
self.send_command(self.LCD_FUNCTION_SET)
time.sleep_us(200)
self.send_command(self.LCD_FUNCTION_SET)
self.send_command(self.LCD_FUNCTION_SET_2)
self.send_command(self.LCD_ENTRY_MODE)
self.send_command(self.LCD_DISPLAY_ON_OFF)
self.clear_display()
def clear_display(self):
self.send_command(self.LCD_CLEAR_SCREEN)
time.sleep_ms(5)
def send_command(self, cmd):
self.rs.off()
self.rw.off()
self.spi.write(bytearray([cmd]))
self.en.on()
self.en.off()
def send_data(self, data):
self.rs.on()
self.rw.off()
self.spi.write(bytearray([data]))
self.en.on()
self.en.off()
def set_cursor(self, x, y):
if y == 0:
addr = x
else:
addr = 0x40 + x
self.send_command(self.LCD_SET_DDRAM_ADDRESS | addr)
def write_string(self, string):
for char in string:
self.send_data(ord(char))
def write_char(self, char):
self.send_data(char)
```
使用示例:
```python
from st7920 import ST7920
lcd = ST7920()
lcd.write_string("Hello, World!")
lcd.set_cursor(0, 1)
lcd.write_string("ST7920 LCD")
```
阅读全文