题目描述:同时用两个定时器控制蜂鸣器发声,定时器0控制频率,定时器1控制同个频率持续的时间,间隔300ms依次输出10,50,100,200,400,800,1k(HZ)的方波。
程序代码如下:
#include <reg52.h>
sbit BUZZ = P1^5;
unsigned int code HZ[] = {
50000,10000,5000,
2500,1250,650,500
};
unsigned char i = 0;
unsigned char j = 0;
unsigned char k = 0; // i为频率改变,j为频段改变,k为50ms脉冲信号时间
void Interrupt
timer0() interrupt 1
{
TH0 = (65536 - HZ[i])/256;
TL0 = (65536 - HZ[i])%256;
j = ~j;
}
void InterruptTimer1() interrupt 3
{
TH1 = (65536-50000)/256;
TL1 = (65536-50000)%256;
k++;
j = 0;
}
void main()
{
EA = 1;
TMOD = 0x11;
TH1 = (65536-50000)/256;
TL1 = (65536-50000)%256;
TR1 = 1; ET1 = 1;
TH0 = (65536 - HZ[0])/256;
TL0 = (65536 - HZ[0])%256;
TR0 = 1; ET0 = 1;
BUZZ = 1;
while (1);
{
if (j)
{
TR0 = 0;
BUZZ = ~BUZZ;
if (k >= 6)
{
i++;
k = 0;
}
if (i > 7)
{
i = 0;
}
TR0 = 1;
}
}
}
程序编译未报错,烧进
单片机里没有反应,求指导。
一周热门 更多>