STC89C52单片机用两个定时器控制蜂鸣器的频率和输出时间间隔

题目

同时用两个定时器控制蜂鸣器发声,定时器0控制频率,定时器1控制同个频率持续的时间,间隔300ms依次输出1,10,50,100,200,400,800,
1k(hz)的方波。

遇到的问题

出现定时器紊乱的现象,这个时候需要把T1定时器的优先级设为高于T0优先级

具体代码如下:

#include 

typedef unsigned int unint;
typedef unsigned char uchar;

sbit beep = P1^5;
sbit led = P2^0;
uchar t1,bphz,times;

uchar code table[]=
{
	500,300,200,100,50,25,12,10
};

void init();

void main()
{
	init();

	while(1)
	{
		if(t1==30)
		{
			led = ~led;
			t1 = 0;
			times++;
			if(times==8)
			{times =0;}
			else
			{	bphz=table[times]*100;}
		}
	}
}

void init(){
	times =0;
	led = 0;
	bphz = table[0]*100;
	TMOD = 0x11;	

	TH1 = (65536-1000)/256;
	TL1 = (65536-1000)%256;
	TH0 = (65536-bphz)/256;
	TL0 = (65536-bphz)%256;
	
	EA = 1;
	ET1 = 1;
	TR1 = 1;
	ET0 = 1;
	TR0 = 1;
	PT1 = 1;//在这里设置优先级

}

void time0() interrupt 1
{
	TR0=0;
	TH0 = (65536-bphz)/256;
	TL0 = (65536-bphz)%256;
	beep = ~beep;
	TR0=1;
}

void time1() interrupt 3
{
	TR1 = 0;
	TH1 = (65536-10000)/256;
	TL1 = (65536-10000)%256;
	t1++;
	TR1 = 1;
}


你可能感兴趣的:(硬件)