分享:树莓派pico使用MicroPython驱动oled屏显示中文和英文

jdh99 10cm2021-04-082283 次点击1 人感谢
# 树莓派pico使用MicroPython驱动oled屏显示中文和英文


本文博客链接:[http://blog.csdn.net/jdh99](http://blog.csdn.net/jdh99),作者:jdh,转载请注明.

欢迎前往社区交流:[海萤物联网社区](http://www.ztziot.com/)

## 介绍
[esp32使用MicroPython驱动oled屏显示中文和英文](https://blog.csdn.net/jdh99/article/details/115485983) 一文中使用esp32驱动oled屏幕,我还有一个树莓派pico,使用ssd1306py驱动下oled屏幕试试。

安装方法和oled库ssd1306py的介绍都请参考文章 [esp32使用MicroPython驱动oled屏显示中文和英文](https://blog.csdn.net/jdh99/article/details/115485983)

## 硬件连接
树莓派pico引脚:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408163143924.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pkaDk5,size_16,color_FFFFFF,t_70)
本文使用i2c端口0,SDA是GP4,SCL是GP5。将pico的引脚与oled屏对应引脚相连。

## 示例
### 显示英文字符
```python
import ssd1306py as lcd


lcd.init_i2c(5, 4, 128, 64, 0)
lcd.text('font8x8', 0, 0, 8)
lcd.text('font16x16', 0, 20, 16)
lcd.text('font24x24', 0, 40, 24)
lcd.show()
```

显示效果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408164050302.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pkaDk5,size_16,color_FFFFFF,t_70)

```python
import ssd1306py as lcd


lcd.init_i2c(5, 4, 128, 64, 0)
lcd.text('font32x32', 0, 0, 32)
lcd.show()
```
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408164248623.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pkaDk5,size_16,color_FFFFFF,t_70)

### 显示汉字
汉字字库较大,单一字号的字库就需要几M字节,所以没有放在库中。需要自己将需要显示的汉字做成字库传入到库中。

制作字库可以使用工具PCtoLCD2002,[百度网盘下载链接](https://pan.baidu.com/s/1gc5swTKB7iuFa7swJrg_Jg),提取码:z4tf

使用方法可参考:[如何使用PCtoLCD2002取模(汉字、ASCII字符集)](https://blog.csdn.net/qq_41359157/article/details/106174897)

提取的汉字字库做成字典格式供程序使用,字典的键是汉字的utf-8编码值。可以使用python获取python的utf-8值,比如:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210407153847574.png)
则汉字”你“的utf-8值是0xe4bda0。

也可以使用在线转换工具查询:[http://www.mytju.com/classcode/tools/encode_utf8.asp](http://www.mytju.com/classcode/tools/encode_utf8.asp)


比如以下示例,显示汉字“你好”。

```python
import ssd1306py as lcd

font16 = {‌
0xe4bda0:
[0x08, 0x08, 0x08, 0x11, 0x11, 0x32, 0x34, 0x50, 0x91, 0x11, 0x12, 0x12, 0x14, 0x10, 0x10, 0x10, 0x80, 0x80,
0x80, 0xFE, 0x02, 0x04, 0x20, 0x20, 0x28, 0x24, 0x24, 0x22, 0x22, 0x20, 0xA0, 0x40], # 你
0xe5a5bd:
[0x10, 0x10, 0x10, 0x10, 0xFC, 0x24, 0x24, 0x25, 0x24, 0x48, 0x28, 0x10, 0x28, 0x44, 0x84, 0x00, 0x00, 0xFC,
0x04, 0x08, 0x10, 0x20, 0x20, 0xFE, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0xA0, 0x40] # 好
}

font24 = {‌
0xe4bda0:
[0x00, 0x01, 0x01, 0x03, 0x03, 0x02, 0x04, 0x04, 0x0E, 0x1C, 0x14, 0x24, 0x44, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x05, 0x04, 0x06, 0x04, 0x00,
0x00, 0x00, 0x8C, 0x0C, 0x08, 0x18, 0x1F, 0x30, 0x21, 0x41, 0x41, 0x91, 0x19, 0x11, 0x31, 0x21, 0x41, 0x41,
0x81, 0x01, 0x11, 0x0F, 0x02, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x0C, 0x10, 0x00, 0x00, 0x00, 0x20, 0x10, 0x18, 0x0C, 0x0C, 0x06,
0x04, 0x00, 0x00, 0x00, 0x00, 0x00], # 你
0xe5a5bd:
[0x00, 0x00, 0x06, 0x06, 0x06, 0x04, 0x04, 0x7F, 0x0C, 0x0C, 0x08, 0x08, 0x08, 0x18, 0x10, 0x11, 0x0D, 0x03,
0x02, 0x04, 0x18, 0x20, 0x40, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0xC0, 0x40, 0x40, 0xC0, 0x80, 0xBF, 0x80, 0x80, 0x00, 0x00, 0x80,
0xC0, 0x60, 0x00, 0x07, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0xF8, 0x18, 0x20, 0x40, 0x80, 0x80, 0x80, 0x84, 0xFE, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x00, 0x00] # 好
}

font32 = {‌
0xe4bda0:
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x07, 0x0D, 0x09, 0x11, 0x11, 0x21,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x40, 0x70, 0x60, 0xE0, 0xC0, 0xC1, 0x81, 0x03,
0x03, 0x86, 0x84, 0x8C, 0x88, 0x90,
0x81, 0x83, 0x83, 0x83, 0x86, 0x86, 0x8C, 0x88, 0x90, 0x90, 0xA0, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x60, 0xE0, 0xC0, 0xC0, 0x80,
0x80, 0xFF, 0x00, 0x10, 0x0C, 0x08, 0x08, 0x08, 0x88, 0x88, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0xF8, 0x38, 0x10, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0xFC, 0x18, 0x30, 0x20, 0x40, 0x00, 0x00, 0x00, 0x80,
0x40, 0x20, 0x30, 0x18, 0x1C, 0x0C,
0x0C, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], # 你
0xe5a5bd:
[0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x03, 0x3F, 0x03, 0x03, 0x02, 0x06, 0x06, 0x04, 0x04, 0x0C,
0x0C, 0x08, 0x08, 0x0E, 0x01, 0x00,
0x00, 0x01, 0x03, 0x04, 0x08, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x80, 0x81, 0x80, 0x00, 0x08, 0xFC,
0x08, 0x08, 0x18, 0x18, 0x18, 0x18,
0x17, 0x30, 0x30, 0x30, 0x60, 0x60, 0xC0, 0xF0, 0xBC, 0x8C, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xFF, 0x00, 0x00,
0x00, 0x01, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0xFF, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0xFC, 0x1C, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0x20, 0xF0, 0x70, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0xFC, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] # 好
}

lcd.init_i2c(5, 4, 128, 64, 0)
lcd.set_font(font16, 16)
lcd.set_font(font24, 24)
lcd.set_font(font32, 32)
lcd.text_cn('你好', 0, 0, 16)
lcd.text_cn('你好', 40, 00, 24)
lcd.text_cn('你好', 0, 30, 32)
lcd.show()
```

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210407154608815.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pkaDk5,size_16,color_FFFFFF,t_70)
收藏 ♥ 感谢
暂无回复

登录注册 后可回复。




› 相关内容关注微信公众号