import display from pyb import Pin,SPI
class LCDTYPE(object) : ST7735X =0 #屏幕128X160 ST7789X =1 #屏幕240x320 ST7796X =2 #屏幕320x480
class SCREEN: def init(self,lcd_type,width=128,height=160,rotate=0): self.lcd_type=lcd_type self.width=width #定义屏幕显示宽 self.height=height #定义屏幕显示高 self.rotate=rotate self.bgr=0x00 self.lcd = display.SPIDisplay(width=self.width,height=self.height) #self.lcd.init(width=self.width,height=self.height)#使用官方库初始化屏幕,配置显示分辨率 #配置显示屏驱动引脚,用于屏幕配置 self.LCD_CS=Pin('P3', Pin.OUT_PP) self.LCD_RST=Pin('P7', Pin.OUT_PP) self.LCD_RS =Pin('P8', Pin.OUT_PP) self.LCD_BACKLIGHT =Pin('P6', Pin.OUT_PP) self.LCD_BACKLIGHT.high() self.spi2= SPI(2,SPI.MASTER,baudrate=72*1000000,phase=1) if (lcd_type==LCDTYPE.ST7789X): self.bgr=0x00 #bgr颜色顺序 self.write_command(0x11) self.write_command(0x36,0x00) self.write_command(0x3A,0x05) self.write_c(0x11) self.write_c(0x21) #LCD 反色 elif (lcd_type==LCDTYPE.ST7796X): self.bgr=0x48 #bgr颜色顺序 self.write_command(0x11) self.write_command(0x36,0x40|0x08) self.write_command(0x3A,0x05) self.write_c(0x11) #配置显示分辨率,可以根据不同的显示需求任意配置,0,0是屏幕显示起始点(影响显示起始位置),240,320屏幕分辨率 self.set_resolution(0,0,self.width,self.height,self.rotate) self.write_command(0x29)
#功能:屏幕写cmd
def write_c(self,c):
self.LCD_CS.low()
self.LCD_RS.low()
self.spi2.send(c)
self.LCD_CS.high()
#功能:屏幕写data,按byte写
def write_d(self,c):
self.LCD_CS.low()
self.LCD_RS.high()
self.spi2.send(c)
self.LCD_CS.high()
#功能:屏幕配置寄存器,按数组写
def write_command(self,c,*data):
self.write_c(c)
if data:
for d in data:
self.write_d(d)
#功能:屏幕配置分辨率属性
def set_resolution(self,x,y,w,h,dir):
reg_data=0
if dir==0:
reg_data = 0xc0|self.bgr
if dir==1:
reg_data = 0xa0|self.bgr
if dir==2:
reg_data = 0x00|self.bgr
if dir==3:
reg_data = 0x60|self.bgr
self.write_c(0x2a)#设置屏幕长宽像素
self.write_d(int(x/256))
self.write_d(x%256)
self.write_d(int((x+w-1)/256))
self.write_d((x+w-1)%256)
self.write_c(0x2b)
self.write_d(int(y/256))
self.write_d(y%256)
self.write_d(int((y+h-1)/256))
self.write_d((y+h-1)%256)
self.write_c(0x36)
self.write_d(reg_data%256)
self.write_c(0x2C)
def display(self,img):
if(img.width()>=self.width) and (img.height()>=self.height):
x=abs(img.width()-self.width)
y=abs(img.height()-self.height)
lcd_roi=(int(x/2),int(y/2),self.width,self.height)
else:
lcd_roi=(0,0,img.width(),img.height())
self.lcd.write(img,roi=lcd_roi)
#lcd.display(img)screen = SCREEN(lcd_type=2,width=480,height=320,rotate=1)
这个驱动屏幕驱动程序哪里有问题呀,为啥openmv4plus可以正常让屏幕显示画面,但是N6就不行呀,哪里配置错了吗?
1、首先需要打开romfs查看是否有对应模型; 2、无对应模型则需要在工具-机器视觉-开放模型库中下载并导入romfs 3、更新固件到5.0.0版本,4.8.1版本不支持完整的romfs使用
按键的使用请查看英文文档:OpenMV N6 - OpenMV MicroPython 1.28 documentation
import sensor,time from screen import SCREEN,screen from pyb import Pin,SPI
sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) sensor.skip_frames(time=2000) # Wait for settings take effect. clock = time.clock() # Create a clock object to track the FPS.
while True: clock.tick() # Update the FPS clock. img = sensor.snapshot() # Take a picture and return the image. screen.display(img) print(clock.fps()) # Note: OpenMV Cam runs about half as fast when connected # to the IDE. The FPS should increase once disconnected.
上面那个是screen.py是屏幕的驱动程序,下面这个是main.py。商家和我说可以在openmv4plus上运行。为啥我用N6就用不了。应该是哪里没配置好,应该就是个小问题,麻烦您帮我看一下
提供一下LCD显示屏的照片
那我的那个问题是为什么
为什么运行不了会报错
具体什么问题,你一共问了4个不同的问题,请在对应的问题下回复
#LCD显示模块 (SPI接口LCD扩展板, 显示YOLO检测画面) lcd = display.SPIDisplay(width=240, height=320, triple_buffer=True) # 初始化LCD
这段代码默认的分辨率是多少?
是不是168*128
我想改为320*240改怎么改。我现在用的屏幕的分辨率为320*240
image — 机器视觉 — MicroPython 1.22 文档 使用image库将图像缩放到320*240即可: 如此例子将(64,80)缩放到(128,160):
# This work is licensed under the MIT license.
# Copyright (c) 2013-2025 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# LCD Shield Example
#
# Note: To run this example you will need a LCD Shield for your OpenMV Cam.
#
# The LCD shield allows you to view your OpenMV Cam's frame buffer on the go.
import csi
import time
import display
import image
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize((64, 80))
# Initialize the lcd screen.
# Note: A DAC or a PWM backlight controller can be used to control the
# backlight intensity if supported:
# lcd = display.SPIDisplay(backlight=display.DACBacklight(channel=2))
# lcd.backlight(25) # 25% intensity
# Otherwise the default GPIO (on/off) controller is used.
# OpenMV Cam M4/M7/H7/H7 Plus -> DAC and GPIO Support
# OpenMV Cam RT1062 -> GPIO Support
# OpenMV Cam N6 -> PWM and GPIO Support
lcd = display.SPIDisplay(vflip=True, hmirror=True)
clock = time.clock()
while True:
clock.tick()
img = csi0.snapshot()
lcd.write(csi0.snapshot(), hint=image.CENTER | image.SCALE_ASPECT_KEEP)
print(clock.fps())
import display
from pyb import Pin,SPI
class LCDTYPE(object) :
ST7735X =0 #屏幕128X160
ST7789X =1 #屏幕240x320
ST7796X =2 #屏幕320x480
class SCREEN:
def __init__(self,lcd_type,width=128,height=160,rotate=0):
self.lcd_type=lcd_type
self.width=width #定义屏幕显示宽
self.height=height #定义屏幕显示高
self.rotate=rotate
self.bgr=0x00
self.lcd = display.SPIDisplay(width=self.width,height=self.height)
#self.lcd.init(width=self.width,height=self.height)#使用官方库初始化屏幕,配置显示分辨率
#配置显示屏驱动引脚,用于屏幕配置
self.LCD_CS=Pin('P3', Pin.OUT_PP)
self.LCD_RST=Pin('P7', Pin.OUT_PP)
self.LCD_RS =Pin('P8', Pin.OUT_PP)
self.LCD_BACKLIGHT =Pin('P6', Pin.OUT_PP)
self.LCD_BACKLIGHT.high()
self.spi2= SPI(2,SPI.MASTER,baudrate=72*1000000,phase=1)
if (lcd_type==LCDTYPE.ST7789X):
self.bgr=0x00 #bgr颜色顺序
self.write_command(0x11)
self.write_command(0x36,0x00)
self.write_command(0x3A,0x05)
self.write_c(0x11)
self.write_c(0x21) #LCD 反色
elif (lcd_type==LCDTYPE.ST7796X):
self.bgr=0x48 #bgr颜色顺序
self.write_command(0x11)
self.write_command(0x36,0x40|0x08)
self.write_command(0x3A,0x05)
self.write_c(0x11)
#配置显示分辨率,可以根据不同的显示需求任意配置,0,0是屏幕显示起始点(影响显示起始位置),240,320屏幕分辨率
self.set_resolution(0,0,self.width,self.height,self.rotate)
self.write_command(0x29)
#功能:屏幕写cmd
def write_c(self,c):
self.LCD_CS.low()
self.LCD_RS.low()
self.spi2.send(c)
self.LCD_CS.high()
#功能:屏幕写data,按byte写
def write_d(self,c):
self.LCD_CS.low()
self.LCD_RS.high()
self.spi2.send(c)
self.LCD_CS.high()
#功能:屏幕配置寄存器,按数组写
def write_command(self,c,*data):
self.write_c(c)
if data:
for d in data:
self.write_d(d)
#功能:屏幕配置分辨率属性
def set_resolution(self,x,y,w,h,dir):
reg_data=0
if dir==0:
reg_data = 0xc0|self.bgr
if dir==1:
reg_data = 0xa0|self.bgr
if dir==2:
reg_data = 0x00|self.bgr
if dir==3:
reg_data = 0x60|self.bgr
self.write_c(0x2a)#设置屏幕长宽像素
self.write_d(int(x/256))
self.write_d(x%256)
self.write_d(int((x+w-1)/256))
self.write_d((x+w-1)%256)
self.write_c(0x2b)
self.write_d(int(y/256))
self.write_d(y%256)
self.write_d(int((y+h-1)/256))
self.write_d((y+h-1)%256)
self.write_c(0x36)
self.write_d(reg_data%256)
self.write_c(0x2C)
def display(self,img):
if(img.width()>=self.width) and (img.height()>=self.height):
x=abs(img.width()-self.width)
y=abs(img.height()-self.height)
lcd_roi=(int(x/2),int(y/2),self.width,self.height)
else:
lcd_roi=(0,0,img.width(),img.height())
self.lcd.write(img,roi=lcd_roi)
#lcd.display(img)
screen = SCREEN(lcd_type=2,width=480,height=320,rotate=1)
# This work is licensed under the MIT license.
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# Hello World Example
#
# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!
import sensor,time
from screen import SCREEN,screen
from pyb import Pin,SPI
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240)
sensor.skip_frames(time=2000) # Wait for settings take effect.
clock = time.clock() # Create a clock object to track the FPS.
while True:
clock.tick() # Update the FPS clock.
img = sensor.snapshot() # Take a picture and return the image.
screen.display(img)
print(clock.fps()) # Note: OpenMV Cam runs about half as fast when connected
# to the IDE. The FPS should increase once disconnected.
1、OpenMV的例程代码仅支持OpenMV官方扩展板和配件,非OpenMV官方的LCD屏幕无法提供有效支持; 2、N6不使用pyb,N6使用machine库; 3、具体machine库的使用方法具体查看英文文档:OpenMV N6 - OpenMV MicroPython 1.28 documentation