单片机利用定时器器实现数码管每隔1s增加一个数

#include
#include
#define uint unsigned int
#define uchar unsigned char

uchar temp,aa,num;
sbit dula=P2^6;
sbit wela=P2^7;

uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};

void delay(uint z);

void main()
{	
	num=0;
	TMOD=0x01;
	TH0=(65536-45728)/256;  //装初值11.0592M晶振,定时50ms数为45872
	TL0=(65536-45728)%256;
	EA=1;
	ET0=1;//开定时器0中断
	TR0=1;
	temp=0xfe;
	P1=temp;
	dula=1;
	P0=table[0];
	dula=0;
	wela=1;
	P0=0xc0;
	wela=0;
	while(1)
	{	
		
		delay(1000);
		temp=_crol_(temp,1);//temp左移一位
		P1=temp;
	}
}

void delay(uint z)
{
	uint x,y;
	for(x=z;x>0;x--)
		for(y=110;y>0;y--);
}

void time0() interrupt 1
{
	TH0=(65536-45728)/256;  //装初值11.0592M晶振,定时50ms数为45872
	TL0=(65536-45728)%256;
	aa++;
	if(aa==40)
		{
			aa=0;
			num++;
			if(num==16)
				num=0;
			dula=1;
			P0=table[num];
		}
}

你可能感兴趣的:(单片机,嵌入式硬件)