|
楼主 |
发表于 2023-1-14 16:58:41
|
显示全部楼层
电子钟- #include <STC15F2K60S2.H>
- sbit S4=P3^3;
- sbit S5=P3^2;
- unsigned char t_m=0;
- unsigned char t_s=0;
- unsigned char t_005s=0;
- unsigned char code SMG_duanma[18]=
- {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
- 0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,
- 0xbf,0x7f};
- void selectHC573(unsigned char channel)
- {
- switch(channel)
- {
- case 4:
- P2=(P2&0x1f)|0x80;
- break;
- case 5:
- P2=(P2&0x1f)|0xa0;
- break;
- case 6:
- P2=(P2&0x1f)|0xc0;
- break;
- case 7:
- P2=(P2&0x1f)|0xe0;
- break;
- }
- }
- void delaySMG(unsigned char t)
- {
- while(t--);
- }
- void displaySMG_Bit(unsigned char value,unsigned char pos)
- {
- selectHC573(6);
- P0=0x01<<pos;
- selectHC573(7);
- P0=value;
- }
- void displayTime()
- {
- displaySMG_Bit(SMG_duanma[t_005s%10],7);
- delaySMG(500);
- displaySMG_Bit(SMG_duanma[t_005s/10],6);
- delaySMG(500);
- displaySMG_Bit(SMG_duanma[16],5);
- delaySMG(500);
-
- displaySMG_Bit(SMG_duanma[t_s%10],4);
- delaySMG(500);
- displaySMG_Bit(SMG_duanma[t_s/10],3);
- delaySMG(500);
- displaySMG_Bit(SMG_duanma[16],2);
- delaySMG(500);
-
- displaySMG_Bit(SMG_duanma[t_m%10],1);
- delaySMG(500);
- displaySMG_Bit(SMG_duanma[t_m/10],0);
- delaySMG(500);
- }
- void initTimer0()
- {
- TMOD=0x01;
- TH0=(65535-50000)/256;
- TL0=(65535-50000)%256;
-
- ET0=1;
- EA=1;
- TR0=1;
- }
- void serviceTimer0() interrupt 1
- {
- TH0=(65535-50000)/256;
- TL0=(65535-50000)%256;
-
- t_005s++;
- if(t_005s==20)
- {
- t_s++;
- t_005s=0;
- if(t_s==60)
- {
- t_m++;
- t_s=0;
- }
- if(t_m==99)
- {
- t_m=0;
- }
- }
- }
- void delayK(unsigned char t)
- {
- while(t--);
- }
- void scanKeys()
- {
- if(S4==0)//miaobiaoqidongyuzanting
- {
- delayK(100);
- if(S4==0)
- {
- TR0=~TR0;
- while(S4==0)
- {
- displayTime();
- }
- }
- }
- if(S5==0)//qingling
- {
- delayK(100);
- if(S5==0)
- {
- t_m=0;
- t_s=0;
- t_005s=0;
- while(S5==0)
- {
- displayTime();
- }
- }
- }
- }
- void main()
- {
- initTimer0();
- while(1)
- {
- displayTime();
- scanKeys();
- }
- }
复制代码 |
|