编写程序,让单片机P2.6口交替输出1KHZ和2KHZ的方波信号,实现蜂鸣器的变 频报警,交替时间

2024-10-30 11:17:25
有1个网友回答
网友(1):

#include
#define uchar unsigned char
uchar cnt=0;
bit flag=0,pf=0;
sbit alarm=P2^6;
void t0isr() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
cnt++;
if(cnt>=20)
{
cnt=0;
flag=~flag;
}
}
void t1isr() interrupt 3
{
if(flag)
{
pf=~pf;
if(pf)alarm=~alarm;
}
else alarm=~alarm;
}
main()
{
TMOD=0x21;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=6;
TL1=6;
TR0=1;
TR1=1;
ET0=1;
ET1=1;
EA=1;
while(1);
}