定时器计数 数码管显示0到99 (中断和查询两种方式)

定时器中计数 数码管显示0到99

定时器计数 数码管显示0到99 (中断和查询两种方式)_第1张图片

定时器中断方式计数

#include
#define uchar unsigned char
#define uint unsigned int
uchar tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
         0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};  
uchar data_L, data_H;
uchar data_0, b;  
void delay(uchar time)
{
	uchar m;
	for(m=0;m<time;m++)
	;
}
void T0_init()
{
   TMOD = 0x05;            
	 TH0=0xFF;        
   TL0=0xFF;
   EA=1; 
   ET0=1; 
   TR0=1;
}
void timer0( ) interrupt 1 
{
	TH0=0xFF;        
  TL0=0xFF;
	if(data_0==99)
	{
		data_0=0;
	}
	else
	{
		data_0++;
	}
 
}
void data_in(void)
   { data_L=data_0%10;
     data_H=data_0/10;
	 }
void display(void)
    {
		 P2=0x01;
     P0=tab[data_H];
     delay(100);
     P2=0x02;
     P0=tab[data_L];
     delay(100);
		}
void main()
{
	data_0=0;
	T0_init();
	
	while(1)
	{
	data_in();
	display();
	}
	
}	

定时器查询方式计数

#include 
#include 
#define uchar unsigned char
#define uint unsigned int
uchar tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};   

uchar data_L, data_H;
uchar a, b;  

void delay( uint k)
{
	uint m, n;
	for(m =0; m<k; m++)
		 {
			 for(n=0;n<120;n++);
		 }
}
	 
void display(void)
{
 P2=0x01;
 P0=tab[data_H];
 delay(1);
 P2=0x02;
 P0=tab[data_L];
 delay(1);
}
		
void count0(void)
{
	if(TF0==1)
	{
	TH0=0xFF;
	TL0=0xFF;
	 if(a==99)
		{ 
		 a=0;
		}
	 else
		{
		 a++;
		}
	 TF0=0;
	}
}

	 
void data_in(void)
{   
    data_L=a%10;
	data_H=a/10;
}

void T0_init( void)
{ 
	TMOD=0x05;
	TH0 =0XFF;
	TL0=0xFF;
	TR0=1; 
	EA =0;
}
		
void main( void)
{ 
 
 a=0;
 T0_init();
 while(1)
 {
	count0();
	data_in();
	display();
 }
}

 
 
 
 
 
		 

你可能感兴趣的:(51单片机)