- #include "reg52.h"
- #include "intrins.h"
- #define uchar unsigned char
- #define uint unsigned int
- uchar code ledcode[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; // 0-9,-
- #define codport P0
- #define sitport P2
- sbit LSA=P2^2;
- sbit LSB=P2^1;
- sbit LSC=P2^0;
- uchar data disbuf[]={10, 10, 10, 10, 10, 10, 10, 10,};
- uchar data hou=23,min=59,s=50,num;
- uchar naoling[]={0,0,0};
- bit flag=0;
- sbit beep=P3^1;
- //************//延时************//
- void delay(unsigned int i)
- {
- while(i--);
- }
- //************//数码管动态扫描************//
- void display()
- {
- unsigned int i;
- for(i=0;i<8;i++)
- {
- switch(i)
- {
- case(0):
- LSA=0;LSB=0;LSC=0; break;
- case(1):
- LSA=1;LSB=0;LSC=0; break;
- case(2):
- LSA=0;LSB=1;LSC=0; break;
- case(3):
- LSA=1;LSB=1;LSC=0; break;
- case(4):
- LSA=0;LSB=0;LSC=1; break;
- case(5):
- LSA=1;LSB=0;LSC=1; break;
- case(6):
- LSA=0;LSB=1;LSC=1; break;
- case(7):
- LSA=1;LSB=1;LSC=1; break;
- }
- codport = ledcode[disbuf[i]];
- delay(100);
- P0=0x00;
- }
- }
- //************//矩阵按键************//
- unsigned char Key()
- {
- unsigned char Row, Col, k;
- P1 = 0x0f;
- k=P1&0x0f;
- if(k == 0x0f)
- return -1;
- delay(12);
- k = P1&0x0f;
- if(k == 0x0f)
- return -1;
- for(Row = 0; Row < 4; Row++)
- {
- P1 = 0x7f>>Row;
- k = P1&0x0f;
- if(k!=0x0f)
- {
- Col = 0;
- while(k&(1<<Col))
- Col++;
- break;
- }
- }
- while((P1&0x0f)!=0x0f);
- k = Row + Col * 4;
- return k;
- }
- //************//定时器0中断函数************//
- void time0() interrupt 1
- {
- T0 = 55536%256;
- TH0 = 55536/256;
- num = num+1;
- if(num > 99)
- {
- num = 0;
- s = s + 1;
- if(s > 59)
- {
- s = 0;
- min = min + 1;
- }
- if(min > 59)
- {
- min = 0;
- hou = hou + 1;
- }
- if(hou > 23)
- {
- hou = 0;
- }
- if((min==0)&&(s==0))
- {
- beep=~beep;
- delay(300);
-
- }
- if(flag == 0)
- {
- disbuf[0] = hou/10;
- disbuf[1] = hou%10;
- disbuf[2] = 10;
- disbuf[3] = min/10;
- disbuf[4] = min%10;
- disbuf[5] = 10;
- disbuf[6] = s/10;
- disbuf[7] = s%10;
- }
- else
- {
- disbuf[0] = naoling[0]/10;
- disbuf[1] = naoling[0]%10;
- disbuf[2] = 10;
- disbuf[3] = naoling[1]/10;
- disbuf[4] = naoling[1]%10;
- disbuf[5] = 10;
- disbuf[6] = naoling[2]/10;
- disbuf[7] = naoling[2]%10;
- }
- }
- }
- //************//选择矩阵按键************//
- void keyProcess(uchar k)
- {
- if(k == 0)
- {
- flag = ~flag;
- }
- if(flag == 0)
- {
- if(k == 7){
- if(hou<=24){hou+=10;}
- else{hou=hou%10;}
- }
- if(k == 11){
- if(hou<=24){hou+=1;}
- else{hou=hou/10;}
- }
- if(k == 6){
- if(min<=60){min+=10;}
- else{min=min%10;}
- }
- if(k == 10){
- if(min<=60){min += 1;}
- else{min=min/10;}
- }
- if(k == 5){
- if(s<=60){s += 10;}
- else{s=s%10;}
- }
- if(k == 9){
- if(s<=60){s += 1;}
- else{s=s/10;}
- }
- }
- }
- //************//主函数************//
- void main()
- {
- uchar k;
- TMOD = 0x01;
- TL0 = 55536%256;
- TH0 = 55536/256;
- ET0 =1;
- EA = 1;
- TR0 = 1;
- while(1)
- {
- display();
- k = Key();
- keyProcess(k);
- }
- }
复制代码 |