Arduino和C51开发OLED显示屏
技术:51单片机、Arduino、OLED显示屏、U8glib
概述
OLED显示屏常常用作为智能产品的显示设备,本文简单介绍OLED显示屏的使用方法。
详细
一、OLED显示屏
OLED显示屏是利用有机电自发光二极管制成的显示屏。由于同时具备自发光有机电激发光二极管,不需背光源、对比度高、厚度薄、视角广、反应速度快、可用于挠曲性面板、使用温度范围广、构造及制程较简单等优异之特性,被认为是下一代的平面显示器新兴应用技术。
这里我们使用的0.96寸的OLED显示屏,它的显示区域是128*64的点阵,每个点都能自己发光。OLED显示屏可以显示汉字、字符和图案等,智能手环和智能手表等智能设备一般都是选择OLED显示屏来作为显示设备。
二、51单片机 — OLED显示屏
硬件连接:
这里简单介绍OLED显示中文:
1. 打开PCtoLCD2002.exe -> 选项,修改一些选项:
2. 输入你想要显示的汉字,点击->生成字模,复制下面的数组即可:
我这里输入的是:”显示屏测试程序”,对应数组为:
{0x00,0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,0x00}, {0x40,0x42,0x44,0x58,0x40,0x7F,0x40,0x40,0x40,0x7F,0x40,0x50,0x48,0x46,0x40,0x00},/*"显",0*/ {0x40,0x40,0x42,0x42,0x42,0x42,0x42,0xC2,0x42,0x42,0x42,0x42,0x42,0x40,0x40,0x00}, {0x20,0x10,0x08,0x06,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x02,0x04,0x08,0x30,0x00},/*"示",1*/ {0x00,0x00,0xFE,0x12,0x92,0xB2,0xD2,0x92,0x92,0x92,0xD2,0xB2,0x9E,0x00,0x00,0x00}, {0x40,0x30,0x0F,0x04,0x84,0x64,0x1F,0x04,0x04,0x04,0xFF,0x04,0x04,0x04,0x00,0x00},/*"屏",2*/ {0x10,0x60,0x02,0x8C,0x00,0xFE,0x02,0xF2,0x02,0xFE,0x00,0xF8,0x00,0xFF,0x00,0x00}, {0x04,0x04,0x7E,0x01,0x80,0x47,0x30,0x0F,0x10,0x27,0x00,0x47,0x80,0x7F,0x00,0x00},/*"测",3*/ {0x40,0x40,0x42,0xCC,0x00,0x90,0x90,0x90,0x90,0x90,0xFF,0x10,0x11,0x16,0x10,0x00}, {0x00,0x00,0x00,0x3F,0x10,0x28,0x60,0x3F,0x10,0x10,0x01,0x0E,0x30,0x40,0xF0,0x00},/*"试",4*/ {0x24,0x24,0xA4,0xFE,0x23,0x22,0x00,0x3E,0x22,0x22,0x22,0x22,0x22,0x3E,0x00,0x00}, {0x08,0x06,0x01,0xFF,0x01,0x06,0x40,0x49,0x49,0x49,0x7F,0x49,0x49,0x49,0x41,0x00},/*"程",5*/ {0x00,0x00,0xFC,0x04,0x04,0x04,0x14,0x15,0x56,0x94,0x54,0x34,0x14,0x04,0x04,0x00}, {0x40,0x30,0x0F,0x00,0x01,0x01,0x01,0x41,0x81,0x7F,0x01,0x01,0x01,0x05,0x03,0x00},/*"序",6*/
想要显示出来的话,要把这些数组放到oledfont.h文件下:
main函数代码:
int main(void) { u8 t; OLED_Init(); //初始化OLED OLED_Clear(); //清屏 t=' '; while(1) { OLED_ShowCHinese(0,0,0); //0:显 OLED_ShowCHinese(18,0,1);//1:示 OLED_ShowCHinese(36,0,2);//2:屏 OLED_ShowCHinese(54,0,3);//3:测 OLED_ShowCHinese(72,0,4);//4:试 OLED_ShowCHinese(90,0,5);//5:程 OLED_ShowCHinese(108,0,6);//6:序 OLED_ShowString(0,2,"1.3' OLED TEST"); //打印字符串 OLED_ShowString(20,4,"2018/12/25"); OLED_ShowString(0,6,"ASCII:"); OLED_ShowString(63,6,"CODE:"); OLED_ShowChar(48,6,t);//显示ASCII字符 t++; if(t>'~') t=' '; OLED_ShowNum(103,6,t,3,16);//显示ASCII字符的码值 delay1s(); } }
程序显示效果如下:
三、Arduino — OLED显示屏
硬件连接:
加载库文件:
打开Arduino IDE -> 项目 -> 加载库 -> 管理库中搜索U8glib,然后安装即可。
OLED显示屏显示代码:
#include "U8glib.h" U8GLIB_SSD1306_128X64 u8g(10, 9, 12, 11, 13); //创建对象 void setup(void) { } void loop(void) { for(int i=10;i<80;i++){ u8g.firstPage(); //表示图像循环的开始 do { u8g.setFont(u8g_font_fub30);//设置字体和自号 u8g.setPrintPos(70, 50); //显示的位置 u8g.print(i);//显示变量i的值 u8g.setFont(u8g_font_fub30);//设置字体和自号 u8g.setPrintPos(20, 50); //显示的位置 u8g.print("i=");//显示 "i = " }while( u8g.nextPage() );//u8g.nextPage() 表示图像循环的结束 delay(500);//显示的时间间隔 } }
效果图如下:
四、总结
本文简单描述了OLED显示屏,并教大家如何用C51和Arduino进行开发。OLED显示屏显示效果非常的好,所以常常也被用来做一些智能设备的显示屏,下篇文章我也会教大家如何在OLED显示屏上显示时钟、温湿度值和光照强度等信息。
五、项目结构图