欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

使用Raspberry Pi Pico的微Python教程

最编程 2024-08-09 13:45:09
...

树莓派Pico的mpy例子,写几个作为参考使用:

import machine
import utime

sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)

while True:
    reading = sensor_temp.read_u16() * conversion_factor
    
    temperature = 27 - (reading - 0.706)/0.001721
    print(temperature)
    utime.sleep(2)

打印内置的温度传感器的温度

先初始化读取内部的ADC的值,然后用65535除一下看分度值

下面死循环,读取。

from machine import Pin, Timer

led = Pin(25, Pin.OUT)
tim = Timer()


def tick(timer):
    global led
    led.toggle()


tim.init(freq=2.5, mode=Timer.PERIODIC, callback=tick)
from machine import Pin

p2 = Pin(2, Pin.IN, Pin.PULL_UP)
p2.irq(lambda pin: print("IRQ with flags:", pin.irq().flags()),
Pin.IRQ_FALLING)

使用内部中断,为了方便。

还是点灯

import time
import _thread
import machine
def task(n, delay):
    led = machine.Pin(25, machine.Pin.OUT)
    for i in range(n):
        led.high()
        time.sleep(delay)
        led.low()
        time.sleep(delay)
    print('done')
_thread.start_new_thread(task, (10, 0.5))

这东西是双核的嘛,所以可以使用线程来加速

我们引入微库,然后定义一个任务,后面线程要执行

代码的实现很简单,就是闪烁个LED

下面直接加到线程里面就可以执行了

就是这个

from machine import SPI

spi = SPI(0)
spi = SPI(0, 100_000)
spi = SPI(0, 100_000, polarity=1, phase=1)

spi.write('test')
spi.read(5)

buf = bytearray(3)
spi.write_readinto('out', buf)

SPI在mpy里面分外简单,直接initial。然后读写操作就好了

import time
from machine import Pin, PWM
pwm = PWM(Pin(25))
pwm.freq(1000)
duty = 0
direction = 1
for _ in range(8 * 256):
    duty += direction
    if duty > 255:
        duty = 255
        direction = -1
    elif duty < 0:
        duty = 0
        direction = 1
    pwm.duty_u16(duty * duty)
    time.sleep(0.001)

使用PWM模块实现LED的亮暗变化

先指定引脚,然后是频率,for的时候用了一个占位符

下面的逻辑是实现了,从小到大和从小到大

控制一个WS2812

# 控制WS2812

import array
import time
from machine import Pin
import rp2

# 分配一个管脚
NUM_LEDS = 8


@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24)
def ws2812():
    T1 = 2
    T2 = 5
    T3 = 3
    wrap_target()
    label("bitloop")
    out(x, 1)               .side(0)[T3 - 1]
    jmp(not_x, "do_zero")   .side(1)[T1 - 1]
    jmp("bitloop")          .side(1)[T2 - 1]
    label("do_zero")
    nop()                   .side(0)[T2 - 1]
    wrap()


sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(22))

sm.active(1)

ar = array.array("I", [0 for _ in range(NUM_LEDS)])

for i in range(4 * NUM_LEDS):
    for j in range(NUM_LEDS):
        r = j * 100 // (NUM_LEDS - 1)
        b = 100 - j * 100 // (NUM_LEDS - 1)
        if j != i % NUM_LEDS:
            r >>= 3
            b >>= 3
        ar[j] = r << 16 | b
    sm.put(ar, 8)
    time.sleep_ms(50)

for i in range(24):
    for j in range(NUM_LEDS):
        ar[j] >>= 1
    sm.put(ar, 8)
    time.sleep_ms(50)

用了一点神奇的东西,这个pico有状态引脚

@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24)
def ws2812():
    T1 = 2
    T2 = 5
    T3 = 3
    wrap_target()
    label("bitloop")
    out(x, 1)               .side(0)[T3 - 1]
    jmp(not_x, "do_zero")   .side(1)[T1 - 1]
    jmp("bitloop")          .side(1)[T2 - 1]
    label("do_zero")
    nop()                   .side(0)[T2 - 1]
    wrap()

就是这段,描述了我们ws2812的控制协议

from machine import Pin, I2C

i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=100000) 
i2c.scan()
i2c.writeto(76, b'123')
i2c.readfrom(76, 4)

i2c = I2C(1, scl=Pin(7), sda=Pin(6), freq=100000)
i2c.scan()
i2c.writeto_mem(76, 6, b'456')
i2c.readfrom_mem(76, 6, 4)

I2C普通的demo,设置引脚,freq

然后扫描一下地址,剩下的命令就是读写了,很常见

普通的I2C的屏幕

from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import framebuf

WIDTH = 128
HEIGHT = 32

i2c = I2C(0)
print("I2C Address      : "+hex(i2c.scan()[0]).upper())
print("I2C Configuration: "+str(i2c))


oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)


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")


fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)


oled.fill(0)


oled.blit(fb, 96, 0)
oled.text("Raspberry Pi", 5, 5)
oled.text("Pico", 5, 15)


oled.show()

驱动普通的SSD1306屏幕,驱动是提前移植好的

https://www.raspberrypi.org/documentation/rp2040/getting-started/#rp2040-boards

参考资料在这里

C/C++ SDK

Python SDK

还有很多有趣的代码没有写,参考来自官网资料~

https://datasheets.raspberrypi.org/pico/raspberry-pi-pico-c-sdk.pdf
https://datasheets.raspberrypi.org/pico/raspberry-pi-pico-python-sdk.pdf
https://docs.micropython.org/en/latest/library/_thread.html