【得捷电子Follow me第1期】+2.驱动外设
[复制链接]
本帖最后由 qinyunti 于 2023-4-9 21:00 编辑
转自公众号,欢迎关注
Raspberry Pi Pico w驱动LED (qq.com)
Raspberry Pi Pico w驱动蜂鸣器与音乐演奏 (qq.com)
Raspberry Pi Pico w驱动OLED (qq.com)
LED驱动视频1
63410c1daa2557d5800b62a5965c9758
LED驱动视频2
63410c1daa2557d5800b62a5965c9758
蜂鸣器演奏音乐视频
35f2f52de0786bc893a5cad5650920d6
驱动OLED
b813770803f2c2ddb75f0fee76392349
一.驱动LED
前言:
基于Micropython,能很方便的进行外设的操作,先从最简单的LED点灯开始,
本篇演示使用演示和定时器两种方式实现LED的闪烁控制。
可以看到使用脚本进行开发,非常方便,能进行快速验证,
降低开饭难度,这也是Pico能快速风靡的原因,使得生态快速发展。
LED对应的引脚参见
pico-w-datasheet.pdf
WL_GPIO0
OP connected to user LED
WL_GPIO0 OP connected to user LED
TP5 WL_GPIO0/LED (not recommended to be used)
测试代码参见
raspberry-pi-pico-python-sdk.pdf
的4.1.1. Blinking the LED from Thonny
注意picow的不是和pico的一样,LED不是连接到GPIO25引脚,而是连接到WL_GPIO0,
所以不能通过25号引脚号获取LED。而是通过”LED”获取led,这样是通用的。
1.1闪烁控制
参考前一篇已经演示了LED的驱动
注意picow的LED不是连接到GPIO25引脚,所以不能通过245引脚号获取LED。
而是通过”LED”获取led,这是通用的。
代码1
以下是直接间隔1秒点亮熄灭LED。
from machine import Pin
import time
led = Pin("LED",Pin.OUT)
for i in range(10):
led.value(1)
time.sleep(1)
led.value(0)
time. Sleep(1)
代码2
以下是使用定时器间隔翻转LED。
from machine import Pin, Timer
led = Pin("LED", Pin.OUT)
tim = Timer()
def tick(timer):
global led
led.toggle()
tim.init(freq=2.5, mode=Timer.PERIODIC, callback=tick)
二.驱动蜂鸣器
蜂鸣器手册参见
https://media.digikey.com/pdf/Data%20Sheets/Seeed%20Technology/107020000_Web.pdf
注意下其频率范围
2.1使用PWM驱动蜂鸣器(响度控制)
参考raspberry-pi-pico-python-sdk.pdf的3.8. PWM
我们将蜂鸣器接到如下位置
蜂鸣器的SIG对应GP16
编写如下代码,通过修改占空比实现蜂鸣器响度的控制
# Example using PWM to fade an BEEP.
import time
from machine import Pin, PWM
# Construct PWM object, with BEEP on Pin(16).
pwm = PWM(Pin(16))
# Set the PWM frequency.
pwm.freq(1000)
# Fade the BEEP 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)
运行代码可以听到蜂鸣器响度逐渐变化。
2.2使用蜂鸣器演奏do-re-mi
以国际标准音A-la-440HZ为准:
do的频率为261.6HZ,
re的频率为293.6HZ,
mi的频率为329.6HZ,
fa的频率为349.2HZ,
sol的频率为392HZ,
la的频率为440HZ,
si的频率为493.8HZ。
根据计算高音比低音的比值为1.12,即高音频率是低音频率的2的1/12次方倍,比如do的频率为261.6HZ,则比它高半音的re的频率为261*2^(1/12)=293.6HZ。
这样类比下来,一个八度的音,这样一直乘下来,所得的结果刚好是2。
由于蜂鸣器频率范围最低是300Hz,所以我们升一个八度
Do频率为261.6x2=523.2Hz,其他的类推。
根据该原理设计如下代码
import time
from machine import Pin, PWM
pwm = PWM(Pin(16))
beepfrep = [523,587,659,698,784,880,987]
beepduty = [400,400,400,400,400,400,400]
beeptime = [1,1,1,1,1,1,1]
while(1):
for i in range(7):
pwm.freq(beepfrep[i])
pwm.duty_u16(beepduty[i])
time.sleep(beeptime[i])
pwm.duty_u16(0)
time.sleep(2)
for i in range(len(beepfrep)-1,-1,-1):
pwm.freq(beepfrep[i])
pwm.duty_u16(beepduty[i])
time.sleep(beeptime[i])
pwm.duty_u16(0)
time. Sleep(2)
beepfrep 代表不同频率即代表do-re-mi-fa-sol-la-si,圆整到整数
beepduty 代表不同声音大小255x255最大
beeptime 代表每个音节演奏多少秒
2.3使用蜂鸣器演奏音乐
根据上一篇我们实际实现了do-re-mi的演奏,以及演奏时间的控制,声音大小的控制,以及休止时间的控制。
由以上基础,实际我们就可以实现任意音乐的播放了。
首先要转换音乐为上述的三个表格。
beepfrep beepduty beeptime
2/4拍子 4分音符一拍 每小节2拍
一分钟76拍。
转化为数组如下
import time
from machine import Pin, PWM
pwm = PWM(Pin(16))
DO = 523
RE = 587
MI = 659
FA = 698
_SO = 392
SO = 784
LA = 880
SI = 987
NO = 523
DUTY0 = 0
METERS = 76 #拍数76 1分钟76拍 每拍60/76秒
#2/4 4分音符一拍 每小节2拍
NOTE_4 = (60/METERS) #先定一拍对应的音符 4分音符
NOTE_8 = (NOTE_4/2) # 8分音符 一个下划线
NOTE_16 = (NOTE_8/2) # 16分音符 二个下划线
beepfrep = [DO,RE,MI,DO,
DO,RE,MI,DO,
MI,FA,SO,NO,
MI,FA,SO,NO,
SO,LA,SO,FA,MI,DO,
SO,LA,SO,FA,MI,DO,
DO,_SO,DO,NO,
DO,_SO,DO,NO]
beepduty = [400,400,400,400,
400,400,400,400,
400,400,400,DUTY0,
400,400,400,DUTY0,
400,400,400,400,400,400,
400,400,400,400,400,400,
400,400,400,DUTY0,
400,400,400,DUTY0,
]
beeptime = [NOTE_8,NOTE_8,NOTE_8,NOTE_8,
NOTE_8,NOTE_8,NOTE_8,NOTE_8,
NOTE_8,NOTE_8,NOTE_8,NOTE_8,
NOTE_8,NOTE_8,NOTE_8,NOTE_8,
NOTE_16,NOTE_16,NOTE_16,NOTE_16,NOTE_8,NOTE_8,
NOTE_16,NOTE_16,NOTE_16,NOTE_16,NOTE_8,NOTE_8,
NOTE_8,NOTE_8,NOTE_8,NOTE_16,
NOTE_8,NOTE_8,NOTE_8,NOTE_16,
]
while(1):
for i in range(len(beepfrep)):
pwm.freq(beepfrep[i])
pwm.duty_u16(beepduty[i])
time.sleep(beeptime[i])
三.驱动LED
3.1资料
https://files.seeedstudio.com/wiki/Grove-OLED-Display-0.96-SSD1315-/res/OEL%20Display%20Module.pdf
https://files.seeedstudio.com/wiki/Grove-OLED-Display-0.96-SSD1315-/res/Grove%20-%20OLED%20Display%200.96%20(SSD1315)_v1.0.zip
3.2接线
oled接到I2C0处
3.3准备驱动代码
Thonny中创建ssd1306.py复制上述文件内容保存到pico中。
# 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[N] 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 = [b"\x40", None] # Co=0, D/C#=1
super().__init__(width, height, external_vcc)
def write_cmd(self, cmd):
self.temp[0] = 0x80 # Co=1, D/C#=0
self.temp[1] = cmd
self.i2c.writeto(self.addr, self.temp)
def write_data(self, buf):
self.write_list[1] = 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([cmd]))
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)
3.4测试
复制 下的测试代码
# 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 = 64 # 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()
Thonny中创建oled1.py复制上述文件内容保存到pico中。
运行
可以看到oled显示如下
|