【得捷电子Follow me第1期】任务2:驱动外设
[复制链接]
硬件搭建
由于Grove外设配的线非常长,把这些外设连在一起后看着就脑袋疼。本来想把所有接插件全卸了,设计一个底板,把所有模块全焊在底板上,看着是最好的,但是又失去了灵活搭配和可扩展性,最后想了个折中方案,把所有模块固定在一个底板上,底板在接口就近开孔,线从底板底下走,天线用双面胶粘在底板上,这样不会显得很乱。
驱动LED
导入machine.Pin类就可以控制LED亮灭,为了让LED闪烁起来,可以导入machine.Timer类,用RP2040的硬件定时器来定时触发中断回调函数,从而使LED闪烁起来。
from machine import Pin, Timer
led = Pin("LED", Pin.OUT)
tim = Timer()
def tick(timer):
global led
led.toggle()
tim.init(freq=2, mode=Timer.PERIODIC, callback=tick)
驱动蜂鸣器
蜂鸣器和LED驱动类似,只需要给高低电平即可。
import time, machine
from machine import Pin
buzzer_pin = machine.Pin(20, machine.Pin.OUT)
while True:
buzzer_pin.value(1)
time.sleep(0.5)
buzzer_pin.value(0)
time.sleep(0.5)
也可以用PWM来实现更多可能性。
import time
from machine import Pin, PWM
# Construct PWM object, with LED on Pin(25).
pwm = PWM(Pin(20))
# Set the PWM frequency.
pwm.freq(1000)
# Fade the LED in and out a few times.
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)
驱动OLED显示屏
OLED显示屏使用SSD1315控制器,可以用SSD1306控制器的代码库。代码库可以从GitHub下载,然后把库文件拖到芯片里,就可以在自己的py文件里使用相应功能。
import time
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
# Grove Shield For Pi Pico I2C0
i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
oled.text("Hello World!", 0, 0, 1)
oled.show()
跟着直播绘制图形
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
#Grove Shield for Pi Pico I2C0
i2c = I2C(0, sda = Pin(8), scl = Pin(9), freq = 400000)
oled = SSD1306_I2C(128, 64, i2c)
oled.fill(0)
oled.fill_rect(0, 0, 32, 32, 1)
oled.fill_rect(2, 2, 28, 28, 0)
oled.vline(9, 8, 22, 1)
oled.vline(16, 2, 22, 1)
oled.vline(23, 8, 22, 1)
oled.fill_rect(26, 24, 2, 4, 1)
oled.text('MicroPython', 40, 0, 1)
oled.text('SSD1306', 40, 12 ,1)
oled.text('OLED 128x64', 40, 24, 1)
oled.show()
绘制树莓派
# Display Image & text on I2C driven ssd1306 OLED display
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import framebuf
WIDTH = 128 # oled display width
HEIGHT = 32 # oled display height
i2c = I2C(0) # Init I2C using I2C0 defaults, SCL=Pin(GP9), SDA=Pin(GP8), freq=400000
print("I2C Address : "+hex(i2c.scan()[0]).upper()) # Display device address
print("I2C Configuration: "+str(i2c)) # Display I2C config
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c) # Init oled display
# Raspberry Pi logo as 32x32 bytearray
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")
# Load the raspberry pi logo into the framebuffer (the image is 32x32)
fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)
# Clear the oled display in case it has junk on it.
oled.fill(0)
# Blit the image from the framebuffer to the oled display
oled.blit(fb, 96, 0)
# Add some text
oled.text("Raspberry Pi",5,5)
oled.text("Pico",5,15)
# Finally update the oled display so the image & text is displayed
oled.show()
驱动RP2040内部温度传感器
RP2040内部有温度传感器,可以将ADC输入连接到内部温度传感器,温度转换公式为:
T = 27 - (ADC_voltage - 0.706)/0.001721
实验显示温度跳动比较大,不知道是供电问题还是需要对ADC采集电压进行滤波和处理。
import time
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
#Grove Shield for Pi Pico I2C0
i2c = I2C(0, sda = Pin(8), scl = Pin(9), freq = 400000)
oled = SSD1306_I2C(128, 64, i2c)
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65536)
while True:
reading = sensor_temp.read_u16() * conversion_factor
temp = 27 - (reading - 0.706) / 0.001721
oled.fill(0)
oled.text(f'temp is {temp:.1f}', 0, 12, 1)
oled.show()
time.sleep(0.5)
|