请教一个51单片机定时中断的问题
程序如下,在定时器T0完成5秒计时后启动T1定时器,spk为蜂鸣器,spk=~spk,蜂鸣器工作工作10秒定时器T1停止工作,那么蜂鸣器是怎么停止工作的呢?请教下各位大佬。
void time0(void) interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
t0++;
if(t0==100)
{
t0=0;
TR0=0;
TR1=1;
t1=0;
}
}
void time1(void) interrupt 3
{
TH1=(65535-1000)/256;
TL1=(65535-1000)%256;
t1++;
spk=~spk;
if(t1==10000)
{
t1=0;
TR1=0;
}
}
如果直流蜂鸣器,假设spk = 1,即spk为高电平是蜂鸣器启动,那关闭蜂鸣器只需要spk = 0就可以了
看你的程序是使用了无源蜂鸣器,普通电磁式无源蜂鸣器需要2~4KHz脉冲驱动,否则发声不好听。你T1设置1ms不合适。修改如下:
void time0(void) interrupt 1
{
TH0=(65535-50000)/256;//50ms
TL0=(65535-50000)%256;//50ms
t0++;
if(t0==100) //5秒
{
t0=0;
TR0=0;
TR1=1;
t1=0;
}
}
void time1(void) interrupt 3
{
// TH1=(65535-1000)/256;//1ms
// TL1=(65535-1000)%256;//1ms
TH1 = 0xFF; //250us
TL1 = 0x06; //250us
t1++;
spk=~spk; //无源蜂鸣器至少需2000Hz
if(t1==40000) //10秒
{
t1=0;
TR1=0;
spk=1;//回到初始状态0或1
}
}
多谢,就是无源蜂鸣器需要脉冲驱动的问题。无脉冲驱动后,蜂鸣器不发声,是这个意思吧?
有源蜂鸣器只需要提供蜂鸣器所需要的供电就可以发声,无源的蜂鸣器需要产生一定频率的脉冲驱动才可以发声,没有的脉冲驱动,就不会发声了
我用有源蜂鸣器用这样驱动有问题吗?大佬。
以这个图为例的话,有源蜂鸣器的驱动是开启蜂鸣器时,对三极管Q1置一,使三极管导通,蜂鸣器就发声了,发声后就不需要再管了,关闭蜂鸣器时,对三极管Q1置0,关闭三极管,蜂鸣器就关闭了。
对于无源蜂鸣器就需要象你原来程序那样产生一个脉冲信号使蜂鸣器发声,只要不产生脉冲信号,蜂鸣器就不发声了。你原来的程序,在关闭定时器1时,蜂鸣器应该就不在发声了,因为脉冲信号由定时器1产生,定时器关闭后,就不再产生脉冲信号了,蜂鸣器也就不发声了。
是这样的,在蜂鸣器停止后一定不要有电流流过蜂鸣器,也就是根据实际电路置相应端口为0或1.关闭蜂鸣器直流通路。
学生party一枚,自学单片机,多谢各位大佬解答。
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar t0,t1;
sbit spk=P2^7;
void main()
{
TMOD=1;
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=0;
while(1)
{
if(t1==200)
{
t1=0;
TR1=0;
spk=~spk;
}
}
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
t0++;
if(t0==100)
{
t0=0;
TR0=0;
TR1=1;
spk=0;
}
}
void timer2() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
t1++;
}
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar t0,t1;
sbit spk=P2^7;
void main()
{
TMOD=1;
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=0;
while(1)
{
if(t1==200)
{
t1=0;
TR1=0;
spk=~spk;
}
}
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
t0++;
if(t0==100)
{
t0=0;
TR0=0;
TR1=1;
spk=0;
}
}
void timer2() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
t1++;
}
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar t0,t1;
sbit spk=P2^7;
void main()
{
TMOD=1;
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=0;
while(1)
{
if(t1==200)
{
t1=0;
TR1=0;
spk=~spk;
}
}
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
t0++;
if(t0==100)
{
t0=0;
TR0=0;
TR1=1;
spk=0;
}
}
void timer2() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
t1++;
}
spk=~spk应该在if语句里面。
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar t0,t1;
sbit spk=P2^7;
void main()
{
TMOD=1;
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=0;
while(1)
{
if(t1==200)
{
t1=0;
TR1=0;
spk=~spk;
}
}
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
t0++;
if(t0==100)
{
t0=0;
TR0=0;
TR1=1;
spk=0;
}
}
void timer2() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
t1++;
}
spk=~spk应该在if语句里面。
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar t0,t1;
sbit spk=P2^7;
void main()
{
TMOD=1;
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=0;
while(1)
{
if(t1==200)
{
t1=0;
TR1=0;
spk=~spk;
}
}
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
t0++;
if(t0==100)
{
t0=0;
TR0=0;
TR1=1;
spk=0;
}
}
void timer2() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
t1++;
}
spk=~spk应该在if语句里面。
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar t0,t1;
sbit spk=P2^7;
void main()
{
TMOD=1;
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=0;
while(1)
{
if(t1==200)
{
t1=0;
TR1=0;
spk=~spk;
}
}
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
t0++;
if(t0==100)
{
t0=0;
TR0=0;
TR1=1;
spk=0;
}
}
void timer2() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
t1++;
}
spk=~spk应该在if语句里面。
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar t0,t1;
sbit spk=P2^7;
void main()
{
TMOD=1;
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=0;
while(1)
{
if(t1==200)
{
t1=0;
TR1=0;
spk=~spk;
}
}
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
t0++;
if(t0==100)
{
t0=0;
TR0=0;
TR1=1;
spk=0;
}
}
void timer2() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
t1++;
}
spk=~spk应该在if语句里面。
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar t0,t1;
sbit spk=P2^7;
void main()
{
TMOD=1;
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=0;
while(1)
{
if(t1==200)
{
t1=0;
TR1=0;
spk=~spk;
}
}
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
t0++;
if(t0==100)
{
t0=0;
TR0=0;
TR1=1;
spk=0;
}
}
void timer2() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
t1++;
}
spk=~spk应该在if语句里面。
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar t0,t1;
sbit spk=P2^7;
void main()
{
TMOD=1;
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=0;
while(1)
{
if(t1==200)
{
t1=0;
TR1=0;
spk=~spk;
}
}
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
t0++;
if(t0==100)
{
t0=0;
TR0=0;
TR1=1;
spk=0;
}
}
void timer2() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
t1++;
}
spk=~spk应该在if语句里面。
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar t0,t1;
sbit spk=P2^7;
void main()
{
TMOD=1;
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=0;
while(1)
{
if(t1==200)
{
t1=0;
TR1=0;
spk=~spk;
}
}
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
t0++;
if(t0==100)
{
t0=0;
TR0=0;
TR1=1;
spk=0;
}
}
void timer2() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
t1++;
}
spk=~spk应该在if语句里面。
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar t0,t1;
sbit spk=P2^7;
void main()
{
TMOD=1;
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=0;
while(1)
{
if(t1==200)
{
t1=0;
TR1=0;
spk=~spk;
}
}
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-5000)%256;
t0++;
if(t0==100)
{
t0=0;
TR0=0;
TR1=1;
spk=0;
}
}
void timer2() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-5000)%256;
t1++;
}
spk=~spk应该在if语句里面。