单片机LCD1602显示年月日时钟带星期和闹钟
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
长春工业大学
测控技术与仪器专业
单片机LCD1602显示年月日时钟带星期和闹钟,可识别闰年
下载文档即可完全复制
#include
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs = P0^7;//液晶引脚定义
sbit led_cs = P0^6;
sbit lcden = P0^5;
sbit boom = P3^6;
sbit key1 = P3^5;
sbit key2 = P3^4;
sbit key3 = P3^3;
sbit keyE = P3^2;
uchar
num,key1num,key2num,count,hour,min,sec,day,mon,year,shi,fen,miao ,week;
char ye1,ye2;
uchar code cg_1602[]={ 0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,
0x0f,0x09,0x0f,0x09,0x0f,0x09,0x11,0x00,
0x0f,0x09,0x09,0x0f,0x09,0x09,0x0f,0x00,
0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,};//"年月日"
/*延时函数*/
void delay(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=114;j>0;j--);
}
/*1602液晶*/
void write_com(uchar com)//写命令{
lcdrs=0;
lcden=0;
P2=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)//写数据{
lcdrs=1;
lcden=0;
P2=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_by_xy(uchar x,uchar y)//坐标定位
{
uchar address;
if(y==0)
address=0x80+x;//y=0,写第一行
else
address=0xc0+x;//y=1,写第二行
write_com(address);
}
/*void write_char(uchar x,uchar y,uchar date)//坐标定位显示{
write_by_xy(x,y);//先定位
write_data(date);//后写数
}*/
void write_string(uchar x,uchar y,uchar *s)//写字符串
{
write_by_xy(x,y);//设定显示位置
while(*s)//write character
{
P2=*s;
write_data(*s);
}
}
void write_sfm(uchar add,uchar date)//写时分秒函数
{
uchar hour,ge;
hour=date/10;//分解一个2位数的十位和个位
ge=date%10;
write_com(0x80+add);//设置显示位置
write_data(0x30+hour);//送去液晶显示十位
write_data(0x30+ge); //送去液晶显示个位
}
void write_cg()//写入固定汉字和符号
{
write_com(0x40); //设定CGRAM地址
for(num=0;num<24;num++) //将自定义字型码写入CGRAM中{
write_data(cg_1602[num]);
}
write_com(0xc4);//年
{
write_data(0);
write_com(0xc7);//月
{
write_data(1);
}
write_com(0xca);//日
{
write_data(2);
}
write_string(2,0,":");
write_string(5,0,":");
write_string(0x0a,0,":");
write_string(0x0d,0,":");
write_string(0x0c,1,"W");
write_string(0x0d,1,"K");
}
void init_1602()//初始化
{
led_cs=0;
hour=23;min=59;sec=55;key1num=0;count=0;day=28;mon=2;ye1=2 0;ye2=13;year=ye1*100+ye2;boom=0;shi=0;fen=0;miao=0;week=7;
write_com(0x38);