OLED屏有多种形式,在例程中提供了一种I2C接口的0.96寸单色屏程序。本人则有一款I2C接口的0.96寸双色屏,在掌握GPIO口使用的基础上,以模拟I2C的方式实现了它的驱动显示。
其引脚的连接关系如下:
VCC----3.3V
GND---GND
SDA---- GPIO15
SCL---- GPIO16
图1 引脚连接
将所用引脚设置为输出模式的语句为:
sda=Pin(15,Pin.OUT) #15
sck=Pin(16,Pin.OUT) #16
实现数据数据发送的函数为:
def Write_IIC_Byte(IIC_Byte):
da=IIC_Byte
sck.off()
for i in range(8):
m=da
m=m&0x80
if(m==0x80):
sda.on()
else:
sda.off()
da=da<<1
sck.on()
sck.off()
实现清屏的函数为:
def OLED_Clear():
for i in range(8):
OLED_WR_Byte (0xb0+i,OLED_CMD)
OLED_WR_Byte (0x00,OLED_CMD)
OLED_WR_Byte (0x10,OLED_CMD)
for j in range(128):
OLED_WR_Byte(0x00,OLED_DATA)
OLED屏的初始化函数为:
def OLED_Init():
OLED_WR_Byte(0xAE,OLED_CMD)
OLED_WR_Byte(0x02,OLED_CMD)
OLED_WR_Byte(0x10,OLED_CMD)
OLED_WR_Byte(0x40,OLED_CMD)
OLED_WR_Byte(0x81,OLED_CMD)
OLED_WR_Byte(0xff,OLED_CMD)
OLED_WR_Byte(0xA1,OLED_CMD)
OLED_WR_Byte(0xC8,OLED_CMD)
OLED_WR_Byte(0xA6,OLED_CMD)
OLED_WR_Byte(0xA8,OLED_CMD)
OLED_WR_Byte(0x3f,OLED_CMD)
OLED_WR_Byte(0xD3,OLED_CMD)
OLED_WR_Byte(0x00,OLED_CMD)
OLED_WR_Byte(0xd5,OLED_CMD)
OLED_WR_Byte(0x80,OLED_CMD)
OLED_WR_Byte(0xD9,OLED_CMD)
OLED_WR_Byte(0xF1,OLED_CMD)
OLED_WR_Byte(0xDA,OLED_CMD)
OLED_WR_Byte(0x12,OLED_CMD)
OLED_WR_Byte(0xDB,OLED_CMD)
OLED_WR_Byte(0x40,OLED_CMD)
OLED_WR_Byte(0x20,OLED_CMD)
OLED_WR_Byte(0x02,OLED_CMD)
OLED_WR_Byte(0x8D,OLED_CMD)
OLED_WR_Byte(0x14,OLED_CMD)
OLED_WR_Byte(0xA4,OLED_CMD)
OLED_WR_Byte(0xA6,OLED_CMD)
OLED_WR_Byte(0xAF,OLED_CMD)
OLED_WR_Byte(0xAF,OLED_CMD)
OLED_Clear()
OLED_Set_Pos(0,0)
为了验证程序的可行性,特将清屏函数改造为彩条绘制函数,其内容为:
def OLED_Clear():
for i in range(8):
OLED_WR_Byte (0xb0+i,OLED_CMD)
OLED_WR_Byte (0x00,OLED_CMD)
OLED_WR_Byte (0x10,OLED_CMD)
for j in range(128):
OLED_WR_Byte(0xf0,OLED_DATA) #OLED_DATA
def OLED_text():
OLED_Init()
OLED_Clear()
OLED_text ()
经上传测试,其显示效果如图2所示,说明驱动程序是有效的。
图2 绘制彩条
为了实现字符,需按如下形式来定义字库:
F8X16=[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,
0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
** ... **
0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
所设计的字符串显示函数为:
def OLED_ShowString(x,y,chr,Char_Size):
for character in chr:
OLED_ShowChar(x,y,character,Char_Size)
x+=8
if(x>120):
x=0
y+=2
实现字符串输出测试的程序如下:
def OLED_text():
OLED_Init()
OLED_Clear()
OLED_ShowString(0,0,'Banana PI ',16)
OLED_ShowString(0,2,'Leaf S3',16)
OLED_ShowString(0,4,'OLED TEST',16)
OLED_ShowString(0,6,'jinglixixi',16)
OLED_text()
经上传测试,其显示效果如图3所示,说明驱动程序是有效的。
图3 显示字符串
通过OLED屏驱动程序的设计,基本上掌握了Thonny的编程方法、函数的设计及数组元素的使用等,收获还是很多的,后面就可以自主地进行功能设计了。