目录
(以下内容均为个人所学总结,希望对你有所帮助,感谢你的阅读!!!)
硬件准备:
01Studio K210核心板、OLED显示屏、母对母杜邦线
详细见电路连接部分。
引脚图:
01Studio K210核心板引脚
OLED引脚
电路连接
OLED上的SCL、SDA引脚可接到任意的IO口,在编写程序时,再注册对应的引脚和构造对应的IO口即可。
烧录驱动文件:
通过MaixPy IDE上的发送文件到开发板即可。
工具->发送文件到开发板->选择需要烧写的驱动文件
驱动文件
'''
ssd1306k.py
'''
_init_command = [0xAE, 0x20, 0x10, 0xB0,
0xC8, 0x00, 0x10, 0x40,
0x81, 0xFF, 0xA1, 0xA6,
0xA8, 0x3F, 0xA4, 0xD3,
0x00, 0xD5, 0xF0, 0xD9,
0x22, 0xDA, 0x12, 0xDB,
0x20, 0x8D, 0x14, 0xAF]
_on_command = [0x8D, 0x14, 0xAF]
_off_command = [0x8D, 0x10, 0xAE]
Font6x8 = [[0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x00, 0x00, 0x2f, 0x00, 0x00],
[0x00, 0x00, 0x07, 0x00, 0x07, 0x00],
[0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14],
[0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12],
[0x00, 0x62, 0x64, 0x08, 0x13, 0x23],
[0x00, 0x36, 0x49, 0x55, 0x22, 0x50],
[0x00, 0x00, 0x05, 0x03, 0x00, 0x00],
[0x00, 0x00, 0x1c, 0x22, 0x41, 0x00],
[0x00, 0x00, 0x41, 0x22, 0x1c, 0x00],
[0x00, 0x14, 0x08, 0x3E, 0x08, 0x14],
[0x00, 0x08, 0x08, 0x3E, 0x08, 0x08],
[0x00, 0x00, 0x00, 0xA0, 0x60, 0x00],
[0x00, 0x08, 0x08, 0x08, 0x08, 0x08],
[0x00, 0x00, 0x60, 0x60, 0x00, 0x00],
[0x00, 0x20, 0x10, 0x08, 0x04, 0x02],
[0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E],
[0x00, 0x00, 0x42, 0x7F, 0x40, 0x00],
[0x00, 0x42, 0x61, 0x51, 0x49, 0x46],
[0x00, 0x21, 0x41, 0x45, 0x4B, 0x31],
[0x00, 0x18, 0x14, 0x12, 0x7F, 0x10],
[0x00, 0x27, 0x45, 0x45, 0x45, 0x39],
[0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30],
[0x00, 0x01, 0x71, 0x09, 0x05, 0x03],
[0x00, 0x36, 0x49, 0x49, 0x49, 0x36],
[0x00, 0x06, 0x49, 0x49, 0x29, 0x1E],
[0x00, 0x00, 0x36, 0x36, 0x00, 0x00],
[0x00, 0x00, 0x56, 0x36, 0x00, 0x00],
[0x00, 0x08, 0x14, 0x22, 0x41, 0x00],
[0x00, 0x14, 0x14, 0x14, 0x14, 0x14],
[0x00, 0x00, 0x41, 0x22, 0x14, 0x08],
[0x00, 0x02, 0x01, 0x51, 0x09, 0x06],
[0x00, 0x32, 0x49, 0x59, 0x51, 0x3E],
[0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C],
[0x00, 0x7F, 0x49, 0x49, 0x49, 0x36],
[0x00, 0x3E, 0x41, 0x41, 0x41, 0x22],
[0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C],
[0x00, 0x7F, 0x49, 0x49, 0x49, 0x41],
[0x00, 0x7F, 0x09, 0x09, 0x09, 0x01],
[0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A],
[0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F],
[0x00, 0x00, 0x41, 0x7F, 0x41, 0x00],
[0x00, 0x20, 0x40, 0x41, 0x3F, 0x01],
[0x00, 0x7F, 0x08, 0x14, 0x22, 0x41],
[0x00, 0x7F, 0x40, 0x40, 0x40, 0x40],
[0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F],
[0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F],
[0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E],
[0x00, 0x7F, 0x09, 0x09, 0x09, 0x06],
[0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E],
[0x00, 0x7F, 0x09, 0x19, 0x29, 0x46],
[0x00, 0x46, 0x49, 0x49, 0x49, 0x31],
[0x00, 0x01, 0x01, 0x7F, 0x01, 0x01],
[0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F],
[0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F],
[0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F],
[0x00, 0x63, 0x14, 0x08, 0x14, 0x63],
[0x00, 0x07, 0x08, 0x70, 0x08, 0x07],
[0x00, 0x61, 0x51, 0x49, 0x45, 0x43],
[0x00, 0x00, 0x7F, 0x41, 0x41, 0x00],
[0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55],
[0x00, 0x00, 0x41, 0x41, 0x7F, 0x00],
[0x00, 0x04, 0x02, 0x01, 0x02, 0x04],
[0x00, 0x40, 0x40, 0x40, 0x40, 0x40],
[0x00, 0x00, 0x01, 0x02, 0x04, 0x00],
[0x00, 0x20, 0x54, 0x54, 0x54, 0x78],
[0x00, 0x7F, 0x48, 0x44, 0x44, 0x38],
[0x00, 0x38, 0x44, 0x44, 0x44, 0x20],
[0x00, 0x38, 0x44, 0x44, 0x48, 0x7F],
[0x00, 0x38, 0x54, 0x54, 0x54, 0x18],
[0x00, 0x08, 0x7E, 0x09, 0x01, 0x02],
[0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C],
[0x00, 0x7F, 0x08, 0x04, 0x04, 0x78],
[0x00, 0x00, 0x44, 0x7D, 0x40, 0x00],
[0x00, 0x40, 0x80, 0x84, 0x7D, 0x00],
[0x00, 0x7F, 0x10, 0x28, 0x44, 0x00],
[0x00, 0x00, 0x41, 0x7F, 0x40, 0x00],
[0x00, 0x7C, 0x04, 0x18, 0x04, 0x78],
[0x00, 0x7C, 0x08, 0x04, 0x04, 0x78],
[0x00, 0x38, 0x44, 0x44, 0x44, 0x38],
[0x00, 0xFC, 0x24, 0x24, 0x24, 0x18],
[0x00, 0x18, 0x24, 0x24, 0x18, 0xFC],
[0x00, 0x7C, 0x08, 0x04, 0x04, 0x08],
[0x00, 0x48, 0x54, 0x54, 0x54, 0x20],
[0x00, 0x04, 0x3F, 0x44, 0x40, 0x20],
[0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C],
[0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C],
[0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C],
[0x00, 0x44, 0x28, 0x10, 0x28, 0x44],
[0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C],
[0x00, 0x44, 0x64, 0x54, 0x4C, 0x44],
[0x00, 0x00, 0x10, 0xFE, 0x82, 0x00],
[0x00, 0x00, 0x00, 0xFF, 0x00, 0x00],
[0x00, 0x82, 0xFE, 0x10, 0x00, 0x00],
[0x02, 0x01, 0x01, 0x02, 0x02, 0x01]]
class SSD1306:
def __init__(self, i2c, addr = 0x3c):
self.i2c = i2c
self.ADDR = addr
self.Init()
def _write_register(self, register, data):
self.i2c.writeto_mem(self.ADDR, register, data, mem_size = 8)
def _set_pos(self, x, y):
if x<128 and y<8:
self._write_register(0x00, 0xb0+y)
self._write_register(0x00, ((x&0xf0)>>4)|0x10)
self._write_register(0x00, x&0x0f)
def Init(self):
for i in _init_command:
self._write_register(0x00, i)
def On(self):
for i in _on_command:
self._write_register(0x00, i)
def Off(self):
for i in _off_command:
self._write_register(0x00, i)
def fill(self, data):
for i in range(0,8):
self._write_register(0x00, 0xb0+i)
self._write_register(0x00, 0x10)
self._write_register(0x00, 0x01)
for j in range(0,128):
self._write_register(0x40, data)
def text (self, STR, x, y ):
self._set_pos(x, y)
for i in range(len(STR)):
Num = ord(STR[i])-32
for j in range(6):
self._write_register(0x40, Font6x8[Num][j])
导包:
from machine import I2C
from ssd1306k import SSD1306 #OLED,烧写进开发板的OLED驱动文件
I2C库
扫描 I2C 总线的设备。返回地址,如:0x3c
i2c.scan()
从指定地址读数据。addr:指定设备地址;len:读取字节数;
i2c.readfrom(addr,len)
写数据。addr:从机地址;buf:数据内容
i2c.writeto(addr,buf)
注销 I2C
i2c.deinit()
OLED库
清屏;value=0x00 (黑屏);value=0xFF(白屏)
oled.fill([value])
将 string 字符写在指定为位置。
【string】:字符
【x】横坐标;[0-127]
【y】纵坐标。[0-7] 共 8 行
oled.text(string,x,y)
配置I2C:
参数:
【id】I2C ID, [0~2] (I2C.I2C0~I2C.I2C2) [3~5] (I2C.I2C3~I2C.I2C5, I2C_SOFT)是软模拟I2C的编号
【mode】模式, 主机(I2C.MODE_MASTER)和从机(I2C.MODE_SLAVE)模式
【scl】时钟引脚;直接传引脚编号,取值范围: [0,47]。 可以不设置,而是使用 fm 统一管理引脚映射。
【sda】数据引脚; 直接传引脚编号,取值范围: [0,47]。 可以不设置,而是使用 fm 统一管理引脚映射。
【gscl】SCL 对应的 GPIOHS,使用软件模拟 I2C 时才需要传入,默认与 scl 相同。
【gsda】SDA 对应的 GPIOHS,使用软件模拟 I2C 时才需要传入,默认与 sda 相同 。
【freq】通信频率,即速度,I2C通信频率, 支持标准100Kb/s, 快速400Kb/s, 以及更高速率(硬件支持超快速模式1000Kb/s,以及高速模式3.4Mb/s)
【timeout】参数保留,设置无效
【addr】从机地址,如果是主机模式不用设置, 从机模式则代表从机(本机)地址
【addr_size】地址长度, 支持 7 位寻址和 10 位寻址, 取值 7 或者 10
【on_recieve】从机模式的接收回调函数
【on_transmit】从机模式的发送回调函数
【on_event】从机模式的事件函数(开始事件和结束事件)
machine.I2C(id, mode=I2C.MODE_MASTER, scl=None, sda=None, gscl=None, gsda=None, freq=400000, timeout=1000, addr=0, addr_size=7, on_recieve=None, on_transmit=None, on_event=None)
i2c = I2C(I2C.I2C0, mode=I2C.MODE_MASTER,scl=27, sda=28) #27,28是对应的底板引脚,可以随便改
配置OLED:
构 OLED 显示屏对象。默认分辨率 128*64;
【i2c】定义好的 I2C 对象;
【addr】显示屏设备地址。
oled = SSD1306(i2c, addr)
oled = SSD1306(i2c, addr=0x3c)
例程:
main.py
SCL->IO16
SDA->IO18I2C总线通讯和OLED显示屏
from machine import I2C
from ssd1306k import SSD1306
#定义I2C接口和OLED对象
i2c = I2C(I2C.I2C0, mode=I2C.MODE_MASTER,scl=16, sda=18)
oled = SSD1306(i2c, addr=0x3c)
#清屏,0x00(黑屏),0xff(白屏)
oled.fill(0)
#显示字符。参数格式为(str,x,y),其中x范围是0-127,y范围是0-7(共8行)
oled.text("Hello World!", 0, 0) #写入第 0 行内容
oled.text("MicroPython", 0, 2) #写入第 2 行内容
oled.text("By 01Studio", 0, 5) #写入第 5 行内容
#oled.text("你好,世界", 0, 6)
oled.text("ni_hao", 0, 6)
oled.text("ni_hao", 0, 7)
#oled.fill(0xff)