基于stc15f2k60s2芯片单片机编程(NE555测频率)

main.c

#include 
#include 


void Timer1Init();	
void xianshi();
void Delay2ms();
void timer0();

unsigned char display[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char shuzu[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char flag;
unsigned int fre;


void main()
{

	P2=0XA0;P0=0X00;P2=0X1F;
	Timer1Init();	EA=1;ET1=1;
	timer0();ET0=1;

	while(1)
	{
		if(flag==1)
		{
			flag=0;
			TR0=0;
			fre=TH0*256+TL0;
			fre=fre*2;
			TL0=0;
			TH0=0;
			TR0=1;
			
		}
		xianshi();
	}
	


}

void Timer1Init(void)		//2毫秒@11.0592MHz
{
	AUXR |= 0x40;		//定时器时钟1T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0x9A;		//设置定时初值
	TH1 = 0xA9;		//设置定时初值
	TF1 = 0;		//清除TF1标志
	TR1 = 1;		//定时器1开始计时
}

void timer1() interrupt 3
{
	static unsigned int count=0;
	count++;
	if(count==500)
	{
		count=0;
		flag=1;
	}
}

void timer0()
{
		TMOD = 0x04;    
		TL0 = 0;
		TH0 = 0;
		TF0 = 0;
		TR0 = 1;
}




void xianshi()
{
	unsigned char i;
	P2=0XE0;
	P0=0XFF;
	P2=0X1F;
	
	P2=0XC0;
	P0=1<<i;
	P2=0X1F;
	
	P2=0XE0;
	P0=display[i];
	i++;
	if(i>=8)
	i=0;

	
	display[0]=0x8e;
	display[1]=0xff;
	display[2]=0xff;
	display[3]=shuzu[fre%100000/10000];
	display[4]=shuzu[fre%10000/1000];
	display[5]=shuzu[fre%1000/100];
	display[6]=shuzu[fre%100/10];
	display[7]=shuzu[fre%10/1];
	Delay2ms();
}
void Delay2ms()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	i = 22;
	j = 128;
	do
	{
		while (--j);
	} while (--i);
}

你可能感兴趣的:(基于stc15f2k60s2芯片单片机编程(NE555测频率))