微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 用定时器实现的1602可调电子钟

用定时器实现的1602可调电子钟

时间:11-29 来源:互联网 点击:
本人最近小搞了下单片机,目前还是菜鸟,以下是单片机程序,基本功能都可以实现,但程序有点小问题(反应不灵敏),还请高手指点下,感激不尽哦!!!
#include #define uchar unsigned char#define uint unsigned intuchar code table[]="   2011-3-11  ";uchar code table1[]="  14:59:55  ";uchar count, hour, minute, second, press;sbit rs=P2^4;sbit rw=P2^5;sbit en=P2^6;sbit key1=P3^0;sbit key2=P3^1;sbit key3=P3^2;void delay (){ ; ;}void write_cmd(uchar cmd)   //1602写指令{rs=0;delay();rw=0;delay();en=0;delay();P0=cmd;en=1;delay();en=0;delay();}void write_byte(uchar date)  //1602写数据{rs=1;delay();rw=0;delay();en=0;delay();P0=date;en=1;delay();en=0;    }    void delay1(uint z){uint x, y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void display(uchar addr, uchar date)  //显示在液晶屏上{uchar ge, shi;ge=date%10;shi=date/10;write_cmd(0x80+0x40+addr);write_byte(shi+0x30);write_byte(ge+0x30);}void keyscan()  //键盘扫描,key1功能键,key2增加,key3减少{if(key1==0){delay1(5);if(key1==0){while(!key1);press++;if(press==1){TR0=0;write_cmd(0x80+0x40+0x09);write_cmd(0x0f);}if(press==2){write_cmd(0x80+0x40+0x06);write_cmd(0x0f);}if(press==3){write_cmd(0x80+0x40+0x03);write_cmd(0x0f);}if(press==4){press=0;write_cmd(0x0c);TR0=1;}}}if(key2==0){delay1(5);if(key2==0){while(!key2);if(press==1){second++;if(second==60){second=0;}display(8,second);}if(press==2){minute++;if(minute==60){minute=0;}display(5,minute);}if(press==3){hour++;if(hour==24){hour=0;}display(2,hour);}}}if(key3==0){delay1(5);if(key3==0){while(!key3);if(press==1){second--;if(second==-1){second=59;}display(8,second);}if(press==2){minute--;if(minute==-1){minute=59;}display(5,minute);}if(press==3){hour--;if(hour==0){hour=23;}display(2,hour);}}}}void init (){en=0;TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;write_cmd(0x38);write_cmd(0x0c);write_cmd(0x06);write_cmd(0x01);hour=14;minute=59;second=55;}void timer0 () interrupt 1 //定时器中断处理函数{TH0=(65536-50000)/256;TL0=(65536-50000)%256;count++;if(count==18){count=0;second++;if(second==60){second=0;minute++;if(minute==60){minute=0;hour++;if(hour==24){hour=0;}display(2,hour);               }display(5,minute);}   display(8,second);}    }void main (){uchar i;init();write_cmd(0x80);for(i=0;i<15;i++){write_byte(table[i]);delay1(5);}write_cmd(0x80+0x40);for(i=0;i<13;i++){write_byte(table1[i]);delay1(5);}  while(1){keyscan();}           }

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top