单片机源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit J9A = P2^2;
- sbit J9B = P2^3;
- sbit J9C = P2^4;
- sbit k1 = P1^0;
- sbit k2 = P1^1;
- sbit k3 = P1^2;
- sbit k4 = P1^3;
- sbit k5 = P1^4;
- sbit k7 = P1^5;
- #define GPIO_DIG P0
- uchar code smgduan[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88
- //,0x83,0xc6,0xa1,0x86,0x8e,0xff 共阳
- uchar LedBuff[8];
- uchar sec = 52;
- uchar min = 58;
- uchar hour = 23;
- uchar flags;
- /*
- void delay10ms(uint c)
- {
- uchar a,b;
- for( ; c>0; c--)
- for(b=38; b>0; b--)
- for(a=130; a>0; a--);
- } */
-
- void delay(uint i) //delay(200)可以看到数码管不闪
- {
- while(i--);
- }
- void DigDisplay()
- {
- uint i;
- for(i=0; i<8; i++)
- {
- switch(i)
- {
- case 0:J9A=0; J9B=0; J9C=0; break;
- case 1:J9A=1; J9B=0; J9C=0; break;
- case 2:J9A=0; J9B=1; J9C=0; break;
- case 3:J9A=1; J9B=1; J9C=0; break;
- case 4:J9A=0; J9B=0; J9C=1; break;
- case 5:J9A=1; J9B=0; J9C=1; break;
- case 6:J9A=0; J9B=1; J9C=1; break;
- case 7:J9A=1; J9B=1; J9C=1; break;
- }
- GPIO_DIG = LedBuff[i];
- delay(100);
- GPIO_DIG = 0x00;
- }
- }
- void DigClock(uchar hour,uchar min,uchar sec)
- {
- LedBuff[0] = smgduan[hour/10];
- LedBuff[1] = smgduan[hour%10];
- LedBuff[2] = 0x40;
- LedBuff[3] = smgduan[min/10];
- LedBuff[4] = smgduan[min%10];
- LedBuff[5] = 0x40;
- LedBuff[6] = smgduan[sec/10];
- LedBuff[7] = smgduan[sec%10];
- }
- void Int0Init()
- {
- TMOD |= 0X01;
- TH0 = 0XFC;
- TL0 = 0X18;
- ET0 = 1;
- EA = 1;
- TR0 = 1;
- }
- void main()
- {
- Int0Init();
- while(1)
- {
- DigClock( hour, min, sec);
- DigDisplay();
-
- if(k1 == 0)
- {
- hour=0; min=0; sec=0;
-
- }
- if(k2 == 0)
- {
- TR0 = 0;
- }
- if(k3 == 0)
- {
- TR0 = 1;
- }
- if(k4 == 0)
- {
- delay(1);
- if(k4 == 0)
- {
- TR0 = 0;
- flags++;
- if(flags > 3)
- {
- flags = 0;
- TR0 = 1;
- }
- }
- while(!k4);
- }
- switch(flags)
- {
- case 1:if(k5 == 0)
- {
- delay(1);
- if(k5 == 0)
- {
- hour++;
- if(hour > 23)
- {
- hour = 0;
- }
- while(!k5);
- }
- }
- // DigClock( hour, min, sec);
- // DigDisplay();
-
- if(k7 == 0)
- {
- delay(1);
- if(k7 == 0)
- {
- hour--;
- if(hour < 0)
- {
- hour = 23;
- }
- while(!k7);
- }
- }
- DigClock( hour, min, sec);
- break;
- case 2:if(k5 == 0)
- {
- delay(1);
- if(k5 == 0)
- {
- min++;
- if(min > 59)
- {
- min = 0;
- }
- while(!k5);
- }
- }
- // DigClock( hour, min, sec);
- // DigDisplay();
-
- if(k7 == 0)
- {
- delay(1);
- if(k7 == 0)
- {
- min--;
- if(min < 0)
- {
- min = 59;
- }
- while(!k7);
- }
- }
- DigClock( hour, min, sec);
- break;
- case 3:if(k5 == 0)
- {
- delay(1);
- if(k5 == 0)
- {
- sec++;
- if(sec > 59)
- {
- sec = 0;
- }
- while(!k5);
- }
- }
- // DigClock( hour, min, sec);
- // DigDisplay();
-
- if(k7 == 0)
- {
- delay(1);
- if(k7 == 0)
- {
- sec--;
- if(sec < 0)
- {
- sec = 59;
- }
- while(!k7);
- }
- }
- DigClock( hour, min, sec);
- break;
- }
- }
- }
- void Timer0() interrupt 1
- {
- uint cnt;
- TH0 = 0XFC;
- TL0 = 0X18;
- cnt++;
- if(cnt == 1000)
- {
- cnt = 0;
- sec++;
- if(sec > 59)
- {
- sec = 0;
- min++;
-
- if(min > 59)
- {
- min = 0;
- hour++;
- if(hour > 23)
- {
- hour = 0;
- }
- }
- }
- }
- }
复制代码 |