2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
// GND-Stromerde
// VCC ist an eine 5-V- oder 3,3-V-Stromversorgung angeschlossen
// D0 P1^4 (SCL)
// D1 P1^3 (SDA)
// RES ist mit P12 verbunden
//DC verbindet sich mit P11
//CS verbindet sich mit P10
OLED-Displays (Organic Light Emitting Diode) werden aufgrund ihres hohen Kontrastverhältnisses, ihres geringen Stromverbrauchs und ihrer schnellen Reaktionszeit häufig in verschiedenen elektronischen Geräten verwendet. Dieses Experiment ist darauf ausgelegt, einen Mikrocontroller zur Schnittstelle und Steuerung eines OLED-Displays zu verwenden. Der verwendete Anzeigetreiber ist SSD1306, ein häufig verwendeter Controller für kleine OLED-Bildschirme.
Das Videospeicherformat von OLED ist wie folgt:
[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;
}
Diese Verzögerungsfunktion implementiert Verzögerungen im Millisekundenbereich über eine einfache Schleife und wird verwendet, um einen stabilen Betrieb während der OLED-Initialisierung und -Steuerung sicherzustellen.
Je nach OLED-Modus werden Bytes über eine parallele oder serielle Schnittstelle auf die SSD1306 geschrieben. Der folgende Code demonstriert Schreibvorgänge im parallelen und seriellen Modus:
#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);
}
Ein- und Ausschalten des OLED-Displays:
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);
}
}
Zeigt ein Zeichen an der angegebenen Position an:
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++;
}
}
Der OLED-Initialisierungsprozess umfasst eine Reihe von Konfigurationsbefehlen, um sicherzustellen, dass das Display ordnungsgemäß funktioniert:
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