电路原理图如下:
单片机源程序如下:
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit key1 = P3^7;
- sbit led = P3^0;
- sbit key2 = P1^7;
- sbit key3 = P1^6;
- sbit key4 = P1^5;
- sbit key5 = P1^4;
- sbit key6 = P1^3;
- sbit key7 = P1^2;
- sbit key8 = P1^1;
- sbit key9 = P1^0;
- code uchar abc[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uchar cba[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
- uchar cp,k,b = 0,j,c;
- uchar key1_zt,key2_zt,key3_zt,key4_zt,key5_zt,key6_zt,key7_zt,key8_zt,key9_zt,key_ms;
- char sec = 55,min = 59,hou = 23,hou_nz,min_nz;
- void delay(uint x)
- {
- while(x--);//????while(x)x--;
- }
- void key(void)
- {
- if(key1 == 1)
- delay(200);
- if(key1 == 1)
- key1_zt = 1;
- if(key1_zt == 1 && key1 == 0)
- {
- key1_zt = 0;
- key_ms++;
- if(key_ms == 9)key_ms = 0;
- }
- if(key2 == 1)
- delay(200);
- if(key2 == 1)
- key2_zt = 1;
- if(key2_zt == 1 && key2 == 0 && key_ms == 1)
- {
- key2_zt = 0;
- hou++;
- }
- if(key3 == 1)
- delay(200);
- if(key3 == 1)
- key3_zt = 1;
- if(key3_zt == 1 && key3 == 0 && key_ms == 2)
- {
- key3_zt = 0;
- hou--;
- if(hou < 0)hou = 23;
- }
- if(key4 == 1)
- delay(200);
- if(key4 == 1)
- key4_zt = 1;
- if(key4_zt == 1 && key4 == 0 && key_ms == 3)
- {
- key4_zt = 0;
- min++;
- }
- if(key5 == 1)
- delay(200);
- if(key5 == 1)
- key5_zt = 1;
- if(key5_zt == 1 && key5 == 0 && key_ms == 4)
- {
- key5_zt = 0;
- min--;
- if(min < 0)min = 59;
- }
- if(key6 == 1)
- delay(200);
- if(key6 == 1)
- key6_zt = 1;
- if(key6_zt == 1 && key6 == 0 && key_ms == 5)
- {
- key6_zt = 0;
- hou_nz++;
- if(hou_nz == 24)hou_nz = 0;
- }
- if(key7 == 1)
- delay(200);
- if(key7 == 1)
- key7_zt = 1;
- if(key7_zt == 1 && key7 == 0 && key_ms == 6)
- {
- key7_zt = 0;
- hou_nz--;
- if(hou_nz < 0)hou_nz = 23;
- }
- if(key8 == 1)
- delay(200);
- if(key8 == 1)
- key8_zt = 1;
- if(key8_zt == 1 && key8 == 0 && key_ms == 7)
- {
- key8_zt = 0;
- min_nz++;
- if(min_nz == 60)min_nz = 0;
- }
- if(key9 == 1)
- delay(200);
- if(key9 == 1)
- key9_zt = 1;
- if(key9_zt == 1 && key9 == 0 && key_ms == 8)
- {
- key9_zt = 0;
- min_nz--;
- if(min_nz < 0)min_nz = 59;
- }
- }
- void timer0_isr(void)interrupt 1//?ж??????
- {
- TH0 = (65536 - 5000) / 256;
- TL0 = (65536 - 5000) % 256;
- cp++;
- if(cp == 100)
- {
- cp = 0;
- k++;
- b = ~b;
- }
- if(k == 2)
- {
- k = 0;
- sec++;
- }
- if(sec == 60)
- {
- sec = 0;
- min++;
- }
- if(min == 60)
- {
- min = 0;
- hou++;
- }
- if(hou == 24)hou = 0;
- P0 = 0xff;
- if(key_ms == 0)
- {
- switch(j)
- {
- case 0:P0 = abc[sec%10];P2 = cba[j];break;
- case 1:P0 = abc[sec/10];P2 = cba[j];break;
- case 2:P0 = abc[min%10]&(0x7f | b);P2 = cba[j];break;
- case 3:P0 = abc[min/10];P2 = cba[j];break;
- case 4:P0 = abc[hou%10]&(0x7f | b);P2 = cba[j];break;
- case 5:P0 = abc[hou/10];P2 = cba[j];break;
- }
- j++;
- if(j == 6)j = 0;
- }
- if(key_ms == 1)
- {
- switch(j)
- {
- case 0:P0 = abc[sec%10];P2 = cba[j];break;
- case 1:P0 = abc[sec/10];P2 = cba[j];break;
- case 2:P0 = abc[min%10] & (0x7f | b);P2 = cba[j];break;
- case 3:P0 = abc[min/10];P2 = cba[j];break;
- case 4:P0 = abc[hou%10] & 0x7f | b;P2 = cba[j];break;
- case 5:P0 = abc[hou/10] | b;P2 = cba[j];break;
- }
- j++;
- if(j == 6)j = 0;
- }
- if(key_ms == 2)
- {
- switch(j)
- {
- case 0:P0 = abc[sec%10];P2 = cba[j];break;
- case 1:P0 = abc[sec/10];P2 = cba[j];break;
- case 2:P0 = abc[min%10] & (0x7f | b);P2 = cba[j];break;
- case 3:P0 = abc[min/10];P2 = cba[j];break;
- case 4:P0 = abc[hou%10] & 0x7f | b;P2 = cba[j];break;
- case 5:P0 = abc[hou/10] | b;P2 = cba[j];break;
- }
- j++;
- if(j == 6)j = 0;
- }
- if(key_ms == 3)
- {
- switch(j)
- {
- case 0:P0 = abc[sec%10];P2 = cba[j];break;
- case 1:P0 = abc[sec/10];P2 = cba[j];break;
- case 2:P0 = abc[min%10] & 0x7f | b;P2 = cba[j];break;
- case 3:P0 = abc[min/10] | b;P2 = cba[j];break;
- case 4:P0 = abc[hou%10] & (0x7f | b);P2 = cba[j];break;
- case 5:P0 = abc[hou/10];P2 = cba[j];break;
- }
- j++;
- if(j == 6)j = 0;
- }
- if(key_ms == 4)
- {
- switch(j)
- {
- case 0:P0 = abc[sec%10];P2 = cba[j];break;
- case 1:P0 = abc[sec/10];P2 = cba[j];break;
- case 2:P0 = abc[min%10] & 0x7f | b;P2 = cba[j];break;
- case 3:P0 = abc[min/10] | b;P2 = cba[j];break;
- case 4:P0 = abc[hou%10] & (0x7f | b);P2 = cba[j];break;
- case 5:P0 = abc[hou/10];P2 = cba[j];break;
- }
- j++;
- if(j == 6)j = 0;
- }
- if(key_ms == 5)
- {
- switch(j)
- {
- case 0:P0 = abc[0];P2 = cba[j];break;
- case 1:P0 = abc[0];P2 = cba[j];break;
- case 2:P0 = abc[min_nz%10] & (0x7f | b);P2 = cba[j];break;
- case 3:P0 = abc[min_nz/10];P2 = cba[j];break;
- case 4:P0 = abc[hou_nz%10] & 0x7f | b;P2 = cba[j];break;
- case 5:P0 = abc[hou_nz/10] | b;P2 = cba[j];break;
- }
- j++;
- if(j == 6)j = 0;
- }
- if(key_ms == 6)
- {
- switch(j)
- {
- case 0:P0 = abc[0];P2 = cba[j];break;
- case 1:P0 = abc[0];P2 = cba[j];break;
- case 2:P0 = abc[min_nz%10] & (0x7f | b);P2 = cba[j];break;
- case 3:P0 = abc[min_nz/10];P2 = cba[j];break;
- case 4:P0 = abc[hou_nz%10] & 0x7f | b;P2 = cba[j];break;
- case 5:P0 = abc[hou_nz/10] | b;P2 = cba[j];break;
- }
- j++;
- if(j == 6)j = 0;
- }
- if(key_ms == 7)
- {
- switch(j)
- {
- case 0:P0 = abc[0];P2 = cba[j];break;
- case 1:P0 = abc[0];P2 = cba[j];break;
- case 2:P0 = abc[min_nz%10] & 0x7f | b;P2 = cba[j];break;
- case 3:P0 = abc[min_nz/10] | b;P2 = cba[j];break;
- case 4:P0 = abc[hou_nz%10] & (0x7f | b);P2 = cba[j];break;
- case 5:P0 = abc[hou_nz/10];P2 = cba[j];break;
- }
- j++;
- if(j == 6)j = 0;
- }
- if(key_ms == 8)
- {
- switch(j)
- {
- case 0:P0 = abc[0];P2 = cba[j];break;
- case 1:P0 = abc[0];P2 = cba[j];break;
- case 2:P0 = abc[min_nz%10] & 0x7f | b;P2 = cba[j];break;
- case 3:P0 = abc[min_nz/10] | b;P2 = cba[j];break;
- case 4:P0 = abc[hou_nz%10] & (0x7f | b);P2 = cba[j];break;
- case 5:P0 = abc[hou_nz/10];P2 = cba[j];break;
- }
- j++;
- if(j == 6)j = 0;
- }
- }
- void timer0_init(void)//??????????????
- {
- TMOD = 0x01;
- TH0 = (65536 - 5000) / 256;
- TL0 = (65536 - 5000) % 256;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void main(void)
- {
- timer0_init();
- while(1);
- {
- key();
- }
- }
复制代码
所有资料51hei提供下载:
电子表.zip
(59.23 KB, 下载次数: 21)
|