Raspberry Pi Pico使用Micopython开发
本帖最后由 topdog 于 2021-11-30 00:10 编辑Micopython官方完美支持Raspberry Pi Pico的应用,定期更新固件。大家可以下载好固件再复制安装到pico片上。更方便的方法是使用Thonny IDE自带的固件更新功能。
(1),按住BOOTSEL按键,用microUSB线把pico插入电脑。
(2),打开Thonny IDE,运行解释器,选择Micopython(Raspberry Pi Pico),关闭IDE。
(3),再次打开Thonny IDE,就会弹出下面的对话框,点击安装。这样新的固件就会刷入Pico。
Micopython官方文档,示例。
本例通过闪烁板载第25管脚LED灯和DHT22(AM2302),SSD1306组成桌面气象站来熟悉一下Raspberry Pi Pico使用Micopython开发。
接线图如下:
pico oled dht22
4 SDA
5 SCL
6 DATA
使用Thonny IDE把下面的三个文件保存到Pico片上。
ssd1306.py
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces
from micropython import const
import framebuf
# register definitions
SET_CONTRAST = const(0x81)
SET_ENTIRE_ON = const(0xA4)
SET_NORM_INV = const(0xA6)
SET_DISP = const(0xAE)
SET_MEM_ADDR = const(0x20)
SET_COL_ADDR = const(0x21)
SET_PAGE_ADDR = const(0x22)
SET_DISP_START_LINE = const(0x40)
SET_SEG_REMAP = const(0xA0)
SET_MUX_RATIO = const(0xA8)
SET_COM_OUT_DIR = const(0xC0)
SET_DISP_OFFSET = const(0xD3)
SET_COM_PIN_CFG = const(0xDA)
SET_DISP_CLK_DIV = const(0xD5)
SET_PRECHARGE = const(0xD9)
SET_VCOM_DESEL = const(0xDB)
SET_CHARGE_PUMP = const(0x8D)
# Subclassing FrameBuffer provides support for graphics primitives
# http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
class SSD1306(framebuf.FrameBuffer):
def __init__(self, width, height, external_vcc):
self.width = width
self.height = height
self.external_vcc = external_vcc
self.pages = self.height // 8
self.buffer = bytearray(self.pages * self.width)
super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
self.init_display()
def init_display(self):
for cmd in (
SET_DISP | 0x00,# off
# address setting
SET_MEM_ADDR,
0x00,# horizontal
# resolution and layout
SET_DISP_START_LINE | 0x00,
SET_SEG_REMAP | 0x01,# column addr 127 mapped to SEG0
SET_MUX_RATIO,
self.height - 1,
SET_COM_OUT_DIR | 0x08,# scan from COM to COM0
SET_DISP_OFFSET,
0x00,
SET_COM_PIN_CFG,
0x02 if self.width > 2 * self.height else 0x12,
# timing and driving scheme
SET_DISP_CLK_DIV,
0x80,
SET_PRECHARGE,
0x22 if self.external_vcc else 0xF1,
SET_VCOM_DESEL,
0x30,# 0.83*Vcc
# display
SET_CONTRAST,
0xFF,# maximum
SET_ENTIRE_ON,# output follows RAM contents
SET_NORM_INV,# not inverted
# charge pump
SET_CHARGE_PUMP,
0x10 if self.external_vcc else 0x14,
SET_DISP | 0x01,
):# on
self.write_cmd(cmd)
self.fill(0)
self.show()
def poweroff(self):
self.write_cmd(SET_DISP | 0x00)
def poweron(self):
self.write_cmd(SET_DISP | 0x01)
def contrast(self, contrast):
self.write_cmd(SET_CONTRAST)
self.write_cmd(contrast)
def invert(self, invert):
self.write_cmd(SET_NORM_INV | (invert & 1))
def show(self):
x0 = 0
x1 = self.width - 1
if self.width == 64:
# displays with width of 64 pixels are shifted by 32
x0 += 32
x1 += 32
self.write_cmd(SET_COL_ADDR)
self.write_cmd(x0)
self.write_cmd(x1)
self.write_cmd(SET_PAGE_ADDR)
self.write_cmd(0)
self.write_cmd(self.pages - 1)
self.write_data(self.buffer)
class SSD1306_I2C(SSD1306):
def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False):
self.i2c = i2c
self.addr = addr
self.temp = bytearray(2)
self.write_list = # Co=0, D/C#=1
super().__init__(width, height, external_vcc)
def write_cmd(self, cmd):
self.temp = 0x80# Co=1, D/C#=0
self.temp = cmd
self.i2c.writeto(self.addr, self.temp)
def write_data(self, buf):
self.write_list = buf
self.i2c.writevto(self.addr, self.write_list)
class SSD1306_SPI(SSD1306):
def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
self.rate = 10 * 1024 * 1024
dc.init(dc.OUT, value=0)
res.init(res.OUT, value=0)
cs.init(cs.OUT, value=1)
self.spi = spi
self.dc = dc
self.res = res
self.cs = cs
import time
self.res(1)
time.sleep_ms(1)
self.res(0)
time.sleep_ms(10)
self.res(1)
super().__init__(width, height, external_vcc)
def write_cmd(self, cmd):
self.spi.init(baudrate=self.rate, polarity=0, phase=0)
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray())
self.cs(1)
def write_data(self, buf):
self.spi.init(baudrate=self.rate, polarity=0, phase=0)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(buf)
self.cs(1)
DHT22.py'''
*
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Daniel Perron
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
'''
import utime
import rp2
from rp2 import PIO, asm_pio
from machine import Pin
#
# A B C D E F
# ___ ___ ...
# ____/ \___/ \___/ \
#
# A = start pulse (> 1ms )
# B = 2-40 us
# C = 80 us
# D = 80 us
#
# E and F aredata clock
#
# E = 50 us
# F =26-28 us => 0 70 us => 1
#
@asm_pio(set_init=(PIO.OUT_HIGH),autopush=True, push_thresh=8)
def DHT22_PIO():
# clock set at 500KhzCycle is 2us
# drive output low for at least 20ms
set(y,1) # 0
pull() # 1
mov(x,osr) # 2
set(pindirs,1) # 3 set pin to output
set(pins,0) # 4 set pin low
label ('waitx')
jmp(x_dec,'waitx') # 5 decrement x reg every 32 cycles
set(pindirs,0) # 6 set pin to input
# STATE A. Wait for high at least 80us. max should bevery short
set(x,31) # 7
label('loopA')
jmp(pin,'got_B') # 8
jmp(x_dec,'loopA') # 9
label('Error')
in_(y,1) # 10
jmp('Error') # 11Infinity loop error
# STATE B. Get HIGH pulse. max should be 40us
label('got_B')
set(x,31) # 12
label('loop_B')
jmp(x_dec,'check_B') # 13
jmp('Error') # 14
label('check_B')
jmp(pin,'loop_B') # 15
# STATE C. Get LOW pulse. max should be 80us
set(x,31) # 16
label('loop_C')
jmp(pin,'got_D') # 17
jmp(x_dec,'loop_C') # 18
jmp('Error') # 19
# STATE D. Get HIGH pulse. max should be 80us
label('got_D')
set(x,31) # 20
label('loop_D')
jmp(x_dec,'check_D') # 21
jmp('Error') # 22
label('check_D')
jmp(pin,'loop_D') # 23
# STATE E. Get Low pulse delay. should be around 50us
set(x,31) # 24
label('loop_E')
jmp(pin,'got_F') # 25
jmp(x_dec,'loop_E') # 26
jmp('Error') # 27
# STATE F.
# wait 40 us
label('got_F')
nop() # 28
in_(pins,1) # 29
# now wait for low pulse
set(x,31) # 30
jmp('loop_D') # 31
class DHT22:
def __init__(self,dataPin, powerPin=None,dht11=False,smID=1):
self.dataPin = dataPin
self.powerPin = powerPin
self.dht11 = dht11
self.smID = smID
self.dataPin.init(Pin.IN, Pin.PULL_UP)
if self.powerPin is not None:
self.powerPin.init(Pin.OUT)
self.powerPin.value(0)
self.sm= rp2.StateMachine(self.smID)
def read_array(self):
if self.powerPin is not None:
self.powerPin.value(1)
utime.sleep_ms(800)
utime.sleep_ms(200)
#start state machine
self.sm.init(DHT22_PIO,freq=500000,
set_base=self.dataPin,
in_base=self.dataPin,
jmp_pin=self.dataPin)
if self.dht11:
self.sm.put(10000)
else:
self.sm.put(1000)
self.sm.active(1)
value = []
for i in range(5):
value.append(self.sm.get())
self.sm.active(0)
if self.powerPin is not None:
self.powerPin.value(0)
return value
def read(self):
value = self.read_array()
sumV = 0
for i in range(4):
sumV += value
if (sumV & 0xff) == value:
if self.dht11:
humidity=value & 0x7f
temperature=value
else:
humidity=((value<<8)+ value)/10.0
temperature=(((value &0x7f) << 8)+ value) /10.0
if (value & 0x80) == 0x80:
temperature = -temperature
return temperature, humidity
else:
return None, None
if __name__ == "__main__":
from machine import Pin
from DHT22 import DHT22
import utime
dht_data = Pin(15,Pin.IN,Pin.PULL_UP)
dht_sensor=DHT22(dht_data,Pin(14,Pin.OUT),dht11=False)
while True:
T,H = dht_sensor.read()
if T is None:
print(" sensor error")
else:
print("{:3.1f}'C{:3.1f}%".format(T,H))
#DHT22 not responsive if delay to short
utime.sleep_ms(500)
main.py
from machine import Pin, I2C
from DHT22 import DHT22
import time
from ssd1306 import SSD1306_I2C
import framebuf
import utime
WIDTH= 128
HEIGHT = 64
SCL = 5
SDA = 4
h = [
0x00, 0x00, 0x00, 0x00, 0x07, 0xC0, 0x00, 0x00, 0x07, 0xE0, 0x00, 0x18, 0x04, 0x20, 0x00, 0x18,
0x04, 0x38, 0x00, 0x3C, 0x04, 0x20, 0x00, 0x24, 0x04, 0x30, 0x00, 0x66, 0x04, 0x30, 0x00, 0x24,
0x04, 0x20, 0x00, 0x3C, 0x05, 0xB8, 0x00, 0x00, 0x05, 0xA0, 0x06, 0x00, 0x05, 0xA0, 0x0E, 0x00,
0x05, 0xB0, 0x0B, 0x00, 0x05, 0xA0, 0x19, 0x00, 0x05, 0xB8, 0x11, 0x80, 0x05, 0xA0, 0x11, 0x80,
0x05, 0xA0, 0x1B, 0x00, 0x0D, 0xA0, 0x0E, 0x00, 0x19, 0xB8, 0x00, 0x18, 0x31, 0x8C, 0x00, 0x18,
0x21, 0x84, 0x00, 0x24, 0x67, 0xC6, 0x00, 0x66, 0x47, 0xE2, 0x00, 0x42, 0x47, 0xE2, 0x00, 0x42,
0x47, 0xE2, 0x00, 0x42, 0x47, 0xC6, 0x00, 0x7E, 0x61, 0x84, 0x00, 0x18, 0x30, 0x0C, 0x00, 0x00,
0x18, 0x18, 0x00, 0x00, 0x0F, 0xF0, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
]
buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
humid = bytearray(h)
led = machine.Pin(25,machine.Pin.OUT)
def BlinkLED(timer_one):
led.toggle()
i2c = I2C(0, scl=Pin(SCL), sda=Pin(SDA), freq=200000)
print("Device Address : "+hex(i2c.scan()).upper())
dht22 = DHT22(Pin(6,Pin.IN,Pin.PULL_UP))
display = SSD1306_I2C(WIDTH, HEIGHT, i2c)
led = machine.Pin(25,machine.Pin.OUT)
timer_one = machine.Timer()
timer_one.init(freq=5, mode=machine.Timer.PERIODIC, callback=BlinkLED)
while True:
T, H = dht22.read()
display.fill(0)
fb = framebuf.FrameBuffer(humid, 32, 32, framebuf.MONO_HLSB)
display.blit(fb, 0, 0)
rpi = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)
display.blit(rpi, 0,35)
#display.text(str('H: ' +"{:0.2f}".format(H)+ "%",2),40,5)
display.text("H: " + str(H) + " %",40,5)
#display.text(str('T: ' +"{:0.2f}".format(T)+ "C",2),40,19)
display.text("T: " + str(T) + " C",40,19)
display.fill_rect(100, 18, 4,4, 1)
display.text("Raspberry Pi",35,35)
display.text("Pico",35,50)
display.show()
time.sleep_ms(5000)
最终呈现出来的效果图:
如果遇到奇怪的状态不能使用,小伙伴可以下载附件中的flash_nuke.uf2安装到Pico中,它会对你的闪存进行“深度清理”。你会失去所有的文件夹,但至少你可以恢复它内核后,重新安装MicroPython。
页:
[1]