#include<reg52.h>
#include <intrins.H>
#define uchar unsigned char
#define uint unsigned int
//数码管的段码编码
uchar DATA_LED_Point[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
uchar BIT_LED[]={0xfe,0xfd,0xfb,0xf7,0xef};//定义需要点亮的数码管,从左往右5~1数码管
uchar disp[5];
uchar led_bit;
uchar timer_count=0;
uint num=0;
void delay_1ms(uchar t) //延时1*t个ms
{
uchar a,b;
for(b=t;b>0;b--)
for(a=148;a>0;a--);
}
void InitTimer0(void) //2ms
{
TMOD = 0x01;
TH0 = 0x0Fc;
TL0 = 0x018;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void Timer0Interrupt(void) interrupt 1
{
uint j;
TH0 = 0x0fc;
TL0 = 0x018;
timer_count++;
if(timer_count==10)
{
timer_count=0 ;
for(j=0;j<5000;j++)
{
disp[4]=num/10000;
disp[3]=(num%10000)/1000; /*取实际值的十位*/
disp[2]=(num%1000)/100; /*取实际值的个位*/
disp[1]=(num%100)/10; /*取设定值的十位*/
disp[0]=num%10; /*取设定值的个位*/
P0=BIT_LED[led_bit];
P2=0xff;
delay_1ms(1) ;
P0=BIT_LED[led_bit];
P2=DATA_LED_Point[disp[led_bit]];
delay_1ms(3) ;
led_bit++;
if(led_bit>5)
{
led_bit=0;
}
}
}
}
void main()
{
InitTimer0();
while(1)
{
num++;
if(num>65535)
num=0;
}
} |