【经验分享】Banana PI Leaf S3开发板之OLED屏显示驱动实现
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
实现数据数据发送的函数为:
1.def Write_IIC_Byte(IIC_Byte):
2. da=IIC_Byte
3. sck.off()
4. for i in range(8):
5. m=da
6. m=m&0x80
7. if(m==0x80):
8. sda.on()
9. else:
10. sda.off()
11. da=da<<1
12. sck.on()
13. sck.off()
实现清屏的函数为:
1.def OLED_Clear():
2. for i in range(8):
3. OLED_WR_Byte (0xb0+i,OLED_CMD)
4. OLED_WR_Byte (0x00,OLED_CMD)
5. OLED_WR_Byte (0x10,OLED_CMD)
6. for j in range(128):
7. OLED_WR_Byte(0x00,OLED_DATA)
OLED屏的初始化函数为:
1.def OLED_Init():
2. OLED_WR_Byte(0xAE,OLED_CMD)
3. OLED_WR_Byte(0x02,OLED_CMD)
4. OLED_WR_Byte(0x10,OLED_CMD)
5. OLED_WR_Byte(0x40,OLED_CMD)
6. OLED_WR_Byte(0x81,OLED_CMD)
7. OLED_WR_Byte(0xff,OLED_CMD)
8. OLED_WR_Byte(0xA1,OLED_CMD)
9. OLED_WR_Byte(0xC8,OLED_CMD)
10. OLED_WR_Byte(0xA6,OLED_CMD)
11. OLED_WR_Byte(0xA8,OLED_CMD)
12. OLED_WR_Byte(0x3f,OLED_CMD)
13. OLED_WR_Byte(0xD3,OLED_CMD)
14. OLED_WR_Byte(0x00,OLED_CMD)
15. OLED_WR_Byte(0xd5,OLED_CMD)
16. OLED_WR_Byte(0x80,OLED_CMD)
17. OLED_WR_Byte(0xD9,OLED_CMD)
18. OLED_WR_Byte(0xF1,OLED_CMD)
19. OLED_WR_Byte(0xDA,OLED_CMD)
20. OLED_WR_Byte(0x12,OLED_CMD)
21. OLED_WR_Byte(0xDB,OLED_CMD)
22. OLED_WR_Byte(0x40,OLED_CMD)
23. OLED_WR_Byte(0x20,OLED_CMD)
24. OLED_WR_Byte(0x02,OLED_CMD)
25. OLED_WR_Byte(0x8D,OLED_CMD)
26. OLED_WR_Byte(0x14,OLED_CMD)
27. OLED_WR_Byte(0xA4,OLED_CMD)
28. OLED_WR_Byte(0xA6,OLED_CMD)
29. OLED_WR_Byte(0xAF,OLED_CMD)
30. OLED_WR_Byte(0xAF,OLED_CMD)
31. OLED_Clear()
32. OLED_Set_Pos(0,0)
为了验证程序的可行性,特将清屏函数改造为彩条绘制函数,其内容为:
1.def OLED_Clear():
2. for i in range(8):
3. OLED_WR_Byte (0xb0+i,OLED_CMD)
4. OLED_WR_Byte (0x00,OLED_CMD)
5. OLED_WR_Byte (0x10,OLED_CMD)
6. for j in range(128):
7. OLED_WR_Byte(0xf0,OLED_DATA) #OLED_DATA
8.
9.def OLED_text():
10. OLED_Init()
11. OLED_Clear()
12.
13.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]
所设计的字符串显示函数为:
1.def OLED_ShowString(x,y,chr,Char_Size):
2. for character in chr:
3. OLED_ShowChar(x,y,character,Char_Size)
4. x+=8
5. if(x>120):
6. x=0
7. y+=2
实现字符串输出测试的程序如下:
1.def OLED_text():
2. OLED_Init() 3.OLED_Clear()
4. OLED_ShowString(0,0,'Banana PI ',16)
5. OLED_ShowString(0,2,'Leaf S3',16)
6. OLED_ShowString(0,4,'OLED TEST',16)
7. OLED_ShowString(0,6,'jinglixixi',16)
8.
9. OLED_text()
经上传测试,其显示效果如图3所示,说明驱动程序是有效的。
图3 显示字符串
通过OLED屏驱动程序的设计,基本上掌握了Thonny的编程方法、函数的设计及数组元素的使用等,收获还是很多的,后面就可以自主地进行功能设计了。
- 上一篇:【电路设计】PCB设计必须考虑的8种安全距离,搞错1种都出大 2022/12/8
- 下一篇:单片机I/O口驱动,为什么一般都选用三极管而不是MOS管? 2022/11/30