简介
Raspberry Pi Pico是具有灵活数字接口的低成本,高性能微控制器板。它集成了Raspberry Pi自己的RP2040微控制器芯片,运行速度高达133 MHz的双核Arm Cortex M0 +处理器,嵌入式264KB SRAM和2MB板载闪存以及26个多功能GPIO引脚。对于软件开发,可以使用Raspberry Pi的C / C ++ SDK或MicroPython。
规格参数
-
双核 Arm Cortex-M0 + @ 133MHz -
芯片内置 264KB SRAM 和 2MB 的板载闪存 -
通过专用 QSPI 总线支持最高 16MB 的片外闪存 -
DMA 控制器 -
30 个 GPIO 引脚,其中 4 个可用作模拟输入 -
2 个 UART、2 个 SPI 控制器和 2 个 I2C 控制器 -
16 个 PWM 通道 -
USB 1.1 主机和设备支持 -
8 个树莓派可编程 I/O(PIO)状态机,用于自定义外围设备支持 -
支持 UF2 的 USB 大容量存储启动模式,用于拖放式编程
引脚图
基础配置
安装micropython
将pico通过usb连接至电脑,此时电脑会多出一个 RPI-RP2的文件夹,文件夹中有一个index.htm 文件,用浏览器打开重定向至树莓派官网,进入micropython模块,下载UF2文件。
将UF2文件拖入Pico的RPI-RP2文件夹,pico将以micropython模式启动。
配置 Thonny IDE
Thonny IDE下载
进入工具->设置-> 解释器,选择MicroPython(Raspberry Pi Pico) 解释器
勾选 视图->文件,可以显示pico中的文件,勾选视图->shell,可以显示pico的命令行模式
此时我们可以开始编写代码,写完后按住ctrl+s 将该文件保存在树莓派PICO上,并命名为main.py ,运行之后:
基础程序:
- 点亮板载led灯
from machine import Pin
if __name__ == '__main__':
LED = Pin(25, Pin.OUT)
LED.value(1)
- 板载led闪烁
from machine import Pin
from utime import sleep
import utime
led = Pin(25, Pin.OUT)
if __name__ == '__main__':
while True:
led.value(1)
utime.sleep_ms(1000)
led.value(0)
utime.sleep_ms(1000)
pico+微雪OLED 1.3
屏幕和oled连接,输入示例代码
from machine import Pin,SPI
import framebuf
import time
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9
class OLED_1inch3(framebuf.FrameBuffer):
def __init__(self):
self.width = 128
self.height = 64
self.cs = Pin(CS,Pin.OUT)
self.rst = Pin(RST,Pin.OUT)
self.cs(1)
self.spi = SPI(1)
self.spi = SPI(1,2000_000)
self.spi = SPI(1,20000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
self.dc = Pin(DC,Pin.OUT)
self.dc(1)
self.buffer = bytearray(self.height * self.width // 8)
super().__init__(self.buffer, self.width, self.height, framebuf.MONO_HMSB)
self.init_display()
self.white = 0xffff
self.balck = 0x0000
def write_cmd(self, cmd):
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
def write_data(self, buf):
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(bytearray([buf]))
self.cs(1)
def init_display(self):
"""Initialize dispaly"""
self.rst(1)
time.sleep(0.001)
self.rst(0)
time.sleep(0.01)
self.rst(1)
self.write_cmd(0xAE)
self.write_cmd(0x00)
self.write_cmd(0x10)
self.write_cmd(0xB0)
self.write_cmd(0xdc)
self.write_cmd(0x00)
self.write_cmd(0x81)
self.write_cmd(0x6f)
self.write_cmd(0x21)
self.write_cmd(0xa0)
self.write_cmd(0xc0)
self.write_cmd(0xa4)
self.write_cmd(0xa6)
self.write_cmd(0xa8)
self.write_cmd(0x3f)
self.write_cmd(0xd3)
self.write_cmd(0x60)
self.write_cmd(0xd5)
self.write_cmd(0x41)
self.write_cmd(0xd9)
self.write_cmd(0x22)
self.write_cmd(0xdb)
self.write_cmd(0x35)
self.write_cmd(0xad)
self.write_cmd(0x8a)
self.write_cmd(0XAF)
def show(self):
self.write_cmd(0xb0)
for page in range(0,64):
self.column = 63 - page
self.write_cmd(0x00 + (self.column & 0x0f))
self.write_cmd(0x10 + (self.column >> 4))
for num in range(0,16):
self.write_data(self.buffer[page*16+num])
if __name__=='__main__':
OLED = OLED_1inch3()
OLED.fill(0x0000)
OLED.show()
OLED.rect(0,0,128,64,OLED.white)
time.sleep(0.5)
OLED.show()
OLED.rect(10,22,20,20,OLED.white)
time.sleep(0.5)
OLED.show()
OLED.fill_rect(40,22,20,20,OLED.white)
time.sleep(0.5)
OLED.show()
OLED.rect(70,22,20,20,OLED.white)
time.sleep(0.5)
OLED.show()
OLED.fill_rect(100,22,20,20,OLED.white)
time.sleep(0.5)
OLED.show()
time.sleep(1)
OLED.fill(0x0000)
OLED.line(0,0,5,64,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(0,0,20,64,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(0,0,35,64,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(0,0,65,64,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(0,0,95,64,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(0,0,125,64,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(0,0,125,41,OLED.white)
OLED.show()
time.sleep(0.1)
OLED.line(0,0,125,21,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(0,0,125,3,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(127,1,125,64,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(127,1,110,64,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(127,1,95,64,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(127,1,65,64,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(127,1,35,64,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(127,1,1,64,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(127,1,1,44,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(127,1,1,24,OLED.white)
OLED.show()
time.sleep(0.01)
OLED.line(127,1,1,3,OLED.white)
OLED.show()
time.sleep(1)
OLED.fill(0x0000)
OLED.text("128 x 64 Pixels",1,10,OLED.white)
OLED.text("Pico-OLED-1.3",1,27,OLED.white)
OLED.text("SH1107",1,44,OLED.white)
OLED.show()
time.sleep(1)
OLED.fill(0x0000)
keyA = Pin(15,Pin.IN,Pin.PULL_UP)
keyB = Pin(17,Pin.IN,Pin.PULL_UP)
while(1):
if keyA.value() == 0:
OLED.fill_rect(0,0,128,20,OLED.white)
print("A")
else :
OLED.fill_rect(0,0,128,20,OLED.balck)
if(keyB.value() == 0):
OLED.fill_rect(0,44,128,20,OLED.white)
print("B")
else :
OLED.fill_rect(0,44,128,20,OLED.balck)
OLED.fill_rect(0,22,128,20,OLED.white)
OLED.text("lm.snakin.top",0,28,OLED.balck)
OLED.show()
time.sleep(1)
OLED.fill(0xFFFF)
最后使用thonny运行即可,之后可以根据示例代码自行更改显示效果。
|