单片机程序如下:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit LED1=P2^0;
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- uchar num,num1,num2,shi,ge;
- uint z,i,j;
- uchar code smgdisplay[]={0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71};
- void delayms(uint z);
- void display();
- void main()
- {
- TMOD=0x10;
- TH0=(65535-50000)/256;
- TL0=(65535-50000)%256;
- TH1=(65535-50000)/256;
- TL1=(65535-50000)%256;
- EA=1;
- TR0=1;
- TR1=1;
- ET0=1;
- ET1=1;
- while(1);
- {
- display();
- }
- }
- void t0_time() interrupt 1
- {
- TH0=(65535-50000)/256;
- TL0=(65535-50000)%256;
- num++;
- if(num==4)
- {
- num=0;
- LED1=~LED1;
-
- }
- }
- void t1_time() interrupt 3 //中断服务程序,是给void display()子函数的变量赋值?初始化寄存器。在主函数中等待中断,执行display()子函数。
- {
- TH1=(65535-50000)/256;
- TL1=(65535-50000)%256;
- num1++;
- if(num1==20)
- {
- num1=0;
- num2++;
- if(num2==60)
- {
- num2=0;
- }
- shi=num2/10;
- ge=num2%10;
- }
- }
- void display()
- {
-
- LSA=0;
- LSB=0;
- LSC=0;
- P0=smgdisplay[ge];
- delayms(2);
- P0=0x00;
-
- LSA=1;
- LSB=0;
- LSC=0;
- P0=smgdisplay[shi];
- delayms(2);
- P0=0x00;
-
- }
-
- void delayms(uint z)
- {
- for(i=z;i>0;i--)
- for(j=110;j>0;j--);
- }
复制代码
知其然而不知其所以然,好痛苦
|