天天看點

C51單片機靜态數位管—(0-F)

數位管靜态顯示輪換

#include<reg52.h>

typedef unsigned char u8;
typedef unsigned int u16;

sbit KZA=P2^2;			  //控制顯示數位管具體位
sbit KZB=P2^3;
sbit KZC=P2^4;
							   //顯示位代碼
u8 code shumag[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
				0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
u8 a=10,i=0;		//不能在main函數裡定義變量
void main()
{
	KZA=0;
	KZB=0;
	KZC=0;
	P0=shumag[i];
	TMOD=0x10;				//定時器1
	TL1=0xb0;				//100ms
	TH1=0x3c;
	IE=0x88;
	TR1=1;
	while(1)
	{
		
		while(1)
		{
			if(a==0) break;
		}
		a=10;				  //a=10為1s
		i++;
		if(i==16) i=0;
		P0=shumag[i];
	}
}

void int1sever(void) interrupt 3 using 1
{
	TF1=0;
	TR1=0;
	a--;
	TL1=0xb0;
	TH1=0x3c;
	TR1=1;
}
           

唯一缺點是在main函數裡定義變量會出錯,隻能定義全局變量。