51單片機12864液晶字符顯示
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define LCD_data P0 //定義數(shù)據(jù)口
sbit LCD_RS=P3^5; //數(shù)據(jù)命令選擇端,高電平為寫數(shù)據(jù),低電平寫命令
sbit LCD_RW=P3^6;//讀寫控制端,高電平讀信息,低電平寫信息
sbit LCD_EN=P3^7; // 使能端
sbit LCD_PSB=P3^4; //拉高 ,選擇并行
//單片機特殊功能位聲明
uchar code dis1[]={"姓名:浩泉"};
uchar code dis2[]={"性別:男"};
uchar code dis3[]={"卡號:050817"};
uchar code dis4[]={"www.szu.edu.cn"};
void delay_1ms(uint x) //延時函數(shù)1ms
{
uint i,j;
for(j=0;j
}
void write_cmd(uchar cmd) //寫命令函數(shù),請參考12864液晶手冊
{
LCD_RS=0; //低電平,寫入命令
LCD_RW=0;
P0=cmd; // 賦值
delay_1ms(5);
LCD_EN=1;//使能端拉高送入命令
delay_1ms(5);
LCD_EN=0;
}
void write_date(uchar date) //寫數(shù)據(jù)
{
LCD_RS=1;
LCD_RW=0;
P0=date;
delay_1ms(5);
LCD_EN=1; //使能端拉高送入數(shù)據(jù)
delay_1ms(5);
LCD_EN=0;
}
void lcd_pose(uchar X,uchar Y) //液晶顯示位置函數(shù)
{
uchar S,pose;
switch (X)
{
case 0:
S=0x80; //液晶第一行顯示
break;
case 1:
S=0x90; //第二行顯示
break;
case 2: //第三行顯示
S=0x88;
break;
case 3: //第四行顯示
S=0x98;
break;
}
pose=S+Y;
write_cmd(pose);
}
void lcd_init() //液晶初始化函數(shù)
{
LCD_PSB=1; // 并行選擇,請參考液晶指令表
write_cmd(0x01);
delay_1ms(5);
write_cmd(0x30);
delay_1ms(5);
write_cmd(0x0C);
delay_1ms(5);
write_cmd(0x06);
delay_1ms(5);
}
main()
{
uchar i;
lcd_init();
lcd_pose(0,0); //液晶第一行第一列的第一個字符顯示
i=0;
while(dis1[i]!=/0)//查詢沒有到字符串末尾
{
write_date(dis1[i]); //寫入字符
i++;
}
lcd_pose(1,0);//第2行
i=0;
while(dis2[i]!=/0)
{
write_date(dis2[i]);
i++;
}
lcd_pose(2,0); //第3行
i=0;
while(dis3[i]!=/0)
{
write_date(dis3[i]);
i++;
}
lcd_pose(3,0); //第4行
i=0;
while(dis4[i]!=/0)
{
write_date(dis4[i]);
i++;
}
while(1);
}
評論