ssd1306 oled 屏幕模块配备 0.9 英寸迷你单色屏,128×32 分辨率,白光像素点。拥有极高的对比度,显示清晰极易辨识。oled 仅使用树莓派的 i2c 和电源引脚,无需独立供电,即插即用。
接线
屏幕模块的 vcc 接树莓派 pico 的 3v3 引脚
屏幕模块的 gnd 接树莓派 pico 的 gnd 引脚
屏幕模块的 sda 引脚接树莓派 pico 的 gp14 引脚
屏幕模块的 scl 引脚接树莓派 pico 的 scl 引脚
代码
这里使用开源的 micropython ssd1306 库。将下面的代码保存在 pico 上,命名为 ssd1306.py。
# micropython ssd1306 oled driver, i2c and spi interfaces from micropython import constimport framebuf # register definitionsset_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_iref_select = const(0xad)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.htmlclass 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, # display off # address setting set_mem_addr, 0x00, # horizontal # resolution and layout set_disp_start_line, # start at line 0 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 set_iref_select, 0x30, # enable internal iref during display on # charge pump set_charge_pump, 0x10 if self.external_vcc else 0x14, set_disp | 0x01, # display on ): # on self.write_cmd(cmd) self.fill(0) self.show() def poweroff(self): self.write_cmd(set_disp) 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 rotate(self, rotate): self.write_cmd(set_com_out_dir | ((rotate & 1) << 3)) self.write_cmd(set_seg_remap | (rotate & 1)) def show(self): x0 = 0 x1 = self.width - 1 if self.width != 128: # narrow displays use centred columns col_offset = (128 - self.width) // 2 x0 += col_offset x1 += col_offset 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 = [bx40, 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)
该库使用了 micropython 的 framebuf 库。关于 framebuf 库的用法可以参考文档,也可以通过这篇文章快速了解一下「micropython 帧缓冲区库 framebuf 的使用方法」。
下面是示例代码,在屏幕上输出指定的信息,将代码保存在 pico 上,命名为 main.py。
import machineimport ssd1306 i2c = machine.i2c(1, sda=machine.pin(14), scl=machine.pin(15), freq=400_000)print(i2c device: + str(i2c.scan()[0]))oled = ssd1306.ssd1306_i2c(128, 64, i2c) oled.text(pico lab, 0, 0)oled.hline(0, 10, 128, 1)oled.text(hello world!, 0, 26)oled.hline(0, 48, 128, 1)oled.text(pico.nxez.com, 0, 52)oled.show()
显示效果如图。
下面的示例代码来画一个树莓派 logo 并显示在屏幕上。将代码保存在 pico 上,命名为 main.py。
import ssd1306import framebuf i2c = machine.i2c(1, sda=machine.pin(14), scl=machine.pin(15), freq=400_000)oled = ssd1306.ssd1306_i2c(128, 64, i2c) data = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0xfc,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf8,0x3e,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xe0,0x07,0x00,0x00,0x00,0x7f,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3f,0x80,0x03,0x80,0x00,0x01,0xf7,0xc0,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x01,0xff,0x80,0x01,0xf8,0x00,0x0f,0x90,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xff,0xe0,0x01,0xff,0x00,0x0e,0x00,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0x01,0xf8,0x01,0xff,0xc0,0x3c,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x00,0xff,0xff,0xe1,0xe0,0x70,0x00,0x60,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x38,0x00,0xff,0xff,0xe0,0x70,0x70,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x00,0xff,0xff,0xf0,0x39,0xc0,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x00,0xff,0xdf,0xf0,0x1d,0xc0,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x01,0xfe,0x01,0xf8,0x0f,0x80,0x00,0x30,0x00,0x00,0x00, 0x00,0x00,0x00,0x01,0xc0,0x01,0xf8,0x00,0x7c,0x0f,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xc0,0x03,0xf8,0x00,0x3e,0x0f,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xc0,0x07,0xf0,0x00,0x1f,0x06,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xc0,0x0f,0xe0,0x00,0x1f,0x86,0x00,0x20,0x30,0x00,0x00,0x00, 0x00,0x00,0x00,0x07,0xc0,0x3f,0xe0,0x00,0x0f,0x86,0x00,0x20,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x0f,0xc0,0x7f,0xe0,0x00,0x0f,0xc6,0x00,0x40,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x0f,0xff,0xff,0xe0,0x00,0x0f,0xf7,0x00,0xc0,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0xff,0xff,0xe0,0x00,0x0f,0xff,0x00,0x80,0x60,0x00,0x00,0x00, 0x00,0x00,0x00,0x3f,0xff,0xff,0xf0,0x00,0x0f,0xff,0x01,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x39,0xff,0x80,0xf0,0x00,0x0f,0x9f,0x83,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0xfe,0x00,0x78,0x00,0x0f,0x0f,0xc6,0x00,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0xfe,0x00,0x3c,0x00,0x0e,0x07,0xfc,0x00,0xc0,0x00,0x00,0x00, 0x00,0x00,0x00,0xe0,0x7c,0x00,0x1e,0x00,0x1e,0x03,0xfc,0x00,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0xc0,0x78,0x00,0x1f,0x00,0x3c,0x03,0xfc,0x03,0x80,0x00,0x00,0x00,0x00,0x00,0x01,0xc0,0x78,0x00,0x0f,0xe0,0xfc,0x03,0xfe,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xc0,0x78,0x00,0x0f,0xff,0xfc,0x01,0xff,0xfc,0x00,0x00,0x00,0x00, 0x00,0x00,0x01,0xc0,0x78,0x00,0x0f,0xff,0xfc,0x01,0xff,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xc0,0x78,0x00,0x0f,0xff,0xfc,0x03,0xff,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xc0,0x78,0x00,0x0f,0xff,0xfc,0x03,0xff,0xfc,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xc0,0x78,0x00,0x0f,0xe0,0xfc,0x03,0xfe,0x07,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xc0,0x78,0x00,0x0f,0x00,0x3c,0x03,0xfc,0x03,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x7c,0x00,0x1e,0x00,0x1e,0x07,0xf8,0x00,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x7e,0x00,0x3c,0x00,0x0e,0x07,0xfc,0x00,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0xfe,0x00,0x38,0x00,0x0f,0x0f,0xc6,0x00,0xe0,0x00,0x00,0x00, 0x00,0x00,0x00,0x38,0xff,0x80,0xf8,0x00,0x07,0x9f,0x83,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0xff,0xe3,0xf0,0x00,0x07,0xff,0x01,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x0f,0xff,0xff,0xf0,0x00,0x07,0xf7,0x00,0x80,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x0f,0xe0,0xff,0xf0,0x00,0x07,0xe7,0x00,0xc0,0x30,0x00,0x00,0x00, 0x00,0x00,0x00,0x07,0xc0,0x3f,0xf0,0x00,0x0f,0x86,0x00,0x40,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xc0,0x0f,0xf0,0x00,0x0f,0x86,0x00,0x20,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xc0,0x07,0xf0,0x00,0x1f,0x0e,0x00,0x20,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xc0,0x03,0xf0,0x00,0x3e,0x0e,0x00,0x10,0x30,0x00,0x00,0x00, 0x00,0x00,0x00,0x01,0xc0,0x01,0xf8,0x00,0x7c,0x4f,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xc0,0x00,0xfc,0x00,0xf8,0x1f,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x00,0xff,0x03,0xf8,0x1d,0x80,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x00,0x7f,0xff,0xf0,0x39,0xc0,0x00,0x30,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x70,0x00,0x7f,0xff,0xe0,0xf1,0xc0,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x7f,0xff,0xe1,0xe0,0x70,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1c,0x00,0x7f,0x00,0xff,0xc0,0x70,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0f,0x01,0xf0,0x00,0xff,0x00,0x3c,0x00,0x60,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x07,0xff,0xc0,0x01,0xf8,0x00,0x0e,0x00,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xff,0x80,0x03,0xc0,0x00,0x07,0x90,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0f,0xe0,0x07,0x80,0x00,0x01,0xf7,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf8,0x1f,0x00,0x00,0x00,0x7f,0xc0,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0xfc,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0xf8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00] buf = framebuf.framebuffer(bytearray(data), 128, 64, framebuf.mono_hlsb)oled.blit(buf, 0, 0)oled.show()
运行效果如图。
儿童智能设备市场符合“刚需”特征
SSD20X USB摄像头使用
运行电容与启动电容的区别,运行电容有什么作用
你知道PCB板上沉金和镀金的原理吗?
一文详解半导体制造技术
树莓派Pico上使用SSD1306 OLED屏幕
人脸识别行业的B端市场浅析
怎样编写自己的PCB设计检查器
物联网背景下的智能家居是怎样的
一个与电感有关的公式
HTC U11怎么样?苹果iPhone7又迎来降价潮,面对年度旗舰HTC U11的强强压迫,你会怎么选?
SpaceX将继续开展 Q/V/Ka 等频段的通信性能测试
艾德克斯可为医疗电子设备提供专业的测试解决方案
三星Note10前置面板谍照曝光 下巴比iPhone还要窄
国行版本任天堂新世代Switch游戏机正式发布售价为2099元
什么是实时操作系统(RTOS)
“走向世界”的不止华为Mate9,老外认可的手机还有华为荣耀Magic
运行内存12g和16g的区别
基于LTC3803-5设计的高温150度C反激电源控制技术
反激开关电源:RCD吸收电路