【得捷电子Follow me第1期】任务2:驱动外设
[复制链接]
本帖最后由 dql2016 于 2023-4-14 16:40 编辑
驱动LED、OLED显示屏、蜂鸣器等外设。
搭配器件:Raspberry Pi Pico w开发板、GROVE SHIELD、GROVE OLED扩展板、GROVE BUZZER扩展板
参考
https://projects.raspberrypi.org/en/projects/sound-machine/2
安装picozero库
点击工具栏->管理包
搜索框输入 ‘picozero’ ,然后安装picozero,安装好效果如下:多了lib目录,picozero驱动库在lib目录里面
驱动LED
from picozero import pico_led
from time import sleep
pico_led.on()
sleep(1)
pico_led.off()
驱动蜂鸣器
from picozero import Speaker
speaker = Speaker(20)#连接蜂鸣器到GP20
speaker.play('c4', 0.5)#播放'c4'时长0.5秒
驱动OLED
在包管理器搜索驱动ssd1306
选择安装micropython-ssd1306,安装完毕,在lib目录可以看到ssd1306.py了
oled驱动基本使用:
from machine import Pin,I2C
from ssd1306 import SSD1306_I2C
i2c=machine.I2C(1, sda=machine.Pin("GP6"), scl=machine.Pin("GP7"), freq=400000)
display = SSD1306_I2C(128, 64, i2c)
display.fill(0)
display.show()
display.text("hello eeworld",5,10)
display.show()
display.poweroff() # power off the display, pixels persist in memory
display.poweron() # power on the display, pixels redrawn
display.contrast(0) # dim
display.contrast(255) # bright
display.invert(1) # display inverted
display.invert(0) # display normal
display.show() # write the contents of the FrameBuffer to display memory
display.fill(0) # fill entire screen with colour=0
display.pixel(0, 10) # get pixel at x=0, y=10
display.pixel(0, 10, 1) # set pixel at x=0, y=10 to colour=1
display.hline(0, 8, 4, 1) # draw horizontal line x=0, y=8, width=4, colour=1
display.vline(0, 8, 4, 1) # draw vertical line x=0, y=8, height=4, colour=1
display.line(0, 0, 127, 63, 1) # draw a line from 0,0 to 127,63
display.rect(10, 10, 107, 43, 1) # draw a rectangle outline 10,10 to 117,53, colour=1
display.fill_rect(10, 10, 107, 43, 1) # draw a solid rectangle 10,10 to 117,53, colour=1
display.text('Hello World', 0, 0, 1) # draw some text at x=0, y=0, colour=1
display.scroll(20, 0) # scroll 20 pixels to the right
# draw another FrameBuffer on top of the current one at the given coordinates
import framebuf
fbuf = framebuf.FrameBuffer(bytearray(8 * 8 * 1), 8, 8, framebuf.MONO_VLSB)
fbuf.line(0, 0, 7, 7, 1)
display.blit(fbuf, 10, 10, 0) # draw on top at x=10, y=10, key=0
display.show()
display.fill(0)
display.fill_rect(0, 0, 32, 32, 1)
display.fill_rect(2, 2, 28, 28, 0)
display.vline(9, 8, 22, 1)
display.vline(16, 2, 22, 1)
display.vline(23, 8, 22, 1)
display.fill_rect(26, 24, 2, 4, 1)
display.text('MicroPython', 40, 0, 1)
display.text('SSD1306', 40, 12, 1)
display.text('OLED 128x64', 40, 24, 1)
display.show()
#raspberry logo
buffer = bytearray(b'\x00\x00\x00\x00\x00\x00\xf0\xf8\x18XX\xd8\x980\xf0\xe0\xf0\x98\x98\xd8X\x18\x18\xf0\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\xf3\xff\xee\xec|?o\xe7\xe7\xef?<|\xec\xde\xfb\xe3\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0e\x7f\xf8??8\xf0\xf8\x18\x0f\x0f\x0e\x98\xf8x8\x1f\xfc\xf1?\x0e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x03\x06\x0e\x0e\x1f?323\x1f\x0f\x0e\x06\x06\x03\x00\x00\x00\x00\x00\x00\x00')
fb = framebuf.FrameBuffer(buffer, 30, 32, framebuf.MVLSB)
display.blit(fb, 10, 10)
display.show()
效果
|