OLED(有机发光二极管 Organic Light-Emitting Diode)是一种显示技术,它使用有机化合物材料作为发光层,当通过电流时,这些材料会发光。OLED显示器由多个单独可控制的OLED像素组成,每个像素都能够发出红、绿、蓝(RGB)中的一种颜色,或者通过不同的颜色组合来显示不同的颜色。
SSD1306是一个单片CMOS OLED/PLED驱动芯片,用于驱动有机发光二极管(OLED)或聚合发光二极管(PLED)显示器。它由Solomon Systech公司开发,广泛应用于便携式设备、消费电子产品和工业显示设备中。SSD1306支持多种接口方式,包括SPI、I2C等,使其易于与微控制器或其他逻辑电路连接。
以下是SSD1306的一些主要特点和功能:
-
显示内存:SSD1306内置64KB的显示内存,用于存储要显示的图像数据。显示内存通常分为多个页面,每个页面包含若干列。在页寻址模式下,可以单独写入每个页面;在垂直寻址模式下,可以按列写入数据。
-
命令集:SSD1306定义了一系列命令,用于控制显示器的各种功能,如显示开启/关闭、页面寻址模式设置、列寻址模式设置、显示起始行设置、对比度设置等。
-
数据传输:数据可以通过SPI或I2C接口传输到SSD1306。在SPI模式下,通常有两条线用于数据(MOSI)和时钟(SCK),以及两条控制线(CS和DC)。在I2C模式下,使用两条线(SDA和SCL)来进行数据传输。
-
显示参数:可以通过命令设置显示的参数,如显示方向、显示起始地址、分割比例等。
-
电源管理:SSD1306提供了电源管理功能,包括正常模式、部分显示模式和睡眠模式等,以降低功耗。
-
支持多种尺寸和分辨率:SSD1306支持多种OLED显示器的尺寸和分辨率,如128x64、128x32等。
SSD1306的内存结构
是由一系列的页面和列组成的,它被设计来存储用于控制OLED显示器的像素数据。具体来说,SSD1306的内存可以被看作是一个二维的数组,其中每一行代表一个页面,每一列代表显示器上的一个垂直像素列。
对于常见的128x64像素的SSD1306显示屏,其内存结构如下:
-
页面(Page):显示内存被分为8页,每页包含128列。每列包含8位,对应于显示屏上的一个垂直像素列。因此,整个显示区域可以看作是8页(Page 0到Page 7)。
-
列(Column):每页有128列,列地址范围是0到127。
-
字节(Byte):每个字节中的8位对应于显示屏上的一列的8个像素。如果某一位是1,对应的像素将被点亮;如果是0,像素则不亮。
例如,如果你想在屏幕的左上角显示一个像素,你需要写入Page 0的Column 0的第一个字节,将该字节的最高位设置为1。
在编程时,你需要指定要写入的页地址和列地址,然后发送相应的数据来控制显示。通常,这涉及到以下
步骤:
- 设置页地址(0x22命令),指定要写入的页。
- 设置列地址的高位和低位(0x21命令),指定要写入的列。
- 写入数据(0x40控制字节后跟数据),这些数据将存储在指定的页和列地址中。
SSD1306还支持不同的寻址模式,包括页面寻址模式和水平寻址模式。在页面寻址模式下,你可以分别写入每个页面的数据;在水平寻址模式下,你可以按水平方向连续写入数据。
指令
常用的SSD1306指令:
-
设置显示开启/关闭 (Set Display ON/OFF)
- 指令:0xAE (关闭) 或 0xAF (开启)
-
设置显示时钟分频/振荡器频率 (Set Display Clock Divide Ratio/Oscillator Frequency)
- 指令:0xD5
- 通常是跟一个字节的数据一起发送,用于设置时钟分频比和振荡器频率。
-
设置多路复用比 (Set Multiplex Ratio)
- 指令:0xA8
- 后跟一个字节的数据,用于设置显示的复用比(即显示的行数)。
-
设置显示偏移 (Set Display Offset)
- 指令:0xD3
- 后跟一个字节的数据,用于设置显示的垂直偏移。
-
设置显示起始行 (Set Display Start Line)
- 指令:0x40 | [0x00 - 0x3F]
- 用于设置显示的起始行,可以是0到63之间的任意值。
-
设置电荷泵 (Set Charge Pump)
- 指令:0x8D
- 后跟一个字节的数据,用于开启或关闭内置电荷泵。
-
设置内存地址模式 (Set Memory Addressing Mode)
- 指令:0x20
- 后跟一个字节的数据,用于设置内存地址的寻址模式。
-
设置列地址 (Set Column Address)
- 指令:0x21
- 后跟两个字节的数据,用于设置列地址的起始和结束地址。
-
设置页地址 (Set Page Address)
- 指令:0x22
- 后跟两个字节的数据,用于设置页地址的起始和结束地址。
-
水平/垂直滚动设置 (Horizontal/Vertical Scroll Setup)
- 指令:0x26 (水平滚动) 或 0x27 (垂直滚动)
- 后跟多个字节的数据,用于设置滚动的参数。
-
连续水平/垂直滚动 (Continuous Horizontal/Vertical Scroll)
- 指令:0x2A (水平滚动) 或 0x29 (垂直滚动)
- 用于启动或停止连续滚动。
-
设置对比度 (Set Contrast Control)
- 指令:0x81
- 后跟一个字节的数据,用于设置显示对比度。
-
整个显示区域点亮 (Entire Display ON)
- 指令:0xA4 (正常显示) 或 0xA5 (整个显示区域点亮)
-
正常/反向显示 (Normal/Inverse Display)
- 指令:0xA6 (正常显示) 或 0xA7 (反向显示)
-
开启/关闭充电周期 (Enable/Disable Charge Pump)
- 指令:0x8D
- 后跟一个字节的数据,用于开启或关闭充电周期。
显示OLED库
使用HAL的I2C1
oled.c
#include "oled.h"
//初始化指令
uint8_t CMD_Data[]={
0xAE, 0x00, 0x10, 0x40, 0xB0, 0x81, 0xFF, 0xA1, 0xA6, 0xA8, 0x3F,
0xC8, 0xD3, 0x00, 0xD5, 0x80, 0xD8, 0x05, 0xD9, 0xF1, 0xDA, 0x12,
0xD8, 0x30, 0x8D, 0x14, 0xAF};
//写指令
void WriteCmd(){
uint8_t i=0;
for(i=0;i<27;i++){
HAL_I2C_Mem_Write(&hi2c1,0x78,0x00,I2C_MEMADD_SIZE_8BIT,CMD_Data+i,1,0x100);
}
}
//向设备写控制命令
void OLED_WR_CMD(uint8_t cmd)
{
HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x00,I2C_MEMADD_SIZE_8BIT,&cmd,1,0x100);
}
//向设备写数据
void OLED_WR_DATA(uint8_t data)
{
HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x40,I2C_MEMADD_SIZE_8BIT,&data,1,0x100);
}
//初始化oled屏幕
void OLED_Init(void)
{
HAL_Delay(200);
WriteCmd();
}
//清屏size12 size16要清两行,其他函数有类似情况
void OLED_Clear()
{
uint8_t i,n;
for(i=0;i<8;i++)
{
OLED_WR_CMD(0xb0+i);
OLED_WR_CMD (0x00);
OLED_WR_CMD (0x10);
for(n=0;n<128;n++)
OLED_WR_DATA(0);
}
}
//清行
void OLED_Clearrow(uint8_t i)
{
uint8_t n;
OLED_WR_CMD(0xb0+i);
OLED_WR_CMD (0x00);
OLED_WR_CMD (0x10);
for(n=0;n<128;n++)
OLED_WR_DATA(0);
}
//开启OLED显示
void OLED_Display_On(void)
{
OLED_WR_CMD(0X8D); //SET DCDC命令
OLED_WR_CMD(0X14); //DCDC ON
OLED_WR_CMD(0XAF); //DISPLAY ON
}
//关闭OLED显示
void OLED_Display_Off(void)
{
OLED_WR_CMD(0X8D); //SET DCDC命令
OLED_WR_CMD(0X10); //DCDC OFF
OLED_WR_CMD(0XAE); //DISPLAY OFF
}
//设置光标位置
//即要写入的像素的列和页地址。
//这个函数接受两个参数:x 和 y,分别代表列地址和页地址。
void OLED_Set_Pos(uint8_t x, uint8_t y)
{
OLED_WR_CMD(0xb0+y);
OLED_WR_CMD(((x&0xf0)>>4)|0x10);
OLED_WR_CMD(x&0x0f);
}
//将所有像素点亮
void OLED_On(void)
{
for (uint8_t page = 0; page < 8; page++) { // SSD1306有8页
OLED_WR_CMD(0xB0 + page); // 设置页地址
OLED_WR_CMD(0x00); // 设置列地址的低四位为0
OLED_WR_CMD(0x10); // 设置列地址的高四位为0
for (uint8_t col = 0; col < 128; col++) {
OLED_WR_DATA(0xFF); // 写入最大亮度值(0xFF)到每个列
}
}
}
//将所有像素熄灭
void OLED_No(void)
{
for (uint8_t page = 0; page < 8; page++) { // SSD1306有8页
OLED_WR_CMD(0xB0 + page); // 设置页地址
OLED_WR_CMD(0x00); // 设置列地址的低四位为0
OLED_WR_CMD(0x10); // 设置列地址的高四位为0
for (uint8_t col = 0; col < 128; col++) {
OLED_WR_DATA(0x00); // 写入最大亮度值(0xFF)到每个列
}
}
}
/************************************6*8的点阵************************************/
const unsigned char F6x8[][6] =
{
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // sp 0
{ 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 }, // ! 1
{ 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 }, // " 2
{ 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 }, // # 3
{ 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 }, // $ 4
{ 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 }, // % 5
{ 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 }, // & 6
{ 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 }, // ' 7
{ 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 }, // ( 8
{ 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 }, // ) 9
{ 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 }, // * 10
{ 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 }, // + 11
{ 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 }, // , 12
{ 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 }, // - 13
{ 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 }, // . 14
{ 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 }, // / 15
{ 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E }, // 0 16
{ 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 }, // 1 17
{ 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 }, // 2 18
{ 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 }, // 3 19
{ 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 }, // 4 20
{ 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 }, // 5 21
{ 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 }, // 6 22
{ 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 }, // 7 23
{ 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 }, // 8 24
{ 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E }, // 9 25
{ 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 }, // : 26
{ 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 }, // ; 27
{ 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 }, // < 28
{ 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 }, // = 29
{ 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 }, // > 30
{ 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 }, // ? 31
{ 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E }, // @ 32
{ 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C }, // A 33
{ 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 }, // B 34
{ 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 }, // C 35
{ 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C }, // D 36
{ 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 }, // E 37
{ 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 }, // F 38
{ 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A }, // G 39
{ 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F }, // H 40
{ 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 }, // I 41
{ 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 }, // J 42
{ 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 }, // K 43
{ 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 }, // L 44
{ 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F }, // M 45
{ 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F }, // N 46
{ 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E }, // O 47
{ 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 }, // P 48
{ 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E }, // Q 49
{ 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 }, // R 50
{ 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 }, // S 51
{ 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 }, // T 52
{ 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F }, // U 53
{ 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F }, // V 54
{ 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F }, // W 55
{ 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 }, // X 56
{ 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 }, // Y 57
{ 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 }, // Z 58
{ 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 }, // [ 59
{ 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55 }, // 55 60
{ 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 }, // ] 61
{ 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 }, // ^ 62
{ 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 }, // _ 63
{ 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 }, // ' 64
{ 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 }, // a 65
{ 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 }, // b 66
{ 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 }, // c 67
{ 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F }, // d 68
{ 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 }, // e 69
{ 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 }, // f 70
{ 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C }, // g 71
{ 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 }, // h 72
{ 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 }, // i 73
{ 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 }, // j 74
{ 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 }, // k 75
{ 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 }, // l 76
{ 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 }, // m 77
{ 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 }, // n 78
{ 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 }, // o 79
{ 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 }, // p 80
{ 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC }, // q 81
{ 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 }, // r 82
{ 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 }, // s 83
{ 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 }, // t 84
{ 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C }, // u 85
{ 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C }, // v 86
{ 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C }, // w 87
{ 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 }, // x 88
{ 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C }, // y 89
{ 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 }, // z 90
{ 0x14, 0x14, 0x14, 0x14, 0x14, 0x14 } // horiz lines
};
const unsigned char F8x16[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};
unsigned int oled_pow(uint8_t m,uint8_t n)
{
unsigned int result=1;
while(n--)result*=m;
return result;
}
//在指定位置显示一个字符,包括部分字符
//x:0~127
//y:0~63
//mode:0,反白显示;1,正常显示
//size:选择字体 16/12
void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size)
{
unsigned char c=0,i=0;
c=chr-' ';//得到偏移后的值
if(x>128-1){x=0;y=y+2;}
if(Char_Size ==16)
{
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
OLED_WR_DATA(F8x16[c*16+i]);
OLED_Set_Pos(x,y+1);
for(i=0;i<8;i++)
OLED_WR_DATA(F8x16[c*16+i+8]);
}
else {
OLED_Set_Pos(x,y);
for(i=0;i<6;i++)
OLED_WR_DATA(F6x8[c][i]);
}
}
//显示2个数字
//x,y :起点坐标
//len :数字的位数
//size:字体大小
//mode:模式 0,填充模式;1,叠加模式
//num:数值(0~4294967295);
void OLED_ShowNum(uint8_t x,uint8_t y,unsigned int num,uint8_t len,uint8_t size2)
{
uint8_t t,temp;
uint8_t enshow=0;
for(t=0;t<len;t++)
{
temp=(num/oled_pow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
OLED_ShowChar(x+(size2/2)*t,y,' ',size2);
continue;
}else enshow=1;
}
OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2);
}
}
//显示一个字符号串
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
{
unsigned char j=0;
while (chr[j]!='\0')
{ OLED_ShowChar(x,y,chr[j],Char_Size);
x+=8;
if(x>120){x=0;y+=2;}
j++;
}
}
oled.h
#ifndef __OLED_H
#define __OLED_H
#include "i2c.h"
#include "stdio.h"
//初始化oled屏幕
void OLED_Init(void);
//开启OLED显示
void OLED_Display_On(void);
//关闭OLED显示
void OLED_Display_Off(void);
//将所有像素点亮
void OLED_On(void);
//将所有像素熄灭
void OLED_No(void);
//写指令
void WriteCmd(void);
//向设备写控制命令
void OLED_WR_CMD(uint8_t cmd);
//向设备写数据
void OLED_WR_DATA(uint8_t data);
//清屏
void OLED_Clear(void);
//清行
void OLED_Clearrow(uint8_t i);
//设置光标
void OLED_Set_Pos(uint8_t x, uint8_t y);
//幂函数
unsigned int oled_pow(uint8_t m,uint8_t n);
//在指定位置显示一个字符,包括部分字符
//x:0~127
//y:0~63
//mode:0,反白显示;1,正常显示
//size:选择字体 16/12
void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size);
//显示2个数字
//x,y :起点坐标
//len :数字的位数
//size:字体大小
//mode:模式 0,填充模式;1,叠加模式
//num:数值(0~4294967295);
void OLED_ShowNum(uint8_t x,uint8_t y,unsigned int num,uint8_t len,uint8_t size2);
//显示一个字符号串
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size);
#endif
main.h文件
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "oled.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_I2C1_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
OLED_Init();
OLED_No();//全部熄灭
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
OLED_Display_On();//开启显示
//OLED_On();//全部点亮
uint8_t a[3]={"li"};
OLED_ShowString(12,3,a,2);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */