2021-10-13
作者:互联网
LW9wZW5pbnN0YWxsLXBiLTEwMTAxMzk4MDgzNzE3LQ==
#include"reg52.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit key_stop = P3^0; //按键定义
sbit key_star = P3^1;
sbit key_hour = P3^2;sbit key_min = P3^3;u8 code ledduan[] = {0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴段码u8 hour=0,min=0,second=0; //时,分,秒void delay(u16 i) //延时函数{u16 z,j;for(z=0;z<i;z++){for(j=0;j<125;j++);}}void Timer0Int() //定时器初始化{TMOD = 0x01;TH0 = (65535-50000)/256;TL0 = (65535-50000)%256;ET0 = 1;EA = 1;TR0 = 1;}void display() //显示定义{ P0 = 0x7f; //0111 1111 //共阴用P0口低电平不用上拉, //低位到高位,这里是最后一位数码管P2 = ledduan[second%10]; //得到秒的个位delay(1);P0 = 0xbf; //1011 1111 P2 = ledduan[second/10]; //得到秒的十位delay(1);P0 = 0xdf; //1101 1111P2 = 0x40; // -delay(1);P0 = 0xef; //1110 1111P2 =ledduan[min%10];delay(1);P0=0xf7; //1111 0111P2 = ledduan[min/10];delay(1);P0 = 0xfb; //1111 1011P2 = 0x40;delay(1);P0 = 0xfd; //1111 1101P2 = ledduan[hour%10];delay(1);P0 = 0xfe; //1111 1110P2 = ledduan[hour/10];delay(1);}void key() //按键定义{if(key_stop==0){delay(1); //消抖延时if(key_stop==0){while(!key_stop);EA = 0; //关中断}}if(key_star==0){delay(1);while(!key_star);if(key_star==0){EA = 1; //开中断}}if(key_hour==0){delay(1);if(key_hour==0){while(!key_hour);hour++;if(hour==24){hour=0;}}}if(key_min==0){delay(1);if(key_min==0){while(!key_min);min++;if(min==60){min=0;} } } }void main() //执行{Timer0Int();while(1){key();display();}}void Timer0()interrupt 1 //中断函数{u8 num;num++;if(num==20){second++;num = 0;if(second==60){second=0;min++;if(min==60){min=0;hour++;if(hour==24){hour=0;min =0;second=0;}}}}}
```</textarea>
标签:10,13,u8,P3,typedef,2021,key,sbit,u16 来源: https://blog.csdn.net/qq_60699380/article/details/120739705