单片机驱动19264a型lcd绘图的程序
时间: 2023-10-06 16:10:26 浏览: 38
下面是一个使用C语言编写的单片机驱动19264a型LCD绘图的程序的示例:
```
#include <reg52.h>
sbit RS = P1^0; // RS引脚
sbit RW = P1^1; // RW引脚
sbit E = P1^2; // E引脚
void delay(unsigned int t) // 延时函数
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<125;j++);
}
void init() // 初始化函数
{
RS = 0; // 设置为指令模式
RW = 0; // 设置为写模式
E = 0; // 设置E引脚初始电平为低电平
delay(15); // 延时15ms
E = 1; // 设置E引脚为高电平
P0 = 0x38; // 设置8位数据总线,2行显示,5×7点阵字符
E = 0; // 设置E引脚为低电平
delay(5); // 延时5ms
E = 1; // 设置E引脚为高电平
P0 = 0x0c; // 打开显示,光标关闭
E = 0; // 设置E引脚为低电平
delay(5); // 延时5ms
E = 1; // 设置E引脚为高电平
P0 = 0x01; // 清屏
E = 0; // 设置E引脚为低电平
delay(5); // 延时5ms
}
void write_com(unsigned char com) // 写入指令
{
RS = 0; // 设置为指令模式
RW = 0; // 设置为写模式
E = 0; // 设置E引脚初始电平为低电平
P0 = com; // 写入指令
E = 1; // 设置E引脚为高电平
delay(5); // 延时5ms
E = 0; // 设置E引脚为低电平
}
void write_data(unsigned char dat) // 写入数据
{
RS = 1; // 设置为数据模式
RW = 0; // 设置为写模式
E = 0; // 设置E引脚初始电平为低电平
P0 = dat; // 写入数据
E = 1; // 设置E引脚为高电平
delay(5); // 延时5ms
E = 0; // 设置E引脚为低电平
}
void set_pos(unsigned char x, unsigned char y) // 设置坐标
{
unsigned char pos;
if(y == 0)
pos = 0x80 + x;
else
pos = 0x80 + x + 0x40;
write_com(pos);
}
void display(unsigned char x, unsigned char y, unsigned char *str) // 显示字符串
{
set_pos(x, y);
while(*str != '\0')
{
write_data(*str);
str++;
}
}
void draw(unsigned char x, unsigned char y, unsigned char *str) // 绘图
{
unsigned char i, j, k;
set_pos(x, y);
for(i=0;i<8;i++)
{
set_pos(x, y+i);
for(j=0;j<16;j++)
{
k = str[i*16+j];
write_data(k);
}
}
}
void main()
{
init(); // 初始化
display(0, 0, "Hello, World!"); // 显示字符串
unsigned char picture[128] = { // 定义绘图数据
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
};
draw(0, 2, picture); // 绘图
while(1);
}
```
这段代码使用了51单片机的P0口作为数据总线,P1口的0、1、2引脚分别作为RS、RW、E引脚。其中,init()函数用于初始化LCD,write_com()函数用于写入指令,write_data()函数用于写入数据,set_pos()函数用于设置显示位置,display()函数用于显示字符串,draw()函数用于绘图。在main()函数中,首先调用init()函数进行初始化,然后使用display()函数显示字符串,最后使用draw()函数绘图。程序最后进入死循环,保持程序运行。