2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
// GND power ground
// VCC connects to 5V or 3.3v power supply
// D0 P1^4(SCL)
// D1 P1^3(SDA)
// RES connects to P12
//DC to P11
// CS connects to P10
OLED (Organic Light Emitting Diode) displays are widely used in various electronic devices due to their high contrast, low power consumption and fast response time. This experiment aims to interface and control an OLED display using a microcontroller. The display driver used is the SSD1306, which is a commonly used controller for small OLED screens.
The storage format of OLED video memory is as follows:
[0]0 1 2 3 ... 127
[1]0 1 2 3 ... 127
[2]0 1 2 3 ... 127
...
[7]0 1 2 3 ... 127
void delay_ms1(unsigned int ms) {
unsigned int a;
while (ms) {
a = 80;
while (a--);
ms--;
}
return;
}
This delay function implements millisecond-level delay through a simple loop, which is used to ensure stable operation during OLED initialization and control.
Depending on the OLED mode, bytes are written to the SSD1306 via the parallel or serial interface. The following code demonstrates the write operation in parallel and serial modes:
#if OLED_MODE == 1
// 并行模式
void OLED_WR_Byte(u8 dat, u8 cmd) {
DATAOUT(dat);
if (cmd) OLED_DC_Set();
else OLED_DC_Clr();
OLED_CS_Clr();
OLED_WR_Clr();
OLED_WR_Set();
OLED_CS_Set();
OLED_DC_Set();
}
#else
// 串行模式
void OLED_WR_Byte(u8 dat, u8 cmd) {
u8 i;
if (cmd) OLED_DC_Set();
else OLED_DC_Clr();
OLED_CS_Clr();
for (i = 0; i < 8; i++) {
OLED_SCLK_Clr();
if (dat & 0x80) OLED_SDIN_Set();
else OLED_SDIN_Clr();
OLED_SCLK_Set();
dat <<= 1;
}
OLED_CS_Set();
OLED_DC_Set();
}
#endif
void OLED_Set_Pos(unsigned char x, unsigned char y) {
OLED_WR_Byte(0xb0 + y, OLED_CMD);
OLED_WR_Byte(((x & 0xf0) >> 4) | 0x10, OLED_CMD);
OLED_WR_Byte((x & 0x0f) | 0x01, OLED_CMD);
}
To turn the OLED display on and off:
void OLED_Display_On(void) {
OLED_WR_Byte(0X8D, OLED_CMD); // SET DCDC命令
OLED_WR_Byte(0X14, OLED_CMD); // DCDC ON
OLED_WR_Byte(0XAF, OLED_CMD); // DISPLAY ON
}
void OLED_Display_Off(void) {
OLED_WR_Byte(0X8D, OLED_CMD); // SET DCDC命令
OLED_WR_Byte(0X10, OLED_CMD); // DCDC OFF
OLED_WR_Byte(0XAE, OLED_CMD); // DISPLAY OFF
}
void OLED_Clear(void) {
u8 i, n;
for (i = 0; i < 8; i++) {
OLED_WR_Byte(0xb0 + i, OLED_CMD); // 设置页地址(0~7)
OLED_WR_Byte(0x00, OLED_CMD); // 设置显示位置—列低地址
OLED_WR_Byte(0x10, OLED_CMD); // 设置显示位置—列高地址
for (n = 0; n < 128; n++) OLED_WR_Byte(0, OLED_DATA);
}
}
Display a character at the specified position:
void OLED_ShowChar(u8 x, u8 y, u8 chr) {
unsigned char c = 0, i = 0;
c = chr - ' ';
if (x > Max_Column - 1) {
x = 0; y = y + 2;
}
if (SIZE == 16) {
OLED_Set_Pos(x, y);
for (i = 0; i < 8; i++) OLED_WR_Byte(F8X16[c * 16 + i], OLED_DATA);
OLED_Set_Pos(x, y + 1);
for (i = 0; i < 8; i++) OLED_WR_Byte(F8X16[c * 16 + i + 8], OLED_DATA);
} else {
OLED_Set_Pos(x, y + 1);
for (i = 0; i < 6; i++) OLED_WR_Byte(F6x8[c][i], OLED_DATA);
}
}
void OLED_ShowString(u8 x, u8 y, u8 *chr) {
unsigned char j = 0;
while (chr[j] != '0') {
OLED_ShowChar(x, y, chr[j]);
x += 8;
if (x > 120) {
x = 0; y += 2;
}
j++;
}
}
The OLED initialization process involves a series of configuration commands to ensure the display works correctly:
void OLED_Init(void) {
OLED_RST_Set();
delay_ms1(100);
OLED_RST_Clr();
delay_ms1(100);
OLED_RST_Set();
OLED_WR_Byte(0xAE, OLED_CMD); // 关闭OLED面板
OLED_WR_Byte(0x00, OLED_CMD); // 设置低列地址
OLED_WR_Byte(0x10, OLED_CMD); // 设置高列地址
OLED_WR_Byte(0x40, OLED_CMD); // 设置起始行地址
OLED_WR_Byte(0x81, OLED_CMD); // 设置对比度控制寄存器
OLED_WR_Byte(0xCF, OLED_CMD); // 设置SEG输出电流亮度
OLED_WR_Byte(0xA1, OLED_CMD); // 设置段/列映射
OLED_WR_Byte(0xC8, OLED_CMD); // 设置COM/行扫描方向
OLED_WR_Byte(0xA6, OLED_CMD); // 设置正常显示
OLED_WR_Byte(0xA8, OLED_CMD); // 设置多路复用比率
OLED_WR_Byte(0x3f, OLED_CMD); // 1/64 duty
OLED_WR_Byte(0xD3, OLED_CMD); // 设置显示偏移
OLED_WR_Byte(0x00, OLED_CMD); // 不偏移
OLED_WR_Byte(0xd5, OLED_CMD); // 设置显示时钟分频比/振荡频率
OLED_WR_Byte(0x80, OLED_CMD); // 设置时钟为100帧/秒
OLED_WR_Byte(0xD9, OLED_CMD); // 设置预充电周期
OLED_WR_Byte(0xF1, OLED_CMD); // 设置预充电为15时钟和放电为1时钟
OLED_WR_Byte(0xDA, OLED_CMD); // 设置COM引脚硬件配置
OLED_WR_Byte(0x12, OLED_CMD);
OLED_WR_Byte(0xDB, OLED_CMD); // 设置VCOMH
OLED_WR_Byte(0x40, OLED_CMD); // 设置VCOM解除电平
OLED_WR_Byte(0x20, OLED_CMD); // 设置页地址模式
OLED_WR_Byte(0x02, OLED_CMD);
OLED_WR_Byte(0x8D, OLED_CMD); // 设置电荷泵使能/禁用
OLED_WR_Byte(0x14, OLED_CMD); // 设置(0x10)禁用
OLED_WR_Byte(0xA4, OLED_CMD); // 禁用整个显示
OLED_WR_Byte(0xA6, OLED_CMD); // 禁用反向显示
OLED_WR_Byte(0xAF, OLED_CMD); // 打开OLED面板
OLED_Clear();
OLED_Set_Pos(0, 0);
}
https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2