用keil写一个使OLED 屏幕亮的代码
时间: 2024-05-11 10:17:00 浏览: 85
好的,以下是用Keil写使OLED屏幕亮的代码:
首先,需要在代码中定义OLED的引脚:
```c
#define OLED_SDA P1_1 // 数据引脚
#define OLED_SCL P1_0 // 时钟引脚
```
然后,需要初始化OLED屏幕,即向OLED发送一些命令,使其进入正常工作状态:
```c
void OLED_Init() {
Write_Command(0xAE);//display off
Write_Command(0x20);//Set Memory Addressing Mode
Write_Command(0x10);//00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
Write_Command(0xB0);//Set Page Start Address for Page Addressing Mode,0-7
Write_Command(0xC8);//Set COM Output Scan Direction
Write_Command(0x00);//---set low column address
Write_Command(0x10);//---set high column address
Write_Command(0x40);//--set start line address
Write_Command(0x81);//--set contrast control register
Write_Command(0xFF);
Write_Command(0xA1);//--set segment re-map 0 to 127
Write_Command(0xA6);//--set normal display
Write_Command(0xA8);//--set multiplex ratio(1 to 64)
Write_Command(0x3F);//--1/64 duty
Write_Command(0xA4);//0xa4,Output follows RAM content;0xa5,Output ignores RAM content
Write_Command(0xD3);//-set display offset
Write_Command(0x00);//-not offset
Write_Command(0xD5);//--set display clock divide ratio/oscillator frequency
Write_Command(0xF0);//--set divide ratio
Write_Command(0xD9);//--set pre-charge period
Write_Command(0x22); //
Write_Command(0xDA);//--set com pins hardware configuration
Write_Command(0x12);
Write_Command(0xDB);//--set vcomh
Write_Command(0x20);//0x20,0.77xVcc
Write_Command(0x8D);//--set DC-DC enable
Write_Command(0x14):// bit2,1 enabled both supplies ,0 disabled both supplies
Write_Command(0xAF);//--turn on oled panel
}
```
接下来,需要写一个函数来向OLED屏幕发送数据或命令:
```c
void Write_Command(unsigned char dat) {
unsigned char i;
OLED_SCL = 0;
for(i=0;i<8;i++) {
OLED_SDA = 0;
OLED_SCL = 1;
OLED_SCL = 0;
OLED_SDA = dat&0x80;
dat <<= 1;
}
OLED_SCL=0;
OLED_SCL=1;
OLED_SCL=0;
}
```
最后,在主函数中调用OLED_Init函数来初始化OLED屏幕,并调用Write_Command函数发送让OLED屏幕亮起的命令:
```c
int main(void) {
OLED_Init();
Write_Command(0xAF); // turn on OLED panel
while(1);
}
```
以上代码是基于8051单片机的Keil开发环境编写的,需要按照具体硬件配置进行修改才能在实际应用中使用。
阅读全文