**这是本文档旧的修订版!**
驱动SSD1306显示屏
添加显示屏
我们将进行的下一个实验是将一个OLED显示器连接到我们的Pico上,当然,也可以在上面打印一些东西。
我们将使用 I2C 显示屏,因此我们也将看到 Pico 如何使用 I2C 连接工作。记住,Pico有两条I2C总线。
我们的OLED是标准的1602型OLED显示器,到处都有。如果你愿意,也可以使用与我的显示屏尺寸不同的显示屏,只需在代码中更改尺寸即可。
这是我们如何把这些东西都挂起来的,只有四根线。 我们的显示器需要一个库,我们可以使用Thonny ID安装。你可能会发现在全菜单模式下比在基本模式下更容易,但这两种方式都可以。
点击 “工具 “菜单 点击 “管理包” 搜索 “ssd1306” 找到 “ssd1306.py “并安装它。 现在我们已经安装了库,我们可以看一下演示OLED显示屏的脚本。
1. I2C屏幕
# 树莓派 Pico OLED Display Test # Uses ssd1306 module # display-ssd1306-test.py # DroneBot Workshop 2021 # https://dronebotworkshop.com import machine import utime sda=machine.Pin(20) scl=machine.Pin(21) i2c=machine.I2C(0, sda=sda, scl=scl, freq=400000) from ssd1306 import SSD1306_I2C oled = SSD1306_I2C(128, 32, i2c) print(i2c.scan()) oled.text('Welcome to the', 0, 0) oled.text('Pi Pico', 0, 10) oled.text('Display Demo', 0, 20) oled.show() utime.sleep(4) oled.fill(1) oled.show() utime.sleep(2) oled.fill(0) oled.show() while True: oled.text("Hello World",0,0) for i in range (0, 164): oled.scroll(1,0) oled.show() utime.sleep(0.01)
我们的 OLED 显示屏是一个 I2C 设备,所以你会注意到,在脚本的开头,我们将两个 GPIO 引脚定义为 SDA (GPIO 20) 和 SCL (GPIO 21)。
Pico有两条I2C总线,你可以使用几种不同的GPIO引脚来连接它们。但它们并不是随便的引脚,例如某些引脚被指定为总线0的SDA,只有它们才能用于SDA总线0。
然后我们使用机器库的I2C函数定义一个I2C连接。我们需要给它提供以下参数。
I2C总线号,在我们的例子中是0。 SDA引脚 SCL引脚 I2C总线频率–在我们的例子中是400KHz。 然后我们添加OLED库,并创建一个I2C OLED对象。我们将大小参数和I2C连接信息传递给它。
注意,我们没有传递I2C地址。SD1306 OLED显示器有一个固定的I2C地址,所以我们不需要指定它。
不过,我在这里添加了一行与显示器无关的内容,但可以让你扫描I2C总线,并打印出它发现占用的地址。请注意,在Shell中打印出来的是十进制,而不是你更可能习惯看到的十六进制。
回到OLED脚本!
我们开始在显示屏上打印,几行文字。注意我们如何指定每行开始的像素位置。
实际上,我们并没有直接打印到显示屏上,而是将数据发送到一个缓冲区。oled.show()这一行将把该缓冲区的数据传输到显示器上。
在打印完欢迎信息并按住它四秒钟后,我们再执行oled.fill(1)。这将打开显示器中的每一个像素,或者更准确地说,是缓冲区中的每一个像素。然后我们做一个oled.show()来显示填充。
我们将填充物保持在显示屏上两秒钟,然后执行oled.fill(0),当我们显示它时,它将关闭显示屏中的每个像素。
现在进入True循环。
我们再次输入一些文本,经典的 “Hello World”。但我们没有进行 “显示 “来显示它,而是开始一个for-loop。在这个循环中,我们使用led.scroll(1,0)将显示水平移动1像素(垂直移动0像素,这就是另一个参数)。
然后我们在屏幕上显示,然后休眠一段很短的时间。然后我们再做一次。
结果将是显示屏在屏幕上滚动。你可以在for-loop中使用参数来改变它。
将脚本加载到你的Pico上,然后观看显示。你应该看到欢迎文字,然后是显示填充,然后是空。之后,只要你让实验运行,滚动的 “Hello World “就会继续。
2. SPI屏幕
from machine import Pin, SPI from ssd1306 import SSD1306_SPI import framebuf from time import sleep from utime import sleep_ms spi = SPI(1, 100000, mosi=Pin(11), sck=Pin(10)) oled = SSD1306_SPI(128, 32, spi, Pin(9),Pin(8), Pin(0)) #oled = SSD1306_SPI(WIDTH, HEIGHT, spi, dc,rst, cs) use GPIO PIN NUMBERS while True: try: for i in range(40): for j in range(56): oled.fill(0) oled.show() #sleep(1) oled.text("HELLO www.eetree.cn",i,j) oled.show() sleep_ms(50) except KeyboardInterrupt: break
14. 使用SSD1306 OLED显示 SSD1306 OLED显示可以使用SPI或I2C接口,支持不同分辨率(128×64, 128×32, 72×40, 64×48)和颜色(白色, 黄色, 蓝色, 黄色 + 蓝色).
硬件SPI接口:
from machine import Pin, SPI import ssd1306 hspi = SPI(1) # sck=14 (scl), mosi=13 (sda), miso=12 (unused) dc = Pin(4) # data/command rst = Pin(5) # reset cs = Pin(15) # chip select, some modules do not have a pin for this display = ssd1306.SSD1306_SPI(128, 64, hspi, dc, rst, cs)
软件SPI接口:
from machine import Pin, SoftSPI import ssd1306 spi = SoftSPI(baudrate=500000, polarity=1, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12)) dc = Pin(4) # data/command rst = Pin(5) # reset cs = Pin(15) # chip select, some modules do not have a pin for this display = ssd1306.SSD1306_SPI(128, 64, spi, dc, rst, cs)
I2C接口:
from machine import Pin, I2C import ssd1306 # using default address 0x3C i2c = I2C(sda=Pin(4), scl=Pin(5)) display = ssd1306.SSD1306_I2C(128, 64, i2c) </code? 在第一行打印Hello World: <code python> display.text('Hello, World!', 0, 0, 1) 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.rotate(True) # rotate 180 degrees display.rotate(False) # rotate 0 degrees display.show() # write the contents of the FrameBuffer to display memory
对FrameBuffer进行子类化提供了对图形原语的支持
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 107,43, colour=1 display.fill_rect(10, 10, 107, 43, 1) # draw a solid rectangle 10,10 to 107,43, 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()
显示MicroPython的Logo并打印一些字符:
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()
Adafruit关于SSD1306的使用介绍
I2C初始化
If your display is connected to the board using I2C (like if using a Feather and the FeatherWing OLED) you'll first need to initialize the I2C bus. On MicroPython.org firmware which uses the machine API you can initialize I2C like the MicroPython I2C guide mentions.
For example on a board like the ESP8266 you can run (assuming you're using the default SDA gpio #4 and SCL gpio #5 pins like on a Feather & SSD1306 FeatherWing):
import machine import ssd1306 i2c = machine.I2C(-1, machine.Pin(5), machine.Pin(4)) oled = ssd1306.SSD1306_I2C(128, 32, i2c)
Note that the first two parameters to the SSD1306_I2C class initializer are the width and height of the display in pixels. Be sure to use the right values for the display you're using!
SPI初始化
If your display is connected to the board using SPI you'll first need to initialize the SPI bus. On MicroPython.org firmware which uses the machine API you can initialize SPI like the MicroPython SPI guide mentions:
import machine import ssd1306 spi = machine.SPI(1, baudrate=8000000, polarity=0, phase=0) oled = ssd1306.SSD1306_SPI(128, 32, spi, machine.Pin(15), machine.Pin(0), machine.Pin(16))
Note the first two parameters to the SSD1306_SPI class initializer are the width and height of the display in pixels. Be sure to use the right values for the display you're using!
The next parameters to the initializer are the pins connected to the display's DC, reset, and CS lines in that order.
Drawing
Once the display is initialized using the code above you're ready to start drawing on it. Follow the drawing section on the CircuitPython page for all the details on pixel, fill, and other drawing functions. The usage of the library between CircuitPython and MicroPython is exactly the same!