【微雪RP2040双核开发板】使用MicroPython驱动LCD
<div class='showpostmsg'><p>1、在使用C++和MicroPython上,多数都会使用MicroPython,基本上可以秒懂,秒会,秒上手。</p><p></p>
<p>出厂范例如下</p>
<p></p>
<p>2、首先安装Thonny,如上贴,在python安装好之后,只需要执行</p>
<p>pip install thonny</p>
<p>就可以的。然后再命令行直接键入</p>
<p>thonny</p>
<p>可以直接启动开发环境。</p>
<p>3、稍有些麻烦的就是驱动。这个开发板出厂是使用了C++的串口驱动,如果需要microPython解释器,就需要自定义并编译驱动,微雪提供了已经build的二进制文件。在编译的build目录下有多个二进制文件,适合拖曳方式的驱动是用uf2为后缀的文件。</p>
<p>首先,准备把RP2040进入boot模式。标准操作是按着boot键,然后上电,这里是把usb线插入电脑。然后,稍等,看windows自动搜寻到pico的外设并配置对应的驱动,然后就可以看到新增的驱动盘。然后把这个uf2文件直接拖曳到该目录,也就是执行一个copy的命令,就完成了驱动写入pico开发板的过程,</p>
<p></p>
<p>随后pico开发板自举重启,windows需要配置新的驱动,然后,就可以在thonny的右下角,找到新增的pico,这里是在COM5接口</p>
<p></p>
<p>自举后的开发板被识别为FS mode,应该是fast speed 模式,对应usb的FS模式,</p>
<p>4 执行范例代码</p>
<p>对应thonny的界面,开发不要太简单。上面的空间是执行程序块的,写入以py结尾的python文件,按照python的语法写代码,下面的是交互式解释器,就是一句一句的喂RP2040,直接反馈结果。</p>
<p>在上面的代码空间写入代码后,点击save,就提示要写在哪里,是本地计算机还是Raspberry Pico,如果写入Pico板,需要定义一个程序名,要用.py作为后缀,这样一加电就自动执行。这里的程序可以写入多个,从_main()_函数作为入口,自动启动,其他程序可以做为模块编写。</p>
<p>运行起来就是用图形化显示了运动传感器的数据。</p>
<p>显示成功。</p>
<p>附代码如下,‘</p>
<pre>
<code class="language-python">from machine import Pin,I2C,SPI,PWM,ADC
import framebuf
import time
I2C_SDA = 6
I2C_SDL = 7
DC = 8
CS = 9
SCK = 10
MOSI = 11
RST = 12
BL = 25
Vbat_Pin = 29
class LCD_1inch28(framebuf.FrameBuffer):
def __init__(self):
self.width = 240
self.height = 240
self.cs = Pin(CS,Pin.OUT)
self.rst = Pin(RST,Pin.OUT)
self.cs(1)
self.spi = SPI(1,100_000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
self.dc = Pin(DC,Pin.OUT)
self.dc(1)
self.buffer = bytearray(self.height * self.width * 2)
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
self.init_display()
self.red = 0x07E0
self.green = 0x001f
self.blue= 0xf800
self.white = 0xffff
self.fill(self.white)
self.show()
self.pwm = PWM(Pin(BL))
self.pwm.freq(5000)
def write_cmd(self, cmd):
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray())
self.cs(1)
def write_data(self, buf):
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(bytearray())
self.cs(1)
def set_bl_pwm(self,duty):
self.pwm.duty_u16(duty)#max 65535
def init_display(self):
"""Initialize dispaly"""
self.rst(1)
time.sleep(0.01)
self.rst(0)
time.sleep(0.01)
self.rst(1)
time.sleep(0.05)
self.write_cmd(0xEF)
self.write_cmd(0xEB)
self.write_data(0x14)
self.write_cmd(0xFE)
self.write_cmd(0xEF)
self.write_cmd(0xEB)
self.write_data(0x14)
self.write_cmd(0x84)
self.write_data(0x40)
self.write_cmd(0x85)
self.write_data(0xFF)
self.write_cmd(0x86)
self.write_data(0xFF)
self.write_cmd(0x87)
self.write_data(0xFF)
self.write_cmd(0x88)
self.write_data(0x0A)
self.write_cmd(0x89)
self.write_data(0x21)
self.write_cmd(0x8A)
self.write_data(0x00)
self.write_cmd(0x8B)
self.write_data(0x80)
self.write_cmd(0x8C)
self.write_data(0x01)
self.write_cmd(0x8D)
self.write_data(0x01)
self.write_cmd(0x8E)
self.write_data(0xFF)
self.write_cmd(0x8F)
self.write_data(0xFF)
self.write_cmd(0xB6)
self.write_data(0x00)
self.write_data(0x20)
self.write_cmd(0x36)
self.write_data(0x98)
self.write_cmd(0x3A)
self.write_data(0x05)
self.write_cmd(0x90)
self.write_data(0x08)
self.write_data(0x08)
self.write_data(0x08)
self.write_data(0x08)
self.write_cmd(0xBD)
self.write_data(0x06)
self.write_cmd(0xBC)
self.write_data(0x00)
self.write_cmd(0xFF)
self.write_data(0x60)
self.write_data(0x01)
self.write_data(0x04)
self.write_cmd(0xC3)
self.write_data(0x13)
self.write_cmd(0xC4)
self.write_data(0x13)
self.write_cmd(0xC9)
self.write_data(0x22)
self.write_cmd(0xBE)
self.write_data(0x11)
self.write_cmd(0xE1)
self.write_data(0x10)
self.write_data(0x0E)
self.write_cmd(0xDF)
self.write_data(0x21)
self.write_data(0x0c)
self.write_data(0x02)
self.write_cmd(0xF0)
self.write_data(0x45)
self.write_data(0x09)
self.write_data(0x08)
self.write_data(0x08)
self.write_data(0x26)
self.write_data(0x2A)
self.write_cmd(0xF1)
self.write_data(0x43)
self.write_data(0x70)
self.write_data(0x72)
self.write_data(0x36)
self.write_data(0x37)
self.write_data(0x6F)
self.write_cmd(0xF2)
self.write_data(0x45)
self.write_data(0x09)
self.write_data(0x08)
self.write_data(0x08)
self.write_data(0x26)
self.write_data(0x2A)
self.write_cmd(0xF3)
self.write_data(0x43)
self.write_data(0x70)
self.write_data(0x72)
self.write_data(0x36)
self.write_data(0x37)
self.write_data(0x6F)
self.write_cmd(0xED)
self.write_data(0x1B)
self.write_data(0x0B)
self.write_cmd(0xAE)
self.write_data(0x77)
self.write_cmd(0xCD)
self.write_data(0x63)
self.write_cmd(0x70)
self.write_data(0x07)
self.write_data(0x07)
self.write_data(0x04)
self.write_data(0x0E)
self.write_data(0x0F)
self.write_data(0x09)
self.write_data(0x07)
self.write_data(0x08)
self.write_data(0x03)
self.write_cmd(0xE8)
self.write_data(0x34)
self.write_cmd(0x62)
self.write_data(0x18)
self.write_data(0x0D)
self.write_data(0x71)
self.write_data(0xED)
self.write_data(0x70)
self.write_data(0x70)
self.write_data(0x18)
self.write_data(0x0F)
self.write_data(0x71)
self.write_data(0xEF)
self.write_data(0x70)
self.write_data(0x70)
self.write_cmd(0x63)
self.write_data(0x18)
self.write_data(0x11)
self.write_data(0x71)
self.write_data(0xF1)
self.write_data(0x70)
self.write_data(0x70)
self.write_data(0x18)
self.write_data(0x13)
self.write_data(0x71)
self.write_data(0xF3)
self.write_data(0x70)
self.write_data(0x70)
self.write_cmd(0x64)
self.write_data(0x28)
self.write_data(0x29)
self.write_data(0xF1)
self.write_data(0x01)
self.write_data(0xF1)
self.write_data(0x00)
self.write_data(0x07)
self.write_cmd(0x66)
self.write_data(0x3C)
self.write_data(0x00)
self.write_data(0xCD)
self.write_data(0x67)
self.write_data(0x45)
self.write_data(0x45)
self.write_data(0x10)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x00)
self.write_cmd(0x67)
self.write_data(0x00)
self.write_data(0x3C)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x01)
self.write_data(0x54)
self.write_data(0x10)
self.write_data(0x32)
self.write_data(0x98)
self.write_cmd(0x74)
self.write_data(0x10)
self.write_data(0x85)
self.write_data(0x80)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x4E)
self.write_data(0x00)
self.write_cmd(0x98)
self.write_data(0x3e)
self.write_data(0x07)
self.write_cmd(0x35)
self.write_cmd(0x21)
self.write_cmd(0x11)
time.sleep(0.12)
self.write_cmd(0x29)
time.sleep(0.02)
self.write_cmd(0x21)
self.write_cmd(0x11)
self.write_cmd(0x29)
def show(self):
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0xef)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0xEF)
self.write_cmd(0x2C)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(self.buffer)
self.cs(1)
class QMI8658(object):
def __init__(self,address=0X6B):
self._address = address
self._bus = I2C(id=1,scl=Pin(I2C_SDL),sda=Pin(I2C_SDA),freq=100_000)
bRet=self.WhoAmI()
if bRet :
self.Read_Revision()
else :
return NULL
self.Config_apply()
def _read_byte(self,cmd):
rec=self._bus.readfrom_mem(int(self._address),int(cmd),1)
return rec
def _read_block(self, reg, length=1):
rec=self._bus.readfrom_mem(int(self._address),int(reg),length)
return rec
def _read_u16(self,cmd):
LSB = self._bus.readfrom_mem(int(self._address),int(cmd),1)
MSB = self._bus.readfrom_mem(int(self._address),int(cmd)+1,1)
return (MSB << 8) + LSB
def _write_byte(self,cmd,val):
self._bus.writeto_mem(int(self._address),int(cmd),bytes())
def WhoAmI(self):
bRet=False
if (0x05) == self._read_byte(0x00):
bRet = True
return bRet
def Read_Revision(self):
return self._read_byte(0x01)
def Config_apply(self):
# REG CTRL1
self._write_byte(0x02,0x60)
# REG CTRL2 : QMI8658AccRange_8gand QMI8658AccOdr_1000Hz
self._write_byte(0x03,0x23)
# REG CTRL3 : QMI8658GyrRange_512dps and QMI8658GyrOdr_1000Hz
self._write_byte(0x04,0x53)
# REG CTRL4 : No
self._write_byte(0x05,0x00)
# REG CTRL5 : Enable Gyroscope And Accelerometer Low-Pass Filter
self._write_byte(0x06,0x11)
# REG CTRL6 : Disables Motion on Demand.
self._write_byte(0x07,0x00)
# REG CTRL7 : Enable Gyroscope And Accelerometer
self._write_byte(0x08,0x03)
def Read_Raw_XYZ(self):
xyz=
raw_timestamp = self._read_block(0x30,3)
raw_acc_xyz=self._read_block(0x35,6)
raw_gyro_xyz=self._read_block(0x3b,6)
raw_xyz=self._read_block(0x35,12)
timestamp = (raw_timestamp<<16)|(raw_timestamp<<8)|(raw_timestamp)
for i in range(6):
# xyz=(raw_acc_xyz[(i*2)+1]<<8)|(raw_acc_xyz)
# xyz=(raw_gyro_xyz[((i+3)*2)+1]<<8)|(raw_gyro_xyz[(i+3)*2])
xyz = (raw_xyz[(i*2)+1]<<8)|(raw_xyz)
if xyz >= 32767:
xyz = xyz-65535
return xyz
def Read_XYZ(self):
xyz=
raw_xyz=self.Read_Raw_XYZ()
#QMI8658AccRange_8g
acc_lsb_div=(1<<12)
#QMI8658GyrRange_512dps
gyro_lsb_div = 64
for i in range(3):
xyz=raw_xyz/acc_lsb_div#(acc_lsb_div/1000.0)
xyz=raw_xyz*1.0/gyro_lsb_div
return xyz
if __name__=='__main__':
LCD = LCD_1inch28()
LCD.set_bl_pwm(65535)
qmi8658=QMI8658()
Vbat= ADC(Pin(Vbat_Pin))
while(True):
#read QMI8658
xyz=qmi8658.Read_XYZ()
LCD.fill(LCD.white)
LCD.fill_rect(0,0,240,40,LCD.red)
LCD.text("RP2040-LCD-1.28",60,25,LCD.white)
LCD.fill_rect(0,40,240,40,LCD.blue)
LCD.text("Waveshare",80,57,LCD.white)
LCD.fill_rect(0,80,120,120,0x1805)
LCD.text("ACC_X={:+.2f}".format(xyz),20,100-3,LCD.white)
LCD.text("ACC_Y={:+.2f}".format(xyz),20,140-3,LCD.white)
LCD.text("ACC_Z={:+.2f}".format(xyz),20,180-3,LCD.white)
LCD.fill_rect(120,80,120,120,0xF073)
LCD.text("GYR_X={:+3.2f}".format(xyz),125,100-3,LCD.white)
LCD.text("GYR_Y={:+3.2f}".format(xyz),125,140-3,LCD.white)
LCD.text("GYR_Z={:+3.2f}".format(xyz),125,180-3,LCD.white)
LCD.fill_rect(0,200,240,40,0x180f)
reading = Vbat.read_u16()*3.3/65535*2
LCD.text("Vbat={:.2f}".format(reading),80,215,LCD.white)
LCD.show()
time.sleep(0.1)
</code></pre>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>自举后的开发板被识别为FS mode,应该是fast speed 模式,这个是系统问题还是开发板识别问题呢</p>
Jacktang 发表于 2023-1-8 09:00
自举后的开发板被识别为FS mode,应该是fast speed 模式,这个是系统问题还是开发板识别问题呢
<p>这个系统描述问题,应该还是可以正确识别的。后续正常。</p>
页:
[1]