用定时器设计时钟并在1602液晶上显示
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#define uint unsigned int
#define uchar unsigned char
sbit lcden=P3^4;
sbit lcdrs=P3^5;
sbit wela=P2^7;
sbit dula=P2^6;
/****分解出的独立按键****/
sbit s1=P3^0; //进入调时间状态
sbit s2=P3^1; //数值增大
sbit s3=P3^2; //数值减少
sbit s4=P3^3; //进入设置闹钟状态
sbit rd=P3^7;
uint a,shi,fen,miao,ri,yue,nian,num,i,j,q,shi1,fen1,miao1,num1;
uchar temp;
uchar code table[]="2012";
/*****显示星期*****/
uchar code table1[][4]={"Mon ","Tues","Wed ","Thur","Fri ","Sat ","Sun "}; /*****延时1ms*****/
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/**********
1602初始化
**********/
void write_com(uchar com) //写命令
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date) //写数据
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init_1602() //初始化
{
// lcdwr=0;
dula=0; //关断选、位选
wela=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80+0x04);
write_data(':');
write_com(0x80+0x07);
write_data(':');
write_com(0x80+0x40);
for(i=0;i<4;i++)
write_data(table[i]);
write_com(0x80+0x44);
write_data('.');
write_com(0x80+0x47);
write_data('.');
}
/************
1602显示时间
************/
void write_sfm() //显示时分秒
{
write_com(0x80+0x02);
write_data(0x30+shi/10);
write_com(0x80+0x03);
write_data(0x30+shi%10);
write_com(0x80+0x05);
write_data(0x30+fen/10);
write_com(0x80+0x06);
write_data(0x30+fen%10);
write_com(0x80+0x08);
write_data(0x30+miao/10);
write_com(0x80+0x09);
write_data(0x30+miao%10);
/*****当显示小时的数小于13,显示AM*****/ if(shi<13)
{
write_com(0x80+0x0c);
write_data('A');
write_com(0x80+0x0d);
write_data('M');
}
/*****显示PM*****/
if(shi>12)
{
write_com(0x80+0x0c);
write_data('P');
write_com(0x80+0x0d);
write_data('M');
}
/*显示月日*/
write_com(0x80+0x45);
write_data(0x30+yue/10);
write_com(0x80+0x46);
write_data(0x30+yue%10);
write_com(0x80+0x48);
write_data(0x30+ri/10);
write_com(0x80+0x49);
write_data(0x30+ri%10);
/*显示星期*/
write_com(0x80+0x4c);
write_data(table1[q][0]);
write_com(0x80+0x4d);
write_data(table1[q][1]);
write_com(0x80+0x4e);
write_data(table1[q][2]);
write_com(0x80+0x4f);
write_data(table1[q][3]);
}
void write_sfm1()
{
write_com(0x80+0x02);
write_data(0x30+shi1/10);
write_com(0x80+0x03);
write_data(0x30+shi1%10);
write_com(0x80+0x05);
write_data(0x30+fen1/10);
write_com(0x80+0x06);
write_data(0x30+fen1%10);
write_com(0x80+0x08);
write_data(0x30+miao1/10);