用单片机完成数字时钟,关于时刻显现,是用的1602液晶,C51单片机,然后再加一个定时器,每到一秒就动态的更新一下液晶的时刻显现,在更新的时分也要核算相应的分针和时针的改变,还要留意边界问题.为了能够到达一个最好的作用,我还加了一个键盘扫描,用到了3个键,分别为功能键,增1,减1来完成用户自定义时刻.
在完成过程中,用到了单片机的中止体系的相关常识,还有定时器,液晶显现,键盘的动态扫描,都是曾经的常识,没什么难度.
程序如下,能够正常运转.
C代码
#i nclude
#define uint unsigned int
#define uchar unsigned char
sbit lcdrs = P1^0;
sbit lcdrw = P1^1;
sbit lcden = P1^2;
sbit key0 = P2^0;//功能键,挑选时分秒
sbit key1 = P2^1;//加1键
sbit key2 = P2^2;//减1键
sbit key4 = P2^4;
uchar key0_count;//按键0被按的次数(0~3)
char hour,minute,second; //时分秒
uint timer_count; //定时器中止计数
uchar table_date[] = “2009-4-12 Mon”;
uchar table_time[] = “00:00:00”;
/****** 函数声明 ********/
void write_cmd_1602(uchar cmd);
void write_da
void write_add(uchar add,uchar dat);
void init1602();
void delay(uint z);
uchar reverse(uchar c);
void keyscan();
void init();
/****** 主函数 ********/
void main()
{
init();
//init1602();
//while(1);
while(1)
{
keyscan();
delay(5);
}
}
//1602液晶初始化
void init1602()
{
lcdrw = 0; //本程序只读不写,故将其置低
lcden = 0; //封闭1602
delay(5);
write_cmd_1602(0x01); //清屏
write_cmd_1602(0x38); //显现形式
write_cmd_1602(0x0c); //开显现,不显现光标,不闪耀
write_cmd_1602(0x06); //地址指针主动加1
write_cmd_1602(0x80+1);//将光标指向榜首行第二个方位
delay(5);
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x–)
for(y=110;y>0;y–);
}
/** 将发送的数据进行翻转,高位到低位,低位转到高位 **/
uchar reverse(uchar c)
{
uchar chbuff=0;
if (c&0x01) chbuff=chbuff|0x80;
if (c&0x02) chbuff=chbuff|0x40;
if (c&0x04) chbuff=chbuff|0x20;
if (c&0x08) chbuff=chbuff|0x10;
if (c&0x10) chbuff=chbuff|0x08;
if (c&0x20) chbuff=chbuff|0x04;
if (c&0x40) chbuff=chbuff|0x02;
if (c&0x80) chbuff=chbuff|0x01;
return chbuff;
}
//向液晶写指令
void write_cmd_1602(uchar cmd)
{
lcdrs = 0; //低表明写指令,高表明写数据
delay(5);
P0 = reverse(cmd);
delay(5);
lcden = 1; //给一个高脉冲
delay(5);
lcden = 0;
delay(5);
}
//向液晶写数据
void write_da
{
lcdrs = 1; //低表明写指令,高表明写数据
delay(5);
P0 = reverse(dat);
delay(5);
lcden = 1; //给一个高脉冲
delay(5);
lcden = 0;
delay(5);
}
//依据地址向液晶写数据
void write_add(uchar add,uchar dat)
{
uchar shi,ge;
shi = dat/10;
ge = dat%10;
delay(5);
write_cmd_1602(0x80+add);
write_da
delay(5);
write_da
}
//键盘扫描函数
void keyscan()
{
key4 = 0;
if(0==key0) //榜首个键被按下
{
delay(5); //消抖
if(0==key0)
{
key0_count ++;
if(4==key0_count)
key0_count = 0;
while(!key0); //松手检测
if(0==key0_count)
{
TR0 = 1; //从头开端计时
write_cmd_1602(0x0c); //制止显现光标并不让其闪耀
}
if(1==key0_count)
{
TR0 = 0; //暂停计时
write_cmd_1602(0x80+0x40+11);//将地址指针指向秒的方位
write_cmd_1602(0x0f); //显现光标并闪耀
delay(5);
}
if(2==key0_count)
{
TR0 = 0; //暂停计时
write_cmd_1602(0x80+0x40+8);//将地址指针指向分的方位
write_cmd_1602(0x0f); //显现光标并闪耀
delay(5);
}
if(3==key0_count)
{
TR0 = 0; //暂停计时
write_cmd_1602(0x80+0x40+5);//将地址指针指向小时的方位
write_cmd_1602(0x0f); //显现光标并闪耀
delay(5);
}
}
}
if(0==key1) //第二个键被按下
{
delay(5); //消抖
if(0==key1)
{
while(!key1); //松手检测
if(key0_count==1)
{
second ++;
if(60==second)
second = 0;
write_add(0x40+10,second);
}
if(key0_count==2)
{
minute ++;
if(60==minute)
minute = 0;
write_add(0x40+7,minute);
}
if(key0_count==3)
{
hour ++;
if(24==hour)
hour = 0;
write_add(0x40+4,hour);
}
}
}
if(0==key2) //第三个键被按下
{
delay(5); //消抖
if(0==key2)
{
while(!key2); //松手检测
if(key0_count==1)
{
second –;
if(-1==second)
second = 59;
write_add(0x40+10,second);
}
if(key0_count==2)
{
minute –;
if(-1==minute)
minute = 59;
write_add(0x40+7,minute);
}
if(key0_count==3)
{
hour –;
if(-1==hour)
hour = 23;
write_add(0x40+4,hour);
}
}
}
}
//程序初始化函数
void init()
{
uchar i;
hour = 0;
minute = 0;
second = 0;
key0_count = 0;
init1602();
delay(20); //延长时刻,保证液晶处于非忙状况
for(i=0;i<14;i++)
{
write_da
delay(5);
}
write_cmd_1602(0x80+0x40+4); //调整地址指针
for(i=0;i<8;i++)
{
write_da
delay(5);
}
//初始化定时器
TMOD = 0x01; //定时器形式
TH0 = (65536-50000)/256; //装初值
TL0 = (65536-50000)%256;
EA = 1;//开总中止
ET0 = 1;//开定时器0中止
TR0 = 1; //发动定时器,此刻开端计时
}
void timer0() interrupt 1 //定时器0的中止处理函数
{
TH0 = (65536-50000)/256; //装初值
TL0 = (65536-50000)%256;
timer_count ++;
if(20==timer_count) //到了1秒更新时分秒的显现
{
timer_count = 0; //中止次数清0,从头计数
second ++; //秒针加1
if(second==60)
{
second = 0;
minute ++; //分针加1
if(60==minute)
{
minute = 0;
hour ++; //小时加1
if(24==hour)
{
hour = 0;
}
write_add(0x40+4,hour);
}
write_add(0x40+7,minute); //更新分针的显现
}
write_add(0x40+10,second); //更新秒针
}
}