- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit Seg_W1 = P2^0;
- sbit Seg_W2 = P2^1;
- sbit Seg_W3 = P2^2;
- sbit Seg_W4 = P2^3;
- uint code biao[]={0xC0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- //uint array[]={0,1,2,3,4,5,6,7,8,9};
- uchar displayDelayCount = 0;
- uchar displayIndex = 0;
- uint displayData = 0;
- uchar count;
- void xianshi(void)//
- {
- if(displayDelayCount >1)//
- {
- displayDelayCount = 0;
- if(displayIndex < 4) //
- {
- displayIndex ++;
- }
- else
- {
- displayIndex = 0;
- }
-
- if(displayIndex == 0)
- {
- // P1=0xff;
- Seg_W1=1;
- P1=biao[displayData%10];
- Seg_W1=0;
- }
- if(displayIndex == 1)
- {
- Seg_W2=1;
- P1=biao[displayData/10%10];
- Seg_W2=0;
- }
- if(displayIndex == 2)
- {
- Seg_W3=1;
- P1=biao[displayData/100%10];
- Seg_W3=0;
- }
-
- if(displayIndex == 3)
- {
- Seg_W4=1;
- P1=biao[displayData/1000%10];
- Seg_W4=0;
- }
- }
- }
- void time0()interrupt 1
- {
- TH0=(65536-2000)/256;
- TL0=(65536-2000)%256; //原来这里这位0.6ms,时间太短
- displayDelayCount++; //数码管点亮时间,这里设为2ms(@12MHz)
-
- count++;
- if(count==100)
- {
- count=0;
- displayData++; //显示数码 200ms加1
- if(displayData==9999)
- displayData=0;
- }
- }
- void main()
- {
- EA=1;
- ET0=1;
- TMOD=0X01;
- TH0=(65536-2000)/256;
- TL0=(65536-2000)%256;
- TR0=1;
-
- while(1)
- {
- xianshi();
- }
- }
复制代码 |