LCD1602显示字母a-z
1、设计目的
LCD1602显示字母a-z
2、仿真电路
3、程序设计(C语言)
#include<reg51.h>
sbit lcdrs=P3^0; //LCD显示的数据/命令选择端
sbit lcdrw=P3^1; //LCD显示的读写操作选择端
sbit lcden=P3^2; //LCD显示的使能信号
/****************************************
函数功能:ms级延时
*****************************************/
void delayms(unsigned int xms) //定义延时函数
{
unsigned int i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
/****************************************
函数功能:LCD写入数据
*****************************************/
void write_data(unsigned char date)
{
lcdrw=0; //RW=0,写操作
lcdrs=1; //RS=1 写数据
P0=date;
delayms(5);
lcden=1; //E=1
delayms(5);
lcden=0; //E=0 当E由1变0时LCD写入数据
}
/****************************************
函数功能:LCD写入指令
*****************************************/
void write_com(unsigned char com)
{
lcdrw=0; //RW=0,写操作
lcdrs=0; //RS=0 写命令
P0=com;
delayms(5);
lcden=1; //E=1
delayms(5);
lcden=0; //E=0 当E由1变0时LCD执行命令
}
/****************************************
函数功能:LCD初始化设置
*****************************************/
void lcd_init()
{
lcden=0; //E=0
write_com(0x38); //00111000两行显示,5*7点阵,8位数据
write_com(0x0c); //00001100开整体显示,光标关,无闪烁
write_com(0x06); //00000110写入1个字符后,地址指针加一
write_com(0x01); //00000001清屏
}
/****************************************
函数功能:LCD显示字母a-z
*****************************************/
void lcd_small_letter(unsigned char letter)
{
int small_letter[26]={0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,
0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,
0x75,0x76,0x77,0x78,0x79,0x7a};
lcden=0;
switch(letter-97)
{
case 0:write_data(small_letter[0]);
break;
case 1:write_data(small_letter[1]);
break;
case 2:write_data(small_letter[2]);
break;
case 3:write_data(small_letter[3]);
break;
case 4:write_data(small_letter[4]);
break;
case 5:write_data(small_letter[5]);
break;
case 6:write_data(small_letter[6]);
break;
case 7:write_data(small_letter[7]);
break;
case 8:write_data(small_letter[8]);
break;
case 9:write_data(small_letter[9]);
break;
case 10:write_data(small_letter[10]);
break;
case 11:write_data(small_letter[11]);
break;
case 12:write_data(small_letter[12]);
break;
case 13:write_data(small_letter[13]);
break;
case 14:write_data(small_letter[14]);
break;
case 15:write_data(small_letter[15]);
break;
case 16:write_data(small_letter[16]);
break;
case 17:write_data(small_letter[17]);
break;
case 18:write_data(small_letter[18]);
break;
case 19:write_data(small_letter[19]);
break;
case 20:write_data(small_letter[20]);
break;
case 21:write_data(small_letter[21]);
break;
case 22:write_data(small_letter[22]);
break;
case 23:write_data(small_letter[23]);
break;
case 24:write_data(small_letter[24]);
break;
case 25:write_data(small_letter[25]);
break;
}
}
/****************************************
函数功能:主函数
*****************************************/
void main()
{
int letter,n=0;
lcd_init();
for(letter='a';letter<='z';letter++)
{
if(n==26)
n=0;
if(n<16)
{
write_com(0x80+n); //第一行显示位置
lcd_small_letter(letter); //显示小写字母
delayms(100);
}
else
{
write_com(0x80+0x40+n-16); //第二行显示位置
lcd_small_letter(letter); //显示小写字母
delayms(100);
}
n++;
}
while(1); //持续运行
}
4、补充内容