1.13.1. OLED屏原理
将单个LED按128x64点阵排列,就是12864点阵屏,通过点亮特定位置的led传达信息,这就是LED屏的原理,如下图所示。
当然,操作128x64个led的需要的IO口比较多,对单片机的资源造成很大的浪费,单片机与LED之间往往配置一个专用驱动芯片。单片机与驱动芯片之间通过串行总线通讯,而驱动芯片负责点亮led。
典型的芯片有SSD1306,对单片机端I2C通讯。
1.13.2. I2C总线介绍
I2C总线在物理连接上非常简单,分别由SDA(串行数据线)和SCL(串行时钟线)及上拉电阻组成。I2C的物理总线必须使用上拉电阻。
通信原理是通过对SCL和SDA线高低电平时序的控制,来产生I2C总线协议所需要的信号进行数据的传递。
在这个阶段,能够做到看到sda、scl立马做到如下两点就足够了:
1. 这是I2C接口,物理上需要有上拉电阻(有些硬件内置了上拉电阻);
2. 能够使用有I2C接口的单片机与其通讯;
至于I2C协议,大家可以自行百度学习。
1.13.3. 倒计时
micropython在esp单片机平台下默认有2个I2C硬件接口,如官网所示:
从开发板引出电源VCC、GND给屏幕供电,屏幕SDA接开发板26脚,屏幕SCL接开发板25脚。
下载ssd1306.py文件到开发板,即完成环境环境配置。
在官网ESP8266菜单中,有ssd1306的驱动案例,该案例在esp32中同样适用:
1 from ssd1306 import SSD1306_I2C 2 from machine import I2C 3 from time import sleep 4 5 display = SSD1306_I2C(128,64,I2C(1)) 6 7 display.fill(0) 8 display.show() 9 10 temp = 9 11 12 while True: 13 display.fill(0) 14 display.text(str(temp),60,20) 15 display.show() 16 if (temp >0): 17 temp -=1 18 else: 19 temp = 9 20 sleep(1)
1.13.4. 设置欢迎界面
Window自带的“画图”软件,“文件”->“属性”设置图纸尺寸,如下:
设计欢迎界面,保存,选择“bmp”单色位图格式。
打开pctolcd2002,配置字模选型,选择“图形模式”->“打开一个bmp图像”->“生成字模”:
Copy出字模,放在单片机中解码,如下:
1 from ssd1306 import SSD1306_I2C 2 from machine import I2C 3 from time import sleep 4 5 display = SSD1306_I2C(128,64,I2C(1)) 6 temp = 9 7 8 display.fill(0) 9 display.show() 10 11 12 wel = (0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 13 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 14 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 15 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 16 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 17 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 18 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 19 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 20 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 21 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 22 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 23 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 24 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 25 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 26 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 27 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 28 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 29 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 30 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 31 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 32 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 33 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 34 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 35 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 36 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 37 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 38 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 39 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 40 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 41 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 42 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 43 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 44 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 45 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 46 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 47 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 48 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,\ 49 0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 50 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 51 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 52 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 53 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 54 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 55 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 56 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 57 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 58 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 59 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 60 0x00,0x00,0x00,0x00,0x0C,0x7C,0xF0,0x80,\ 61 0x00,0x00,0x00,0xC0,0xF8,0x3C,0xFC,0xF0,\ 62 0x80,0x00,0x00,0x80,0xF0,0x7C,0x0C,0x00,\ 63 0xC0,0xF0,0xB8,0x98,0x8C,0x8C,0x8C,0x8C,\ 64 0x8C,0x98,0xF0,0xE0,0x00,0x00,0x00,0xFF,\ 65 0xFF,0x00,0x00,0x00,0xC0,0xF0,0x38,0x18,\ 66 0x0C,0x0C,0x0C,0x0C,0x0C,0x18,0x00,0x00,\ 67 0xC0,0xF0,0x38,0x18,0x0C,0x0C,0x0C,0x0C,\ 68 0x1C,0x38,0xF0,0xE0,0x00,0x00,0x00,0x00,\ 69 0xFC,0xFC,0x30,0x08,0x0C,0x0C,0x0C,0x1C,\ 70 0xF8,0xF0,0x38,0x18,0x0C,0x0C,0x0C,0x1C,\ 71 0xF8,0xF0,0x00,0x00,0x00,0xC0,0xF0,0xB8,\ 72 0x98,0x8C,0x8C,0x8C,0x8C,0x8C,0x98,0xF0,\ 73 0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 74 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 75 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 76 0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x1F,\ 77 0x78,0x78,0x3E,0x07,0x00,0x00,0x00,0x07,\ 78 0x3F,0x78,0x78,0x1F,0x03,0x00,0x00,0x00,\ 79 0x07,0x1F,0x39,0x71,0x61,0x61,0x61,0x61,\ 80 0x61,0x71,0x31,0x01,0x00,0x00,0x00,0x7F,\ 81 0x7F,0x00,0x00,0x00,0x07,0x1F,0x38,0x30,\ 82 0x60,0x60,0x60,0x60,0x60,0x30,0x00,0x00,\ 83 0x07,0x1F,0x38,0x70,0x60,0x60,0x60,0x60,\ 84 0x70,0x38,0x1F,0x07,0x00,0x00,0x00,0x00,\ 85 0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,\ 86 0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,\ 87 0x7F,0x7F,0x00,0x00,0x00,0x07,0x1F,0x39,\ 88 0x71,0x61,0x61,0x61,0x61,0x61,0x71,0x31,\ 89 0x01,0x00,0x00,0x70,0x70,0x70,0x00,0x00,\ 90 0x00,0x70,0x70,0x70,0x00,0x00,0x00,0x70,\ 91 0x70,0x70,0x00,0x00,0x00,0x00,0x00,0x00,\ 92 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 93 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 94 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 95 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 96 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 97 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 98 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 99 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 100 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 101 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 102 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 103 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 104 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 105 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 106 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 107 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 108 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 109 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 110 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 111 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 112 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 113 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 114 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 115 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 116 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 117 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 118 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 119 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 120 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 121 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 122 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 123 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 124 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 125 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 126 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 127 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 128 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 129 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 130 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 131 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 132 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 133 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 134 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 135 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\ 136 0x00,0x00,0x00,0x00,0x00,0x00,0x00) 137 138 x = 0 139 y = 0 140 141 for v in wel: 142 for i in range(8): 143 if (v & (1<<i)): 144 display.pixel(x,y,1) 145 else: 146 display.pixel(x,y,0) 147 i += 1 148 x += 8 149 150 if ((x//64) > 0): 151 x = 0 152 y += 1 153 154 sleep(2) 155 156 while True: 157 display.fill(0) 158 display.text(str(temp),60,20) 159 display.show() 160 if (temp >0): 161 temp -=1 162 else: 163 temp = 9 164 sleep(1)
----*----*----*----*----*----*----*----*----*----*----*----
史久超
email:shijiuchao1983@163.com
tel:15999550975 微信同号
qq:157185173 经常不在线,尽量微信