最近没事,想在lcd1602显示汉字看看效果怎么样,显示的汉字还是很清晰的. 在lcd显示“年月日”.
//***************************************************************************************
//硬件连接:1602VDD接5V,VO接地,BL1接5V,BL2接地,8根数据线接P0口,RS RW E分别接P1.1~3口
//***************************************************************************************
#include <reg52.h>
#include <string.h>
#define Busy 0x80 //用于检测LCM状态字中的Busy标识
#define LCM_Data P0
sbit LCM_RS=P1^0; //寄存器选择
sbit LCM_RW=P1^1; //读/写控制
sbit LCM_E=P1^2; //读/写使能
int i,j;
//自定义字符列表
//=====================================================================================
unsigned char character0[8] = {0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02}, //年
character1[8] = {0x0f,0x09,0x0f,0x09,0x0f,0x09,0x0b,0x11}, //月
character2[8] = {0x0f,0x09,0x09,0x09,0x0f,0x09,0x09,0x0f}, //日
characterN[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //日
//=====================================================================================
//=====================================================================================
//延时程序
//=====================================================================================
void Delay5Ms(void)
{
unsigned long int TempCyc = 5552;
while(TempCyc--);
}
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
while(TempCycA--)
{
TempCycB=7269;
while(TempCycB--);
};
}
//=====================================================================================
//读写子程序
//=====================================================================================
//读数据
unsigned char ReadDataLCM(void)
{
LCM_RS = 1;
LCM_RW = 1;
LCM_E = 1;
LCM_E = 0;
for(i=0;i<100;i++);
LCM_E = 1;
return(LCM_Data);
}
//读状态
unsigned char ReadStatusLCM(void)
{
LCM_Data = 0xFF;
LCM_RS = 0;
LCM_RW = 1;
LCM_E = 1;
LCM_E = 0;
for(i=0;i<100;i++);
LCM_E = 1;
//while (LCM_Data & Busy); //检测忙信号
return(LCM_Data);
}
//写数据
void WriteDataLCM(unsigned char WDLCM)
{
ReadStatusLCM(); //检测忙
LCM_Data = WDLCM;
LCM_RS = 1;
LCM_RW = 0;
LCM_E = 1;
LCM_E = 0; //若晶振速度太高可以在这后加小的延时
for(i=0;i<100;i++);//延时
LCM_E = 1;
}
//写指令
void WriteCommandLCM(unsigned char WCLCM,BuysC) //BuysC为0时忽略忙检测
{
if (BuysC) ReadStatusLCM(); //根据需要检测忙
LCM_Data = WCLCM;
LCM_RS = 0;
LCM_RW = 0;
LCM_E = 1;
LCM_E = 0;
for(i=0;i<100;i++);
LCM_E = 1;
}
//=====================================================================================
//初始化子程序
//=====================================================================================
void LCMInit(void) //LCM初始化
{
LCM_Data = 0;
WriteCommandLCM(0x38,0); // 三次显示模式设置,不检测忙信号
Delay5Ms();
WriteCommandLCM(0x38,0);
Delay5Ms();
WriteCommandLCM(0x38,0);
Delay5Ms();
WriteCommandLCM(0x38,1); // 显示模式设置,开始要求每次检测忙信号
Delay5Ms();
WriteCommandLCM(0x08,1); // 关闭显示
Delay5Ms();
WriteCommandLCM(0x01,1); // 清屏
Delay5Ms();
WriteCommandLCM(0x06,1); // 显示光标移动设置
Delay5Ms();
WriteCommandLCM(0x0c,1); // 显示开及光标设置
Delay5Ms();
}
//=====================================================================================
//按指定位置显示一个字符
//=====================================================================================
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
if (Y) X |= 0x40; //当要显示第二行时地址码+0x40;
X |= 0x80; //算出指令码
WriteCommandLCM(X, 0); //这里不检测忙信号,发送地址码
WriteDataLCM(DData);
}
//=====================================================================================
//按指定位置显示一串字符
//void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
//说明: x(0-15):x参数 y(0-1):y参数 DData(字符串):要显示的内容(英文、数字、符号)
//=====================================================================================
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
unsigned char ListLength,j;
ListLength = strlen(DData);
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
if (X <= 0xF) //X坐标应小于0xF
{
for(j=0;j<ListLength;j++)
{
DisplayOneChar(X, Y, DData[j]); //显示单个字符
X++;
}
}
}
//=====================================================================================
//显示自定义字符
//void mychar(char xx,char yy,unsigned char *character,unsigned char saveto)
//说明:xx(0-15):为x参数.yy(0-1):y参数.character:要显示的字符的列表地址,在程序前面有定义
//saveto(1-7)为字符保存的RAM,每屏最多显示7个自定义字符
//(0x00-0x0h是自定义字符)
//=====================================================================================
void mychar(char xx,char yy,unsigned char *character,unsigned char saveto)
{
unsigned char add = (saveto<<3) | 0x40;
unsigned char t; //临时变量,每一行的值
t=*(character+0);
WriteCommandLCM(add, 0); //第1行
WriteDataLCM(t);
t=*(character+1);
WriteCommandLCM(add+1, 0); //第2行
WriteDataLCM(t);
t=*(character+2);
WriteCommandLCM(add+2, 0); //第3行
WriteDataLCM(t);
t=*(character+3);
WriteCommandLCM(add+3, 0); //第4行
WriteDataLCM(t);
t=*(character+4);
WriteCommandLCM(add+4, 0); //第5行
WriteDataLCM(t);
t=*(character+5);
WriteCommandLCM(add+5, 0); //第6行
WriteDataLCM(t);
t=*(character+6);
WriteCommandLCM(add+6, 0); //第7行
WriteDataLCM(t);
t=*(character+7);
WriteCommandLCM(add+7, 0); //第8行
WriteDataLCM(t);
DisplayOneChar(xx,yy,saveto); //显示字符
}
//=====================================================================================
//主函数
//=====================================================================================
main()
{
Delay400Ms();
LCMInit();
Delay400Ms(); //1602初始化
while(1)
{
DisplayListChar(0,0,"This is ListChar");
DisplayListChar(0,1,"!");
for(j=0;j<30;j++)for(i=0;i<30000;i++);
WriteCommandLCM(0x01,1); //清屏
Delay5Ms();
DisplayListChar(0,0,"This is OneChar:");
DisplayOneChar(0,1,0x4f);
DisplayOneChar(1,1,0x6e);
DisplayOneChar(2,1,0x65);
DisplayOneChar(3,1,0x21);
for(j=0;j<30;j++)for(i=0;i<30000;i++);
WriteCommandLCM(0x01,1); //清屏
Delay5Ms();
DisplayListChar(0,0,"This is MyChar:");
mychar(0,1, character0,1);
mychar(1,1, character1,2);
mychar(2,1, character2,3);
for(j=0;j<30;j++)for(i=0;i<30000;i++);
WriteCommandLCM(0x01,1); //清屏
Delay5Ms();
}
}
看起来,也不是那么难看.^_^
用户377235 2014-9-17 19:54
用户1601331 2009-7-19 20:05
用户1414612 2008-11-20 15:04
用户38817 2008-10-31 20:03
用户1199424 2008-9-23 15:36
用户1480039 2008-9-23 10:42