基于C51单片机控制的OLED12864屏幕的显示代码
时间: 2023-10-03 22:07:06 浏览: 151
以下是一个基于C51单片机控制OLED 12864屏幕的显示代码的示例:
```c
#include <reg51.h>
#include <intrins.h>
#define LCD_SCL P1_0 // OLED串行时钟线
#define LCD_SDA P1_1 // OLED串行数据线
void delay_us(unsigned int t) {
while(t--);
}
void OLED_write_cmd(unsigned char cmd) {
LCD_SCL = 0;
LCD_SDA = 0;
LCD_SCL = 1;
LCD_SDA = 0;
LCD_SCL = 0;
_nop_();
for(int i = 0; i < 8; i++) {
LCD_SDA = (cmd >> i) & 0x01;
LCD_SCL = 1;
_nop_();
LCD_SCL = 0;
_nop_();
}
}
void OLED_write_data(unsigned char dat) {
LCD_SCL = 0;
LCD_SDA = 1;
LCD_SCL = 1;
LCD_SDA = 0;
LCD_SCL = 0;
_nop_();
for(int i = 0; i < 8; i++) {
LCD_SDA = (dat >> i) & 0x01;
LCD_SCL = 1;
_nop_();
LCD_SCL = 0;
_nop_();
}
}
void OLED_init() {
OLED_write_cmd(0xAE); // 关闭OLED显示
OLED_write_cmd(0x00); // 设置低列地址
OLED_write_cmd(0x10); // 设置高列地址
OLED_write_cmd(0x40); // 设置显示开始行
OLED_write_cmd(0xB0); // 设置页地址
OLED_write_cmd(0x81); // 设置对比度控制
OLED_write_cmd(0xCF); // 设置电荷泵倍率
OLED_write_cmd(0xA1); // 设置段重新映射
OLED_write_cmd(0xA6); // 设置正常/反向显示
OLED_write_cmd(0xA8); // 设置多重比例
OLED_write_cmd(0x3F); // 设置MUX比率
OLED_write_cmd(0xC8); // 设置COM扫描方向
OLED_write_cmd(0xD3); // 设置显示偏移
OLED_write_cmd(0x00); // 不偏移
OLED_write_cmd(0xD5); // 设置显示时钟分频
OLED_write_cmd(0x80); // 100分频
OLED_write_cmd(0xD9); // 设置预充电周期
OLED_write_cmd(0xF1); // PHASE 1: 2D, PHASE 2: 5D
OLED_write_cmd(0xDA); // 设置COM引脚硬件配置
OLED_write_cmd(0x12);
OLED_write_cmd(0xDB); // 设置VCOMH电压倍率
OLED_write_cmd(0x40);
OLED_write_cmd(0x8D); // 设置电荷泵使能/禁用
OLED_write_cmd(0x14);
OLED_write_cmd(0xAF); // 打开OLED显示
}
void OLED_clear() {
for(int i = 0; i < 8; i++) {
OLED_write_cmd(0xB0 + i);
OLED_write_cmd(0x00);
OLED_write_cmd(0x10);
for(int j = 0; j < 128; j++)
OLED_write_data(0x00);
}
}
void OLED_display(unsigned char page, unsigned char column, unsigned char *text) {
OLED_write_cmd(0xB0 + page);
OLED_write_cmd(((column & 0xF0) >> 4) | 0x10);
OLED_write_cmd(column & 0x0F);
for(int i = 0; i < 8; i++)
OLED_write_data(text[i]);
}
void main() {
OLED_init();
OLED_clear();
unsigned char text[8] = {
0x00, 0x3C, 0x42, 0x42, 0x3C, 0x00, 0x00, 0x00
};
OLED_display(0, 0, text);
while(1);
}
```
请注意,该代码假设你已将OLED 12864屏幕的串行时钟线连接到C51单片机的P1.0引脚,串行数据线连接到P1.1引脚。你可以根据实际情况进行调整。确保在使用该代码之前,你已经正确地连接了C51单片机与OLED 12864屏幕。
阅读全文