void Timer0_Init (void)
{
unsigned int RCAP0;
TMOD |= 0x01;
RCAP0 = 65535 -(SYSCLK/12/400); //24.5MHz主频下最小频率30Hz
timer0l = RCAP0 % 256;
timer0h = RCAP0 / 256;
TL0 = timer0l; //时钟设置为24.5MHz
TH0 = timer0h;
PT0 = 1; //高中断优先级
ET0 = 1; //开启定时器0中断
TR0 = 1; //启动定时器
}
void Timer0_ISR(void) interrupt 1 //中断周期约1ms
{
TL0 = timer0l; //时钟设置为24.5MHz
TH0 = timer0h;
PWM = ~PWM;
}
#include
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
sbit Waveout1=P1^0;
sbit CF=P3^2;//调节频率
int num,a,f;
int Timer0_H,Timer0_L;
void Init()
{
num=0;
a=1000;
f=2;
Waveout1=0;
TMOD=0x02;
TH0=(uint)(256-(1000/(2*f)));
TL0=(uint)(256-(1000/(2*f)));
ET0=1;
EA=1;
TR0=1;
}
void main()
{
Init();
while(1)
{
if(CF==0)
{
f++;
TH0=(int)(256-(1000/(2*f)));
TL0=(int)(256-(1000/(2*f)));
while(CF==0);
}
if(num==a)
{
num=0;
Waveout1=~Waveout1;
}
}
}
void Timer0()interrupt 1
{
num++;
}