- #include<AT89X52.h>
- #include <intrins.h>
- unsigned char Tab[ ]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f}; //1-9段码
- unsigned char port[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};//数码管
- unsigned char TABLE[]={0,0,0,0,0,0};//显示缓冲区
- unsigned char count ,second,minute,hour,restar,start,flgo; //定义 秒 分 时变量
- unsigned char digital_number = 0;
- sbit k1=P3^4;
- sbit k2=P3^5;
- sbit k3=P3^6;
- sbit k4=P3^7;
- void delay(unsigned int us)
- {
- while(us--);
- }
- void key()
- {
- P0=0xc0;//1100 0000
- restar=0;
- if(k1==0)
- {
- delay(100);
- if(k1==0)
- {
- second++;
- if(second==60)
- {
- second=0;
- }
- k1=1;
- while(second==0)
- TABLE[4]=second/10; //取秒的十位数
- TABLE[5]=second%10;
- }
- }
- if(k2==0)
- {
- delay(100);//避开抖动时期
- if(k2==0)
- {
- minute++;
- if(minute==60)
- {
- minute=0;
- }
- k2=1;
- while(minute==0)
- TABLE[2]=minute/10; //取分的十位
- TABLE[3]=minute%10;
- }
- }
- if(k3==0)
- {
- delay(100);
- if(k3==0)
- {
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- k3=1;
- while(hour==0)
- TABLE[0]=hour/10; //取时的十位
- TABLE[1]=hour%10;
- }
- }
- if(start==0)
- {
- delay(100);
- if(start==1)
- {
- restar=1;
- }
- }
- }
- void main()
- {
- TMOD=0X11; //T1做计数器 T0做定时器
- TL0=0X00; //定时五十微秒赋初值
- TH0=0X4C;
- TR0=1; //启动定时器/计数器
- EA=1; //启动CPU中断
- ET0=1; //T0中断
- TMOD=0X11; //T1做定时器 T0做定时器
- TL1=0XFA; //定时初始值根据显示效果调整
- TH1=0XF2;
- TR1=1; //启动定时器/计数器
- ET1=1; //T1中断
- key();
- while(1)
- {
- if(flgo==1) //当标志位满足时执行
- {
- second++;
- TABLE[4]=second/10; //取秒的十位数
- TABLE[5]=second%10; //取秒的个位数
- if(second==60)
- {
- second=0; //如果秒计满60,将秒计数变量清0
- TABLE[4]=second/10;
- TABLE[5]=second%10;
- minute++; //分钟计数变量加1
- TABLE[2]=minute/10; //取分的十位
- TABLE[3]=minute%10; //取分的个位
- }
- if(minute==60)
- {
- minute=0; //如果分钟计满60,将分钟计数变量清0
- TABLE[2]=minute/10;
- TABLE[3]=minute%10;
- hour++; //小时计数变量加1
- TABLE[0]=hour/10; //取时的十位
- TABLE[1]=hour%10; //取时的个位
- }
- if(hour==24)
- {
- hour=0; //如果小时计满24,将小时计数变量清0
- }
- flgo=0; //标志位清零
- }
- }
- }
- void t0interrupt(void) interrupt 1 //定时器T0的中断处理
- {
- TL0=0X00; //重赋初值
- TH0=0X4C;
- count++;
- if(count==20) //延时一秒
- {
- count=0;
- flgo=1; //标志位
- }
- }
- void t1interrupt(void) interrupt 3 //计数器T1的中断处理
- {
- unsigned char temp;
- TL1=0XFA;
- TH1=0XF2;
- P2_6= 0;
- P2_7= 0;
- temp = TABLE[digital_number]; // 取显示内容
- P0 = Tab[temp];
- P2_6 = 1;
- _nop_(); //空指令,延时一个指令周期
- P2_6 = 0;
- P0 = port[digital_number];
- P2_7= 1;
- _nop_();
- P2_7 = 0;
- digital_number ++;
- if(digital_number == 6) digital_number = 0;
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>