STM32F1单片机-OLED显示屏
一、OLED显示屏介绍
- OLED:有机发光二极管,主要有0.96寸(SSD1306/1315)和1.3寸(SH116)显示屏
- 0.96寸OLED模块如下图所示,分别为4针(4行16列)和6针(4行16列)
- 供电:3-5.5V,通信协议I2C(4针)/SPI(6针),分辨率:128*64
下图分别为4针和6针OLED接线图
二、OLED显示汉字
- OLED显示汉字,需要事先定义好汉字字符数据,在函数中汉字都是UTF-8编码格式的,需要在控制栏里填入如下数据,开启对UTF-8汉字写入代码操作的支持
- 定义汉字数组和图片数组,使用如下软件
设置如下
将字模数据按格式复制到OLED.Data.c,就可以显示出想要的汉字
三、OLED显示图像
- OLED显示图像,事先定义好图像数组,取模方式同样采用上述软件,步骤如下,每次打开软件时,都需要事先设置好配置
最后将图像数据按格式复制到OLED.Data.c,就可以显示出所画的图像
四、编程
4.1 显示字符
下面是OLED.h
void OLED_Init(void);
void OLED_Clear(void);
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
4.2 显示汉字和图像
得到字模数据以后,在OLED.Data.c定义好
const ChineseCell_t OLED_CF16x16[] = {
",",
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x58,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
"。",
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x24,0x24,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
"你",
0x00,0x80,0x60,0xF8,0x07,0x40,0x20,0x18,0x0F,0x08,0xC8,0x08,0x08,0x28,0x18,0x00,
0x01,0x00,0x00,0xFF,0x00,0x10,0x0C,0x03,0x40,0x80,0x7F,0x00,0x01,0x06,0x18,0x00,
"好",
0x10,0x10,0xF0,0x1F,0x10,0xF0,0x00,0x80,0x82,0x82,0xE2,0x92,0x8A,0x86,0x80,0x00,
0x40,0x22,0x15,0x08,0x16,0x61,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,
"世",
0x20,0x20,0x20,0xFE,0x20,0x20,0xFF,0x20,0x20,0x20,0xFF,0x20,0x20,0x20,0x20,0x00,
0x00,0x00,0x00,0x7F,0x40,0x40,0x47,0x44,0x44,0x44,0x47,0x40,0x40,0x40,0x00,0x00,
"界",
0x00,0x00,0x00,0xFE,0x92,0x92,0x92,0xFE,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,0x00,
0x08,0x08,0x04,0x84,0x62,0x1E,0x01,0x00,0x01,0xFE,0x02,0x04,0x04,0x08,0x08,0x00,
"苏",
0x00,0x00,0x10,0x10,0x10,0x1C,0x30,0xD0,0x90,0x98,0x8C,0x08,0x08,0x00,0x00,0x00,
0x00,0x00,0x0C,0x26,0x11,0x0D,0x03,0x20,0x60,0x38,0x07,0x04,0x04,0x0C,0x00,0x00,
"州",
0x00,0x00,0x80,0x00,0xF8,0x00,0xC0,0x00,0xF8,0x00,0xC0,0x02,0xFC,0x00,0x00,0x00,
0x00,0x00,0x21,0x18,0x07,0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x7F,0x00,0x00,0x00,
"科",
0x00,0x80,0x80,0x90,0xD0,0xF8,0x44,0x40,0x40,0xD0,0x00,0xFC,0x00,0x00,0x00,0x00,
0x00,0x08,0x04,0x02,0x01,0x3F,0x01,0x02,0x02,0x02,0x02,0xFF,0x01,0x01,0x01,0x00,
"技",
0x00,0x00,0x40,0x40,0xD2,0x2C,0xA0,0x00,0x20,0xE0,0x3C,0x90,0x10,0x00,0x00,0x00,
0x00,0x04,0x04,0x02,0x3E,0x01,0x20,0x23,0x25,0x15,0x0D,0x13,0x20,0x20,0x20,0x00,
"大",
0x00,0x00,0x00,0x80,0x80,0x80,0x80,0xFC,0x80,0x40,0x40,0x40,0x40,0x00,0x00,0x00,
0x00,0x00,0x00,0x40,0x20,0x10,0x0C,0x03,0x06,0x08,0x30,0x20,0x20,0x40,0x00,0x00,
"学",
0x00,0x00,0xC0,0x40,0x20,0xAC,0xA0,0xA4,0xE0,0x98,0x26,0x22,0x70,0x20,0x20,0x00,
0x00,0x00,0x04,0x04,0x04,0x04,0x44,0x45,0x3E,0x02,0x02,0x02,0x02,0x02,0x00,0x00,
/*按照上面的格式,在这个位置加入新的汉字数据*/
//...
/*未找到指定汉字时显示的默认图形(一个方框,内部一个问号),请确保其位于数组最末尾*/
"",
0xFF,0x01,0x01,0x01,0x31,0x09,0x09,0x09,0x09,0x89,0x71,0x01,0x01,0x01,0x01,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x80,0x80,0x96,0x81,0x80,0x80,0x80,0x80,0x80,0x80,0xFF,
};
/*测试图像(一个方框,内部一个二极管符号),宽16像素,高16像素*/
const uint8_t Diode[] = {
0xFF,0x01,0x81,0x81,0x81,0xFD,0x89,0x91,0xA1,0xC1,0xFD,0x81,0x81,0x81,0x01,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x9F,0x88,0x84,0x82,0x81,0x9F,0x80,0x80,0x80,0x80,0xFF,
};
const uint8_t Xin[] = {
0xE0,0x30,0x18,0x18,0x10,0x10,0x30,0x20,0x60,0xC0,0x80,0xC0,0x60,0x20,0x30,0x10,
0x18,0x18,0x30,0xE0,0x03,0x0C,0x38,0x60,0x40,0xC0,0x80,0x00,0x00,0x00,0x01,0x00,
0x00,0x80,0xC0,0x40,0x60,0x38,0x0C,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,
0x02,0x06,0x04,0x06,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
}; //新加的星星图标
主函数中调用显示函数
#include "stm32f10x.h" // Device header
#include "OLED.h"
//编码格式UTF-8
int main(void)
{
OLED_Init();//OLED初始化
OLED_ShowChar(0,0,'A',OLED_8X16); //OLED_8X16点阵 字体大小
OLED_ShowChar(0,20,'B',OLED_8X16);
OLED_ShowString(20,0,"Hello world",OLED_8X16);
OLED_ShowNum(0,40,520,3,OLED_8X16);
OLED_ShowChinese(20,20,"你好,世界。");
OLED_ShowImage(55,20,20,20,Xin);
OLED_Printf(0,0,OLED_8X16,"NUM1:%d,NUM2:%d",123,-456);
OLED_DrawPoint(0,0);
OLED_Clear();
OLED_ShowChinese(15,0,"苏州科技大学");
OLED_Update();//OLED显示必须添加更新函数
while(1)
{
}
}