0.96寸oled显示屏4针51的程序
时间: 2023-09-09 09:09:49 浏览: 130
以下是0.96寸OLED显示屏4针51的程序示例:
```c
#include <reg51.h>
#define SCL P1_0
#define SDA P1_1
void delay(unsigned int x)
{
unsigned int i, j;
for (i = 0; i < x; i++)
for (j = 0; j < 120; j++);
}
void start(void)
{
SDA = 1;
SCL = 1;
delay(50);
SDA = 0;
delay(50);
SCL = 0;
}
void stop(void)
{
SDA = 0;
SCL = 1;
delay(50);
SDA = 1;
delay(50);
}
void write_byte(unsigned char dat)
{
unsigned char i;
for (i = 0; i < 8; i++)
{
SDA = dat & 0x80;
SCL = 1;
dat <<= 1;
SCL = 0;
}
}
void write_cmd(unsigned char cmd)
{
start();
write_byte(0x78);
write_byte(0x00);
write_byte(cmd);
stop();
}
void write_dat(unsigned char dat)
{
start();
write_byte(0x78);
write_byte(0x40);
write_byte(dat);
stop();
}
void init(void)
{
write_cmd(0xAE); // 关闭显示
write_cmd(0xD5); // 设置显示时钟分频因子和振荡器频率
write_cmd(0x80);
write_cmd(0xA8); // 设置驱动路数
write_cmd(0x3F); // 设置131像素点
write_cmd(0xD3); // 设置显示偏移
write_cmd(0x00);
write_cmd(0x40); // 设置显示起始行
write_cmd(0x8D); // 开启电荷泵
write_cmd(0x14);
write_cmd(0x20); // 设置内存地址模式
write_cmd(0x00);
write_cmd(0xA1); // 设置段重映射
write_cmd(0xC8); // 设置列重映射
write_cmd(0xDA); // 设置COM硬件引脚配置
write_cmd(0x12);
write_cmd(0x81); // 设置对比度控制
write_cmd(0xCF);
write_cmd(0xD9); // 设置预充电周期
write_cmd(0xF1);
write_cmd(0xDB); // 设置VCOMH电压倍率
write_cmd(0x40);
write_cmd(0xA4); // 关闭全局显示开启显示
write_cmd(0xA6); // 设置正常/反色显示
write_cmd(0xAF); // 打开显示
}
void main(void)
{
init();
write_dat(0xFF);
while (1);
}
```
以上代码使用51单片机控制OLED显示屏,使用4针(SCL、SDA、VCC、GND)连接。其中,start()、stop()、write_byte(unsigned char dat)、write_cmd(unsigned char cmd)、write_dat(unsigned char dat) 函数是I2C通信相关的函数,init()函数用于初始化OLED显示屏。在main()函数中,先进行初始化,然后使用write_dat(unsigned char dat)函数向OLED显示屏写入数据。
阅读全文