2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > [树莓派]PICO基础使用_微雪OLED显示

[树莓派]PICO基础使用_微雪OLED显示

时间:2021-03-19 20:05:01

相关推荐

[树莓派]PICO基础使用_微雪OLED显示

简介

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 Pinif __name__ == '__main__':# 构建led对象# 板载LED灯连接与引脚25相连# LED = Pin(id, mode, pull)# id:PICO引脚编号# mode:输入输出方式,有Pin.IN(输入)和Pin.OUT(输出)两种# pull:上下拉电阻配置,有None(无上下拉电阻)、Pin.PULL_UP(上拉电阻)和Pin.PULL_DOWN(下拉电阻)三种LED = Pin(25, Pin.OUT)# 高电平点亮LED.value(1)

板载led闪烁

from machine import Pinfrom utime import sleepimport utimeled = Pin(25, Pin.OUT)if __name__ == '__main__':while True:# led点亮led.value(1)utime.sleep_ms(1000)# led熄灭led.value(0)utime.sleep_ms(1000)

pico+微雪OLED 1.3

屏幕和oled连接,输入示例代码

from machine import Pin,SPIimport framebufimport timeDC = 8RST = 12MOSI = 11SCK = 10CS = 9class OLED_1inch3(framebuf.FrameBuffer):def __init__(self):self.width = 128self.height = 64self.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 = 0xffffself.balck = 0x0000def 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)#turn off OLED displayself.write_cmd(0x00) #set lower column addressself.write_cmd(0x10) #set higher column address self.write_cmd(0xB0) #set page address self.write_cmd(0xdc) #et display start line self.write_cmd(0x00) self.write_cmd(0x81) #contract control self.write_cmd(0x6f) #128self.write_cmd(0x21) # Set Memory addressing mode (0x20/0x21) #self.write_cmd(0xa0) #set segment remap self.write_cmd(0xc0) #Com scan directionself.write_cmd(0xa4) #Disable Entire Display On (0xA4/0xA5) self.write_cmd(0xa6) #normal / reverseself.write_cmd(0xa8) #multiplex ratio self.write_cmd(0x3f) #duty = 1/64self.write_cmd(0xd3) #set display offset self.write_cmd(0x60)self.write_cmd(0xd5) #set osc division self.write_cmd(0x41)self.write_cmd(0xd9) #set pre-charge periodself.write_cmd(0x22) self.write_cmd(0xdb) #set vcomh self.write_cmd(0x35) self.write_cmd(0xad) #set charge pump enable self.write_cmd(0x8a) #Set DC-DC enable (a=0:disable; a=1:enable)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运行即可,之后可以根据示例代码自行更改显示效果。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。