用AT89C52写keil关于点阵显示设计的程序
时间: 2023-09-10 19:08:59 浏览: 142
您好,以下是一个基于AT89C52的点阵显示程序设计示例:
```c
#include <reg52.h> //头文件
#define LCD_DATA P0 //定义 LCD 数据口
sbit CS = P1^0; //定义片选信号控制口
sbit WR = P1^1; //定义写信号控制口
sbit RD = P1^2; //定义读信号控制口
sbit RESET = P1^3; //定义复位信号控制口
sbit D_C = P1^4; //定义数据/命令选择信号控制口
unsigned char const code gImage[] = { // 图形数据数组
0x00,0x7E,0x42,0x42,0x42,0x7E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x7E,0x4A,0x4A,0x4A,0x7E,0x00,0x00,0x08,0x08,0x00,0x00,0x08,0x08,0x00,0x00,
0x00,0x7E,0x02,0x02,0x02,0x7E,0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,
0x44,0x7E,0x44,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x3C,0x42,0x81,0x81,0x81,0x42,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x82,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x00,0x00,0x00,0x00,
0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x00,0x00,0x00,0x00,
0x00,0x7E,0x10,0x28,0x44,0x82,0x82,0x00,0x00,0x08,0x10,0x10,0x10,0x10,0x08,0x00,
};
void delay(unsigned int i){ //延时函数
unsigned int j;
for(j = 0; j < i; j++);
}
void Write_Cmd(unsigned char cmd){ //写命令函数
D_C = 0;
LCD_DATA = cmd;
WR = 0;
WR = 1;
}
void Write_Data(unsigned char dat){ //写数据函数
D_C = 1;
LCD_DATA = dat;
WR = 0;
WR = 1;
}
void Init(){ //LCD初始化函数
CS = 1;
RESET = 0;
delay(200);
RESET = 1;
delay(200);
Write_Cmd(0x36); //设置扫描模式
Write_Data(0x00);
Write_Cmd(0x3A); //设置数据位数
Write_Data(0x05);
Write_Cmd(0xB2); //设置行驱动时序
Write_Data(0x0C);
Write_Data(0x0C);
Write_Data(0x00);
Write_Data(0x33);
Write_Data(0x33);
Write_Cmd(0xB7); //设置接口格式
Write_Data(0x35);
Write_Cmd(0xBB); //设置底层亮度
Write_Data(0x1F);
Write_Cmd(0xC0); //设置上下视角
Write_Data(0x2C);
Write_Cmd(0xC2); //设置左右视角
Write_Data(0x01);
Write_Cmd(0xC3); //设置复位时序
Write_Data(0x0B);
Write_Cmd(0xC4); //设置时钟分频因子
Write_Data(0x10);
Write_Cmd(0xC6); //设置像素数据格式
Write_Data(0x0F);
Write_Cmd(0xD0); //设置显示模式
Write_Data(0xA4);
Write_Data(0xA1);
Write_Cmd(0xE0); //设置Gamma校正
Write_Data(0xD0);
Write_Data(0x04);
Write_Data(0x0D);
Write_Data(0x11);
Write_Data(0x13);
Write_Data(0x2B);
Write_Data(0x3F);
Write_Data(0x54);
Write_Data(0x4C);
Write_Data(0x18);
Write_Data(0x0D);
Write_Data(0x0B);
Write_Data(0x1F);
Write_Data(0x23);
Write_Cmd(0xE1); //设置Gamma校正
Write_Data(0xD0);
Write_Data(0x04);
Write_Data(0x0C);
Write_Data(0x11);
Write_Data(0x13);
Write_Data(0x2C);
Write_Data(0x3F);
Write_Data(0x44);
Write_Data(0x51);
Write_Data(0x2F);
Write_Data(0x1F);
Write_Data(0x1F);
Write_Data(0x20);
Write_Data(0x23);
Write_Cmd(0x2A); //设置列地址
Write_Data(0x00);
Write_Data(0x00);
Write_Data(0x01);
Write_Data(0x3F);
Write_Cmd(0x2B); //设置行地址
Write_Data(0x00);
Write_Data(0x00);
Write_Data(0x00);
Write_Data(0x7F);
Write_Cmd(0x29); //打开显示
}
void Display(unsigned char const *p, unsigned char x, unsigned char y) { //显示函数
unsigned char i, j;
Write_Cmd(0x2A); //设置列地址
Write_Data(x >> 8);
Write_Data(x & 0xff);
Write_Data((x + 15) >> 8);
Write_Data((x + 15) & 0xff);
Write_Cmd(0x2B); //设置行地址
Write_Data(y >> 8);
Write_Data(y & 0xff);
Write_Data((y + 7) >> 8);
Write_Data((y + 7) & 0xff);
Write_Cmd(0x2C); //写数据
for(i = 0; i < 8; i++) {
for(j = 0; j < 16; j++) {
Write_Data(*p++);
}
}
}
void main() { //主函数
Init(); //LCD初始化
while(1) { //显示图形
Display(gImage, 0, 0);
delay(1000);
}
}
```
在这个示例中,我们使用了AT89C52单片机控制一个点阵显示屏,实现了一个简单的图形显示效果。在程序中,我们首先定义了各个引脚的控制口,并声明了一些需要用到的函数,如延时函数、写命令函数、写数据函数、LCD初始化函数、显示函数等。
在主函数中,我们调用了LCD初始化函数,然后通过循环调用显示函数,将数组中的图形数据显示在屏幕上。
当然,这只是一个简单的示例,如果您需要更加复杂的功能,还需要根据具体的需求进行修改和完善。
阅读全文