#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit BEEP = P1^2;
uchar STH0, STL0;
uchar time;
uint code SONG_TONE[] ={64580,64684,64777,64580,64580,64684,64777,64580,64777,64820,
64898,64777,64820,64898,64898,64968,64898,64820,64777,64580,
64898,64968,64898,64820,64777,64580,64684,64260,64580,64684,
64260,64580,0} ;
uint code SONG_LONG[] ={2,2,2,2,2,2,2,2,2,2,
4,2,2,4,1,1,1,1,2,2,
1,1,1,1,2,2,2,2,4,2,
2,4,0};
void delay(uint x)
{
uint i,j;
for(i=0;i<x;i++)
for(j=0;j<440;j++) ;
TR0=0;
}
void main(void)
{
unsigned char i;
TMOD=0x01;
ET0=1;
EA=1;
for(i=0;SONG_LONG[i]!=0;i++)
{
STH0=SONG_TONE[i]/256;
STL0=SONG_TONE[i]%256;
TR0=1;
time=SONG_LONG[i]*800;
delay(time);
}
}
void t0(void) interrupt 1 using 0{
TH0=STH0;
TL0=STL0;
BEEP=!BEEP;
}
我是菜鸟,这是我的蜂鸣器唱歌程序,但是它每次播放一次后,又重新循环播放。我想让它在播放一次后就停止播放。怎么弄?求高手指点。
关于单片机蜂鸣器唱歌的问题,怎样让音乐播放一次后停下来
答案:4 悬赏:0 手机版
解决时间 2021-02-15 06:07
- 提问者网友:酱爆肉
- 2021-02-14 14:47
最佳答案
- 五星知识达人网友:拾荒鲤
- 2021-02-14 15:06
参考这个例子:
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar i=0;
sbit SPK = P3^4;
sbit K1 = P1^0;
uchar code HI_LIST[]=
{
0,266,229,232,233,236,238,240,241,242,244,245,246,247,248
};
uchar code LO_LIST[]=
{
0,4,13,10,20,3,8,6,2,23,5,26,1,4,3
};
void T0_INT() interrupt 1
{
TL0 = LO_LIST[i];
TH0 = HI_LIST[i];
SPK = !SPK;
}
void DelayMS(uint ms)
{
uchar t;
while(ms--)
{
for(t=0;t<120;t++);
}
}
void main()
{
IE = 0x82;
TMOD = 0x00;
while(1)
{
while(K1==1);
while(K1==0);
for(i=1;i<15;i++)
{
TR0 = 1;
DelayMS(500);
TR0 = 0;
DelayMS(50);
}
}
}
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar i=0;
sbit SPK = P3^4;
sbit K1 = P1^0;
uchar code HI_LIST[]=
{
0,266,229,232,233,236,238,240,241,242,244,245,246,247,248
};
uchar code LO_LIST[]=
{
0,4,13,10,20,3,8,6,2,23,5,26,1,4,3
};
void T0_INT() interrupt 1
{
TL0 = LO_LIST[i];
TH0 = HI_LIST[i];
SPK = !SPK;
}
void DelayMS(uint ms)
{
uchar t;
while(ms--)
{
for(t=0;t<120;t++);
}
}
void main()
{
IE = 0x82;
TMOD = 0x00;
while(1)
{
while(K1==1);
while(K1==0);
for(i=1;i<15;i++)
{
TR0 = 1;
DelayMS(500);
TR0 = 0;
DelayMS(50);
}
}
}
全部回答
- 1楼网友:毛毛
- 2021-02-14 18:31
include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit BEEP = P1^2;
uchar STH0, STL0;
uchar time;
uint code SONG_TONE[] ={64580,64684,64777,64580,64580,64684,64777,64580,64777,64820,
64898,64777,64820,64898,64898,64968,64898,64820,64777,64580,
64898,64968,64898,64820,64777,64580,64684,64260,64580,64684,
64260,64580,0} ;
uint code SONG_LONG[] ={2,2,2,2,2,2,2,2,2,2,
4,2,2,4,1,1,1,1,2,2,
1,1,1,1,2,2,2,2,4,2,
2,4,0};
void delay(uint x)
{
uint i,j;
for(i=0;i<x;i++)
for(j=0;j<440;j++) ;
TR0=0;
}
void main(void)
{
unsigned char i;
TMOD=0x01;
ET0=1;
EA=1;
for(i=0;SONG_LONG[i]!=0;i++)
{
STH0=SONG_TONE[i]/256;
STL0=SONG_TONE[i]%256;
TR0=1;
time=SONG_LONG[i]*800;
delay(time);
}
}
void t0(void) interrupt 1 using 0{
TH0=STH0;
TL0=STL0;
BEEP=!BEEP;
- 2楼网友:毛毛
- 2021-02-14 16:54
你看一下音乐存放的数组song的话
会发现song数组中存放的规律是 频率,音长,频率,音长……
所以第一个temp1 = song[addr++]; 读出的是频率
接着temp2 = song[addr++];读出的是音长,音长即1拍、1/2拍、1/4拍等
- 3楼网友:愁杀梦里人
- 2021-02-14 15:52
main函数最后加一句:
void main(void)
{
unsigned char i;
TMOD=0x01;
ET0=1;
EA=1;
for(i=0;SONG_LONG[i]!=0;i++)
{
STH0=SONG_TONE[i]/256;
STL0=SONG_TONE[i]%256;
TR0=1;
time=SONG_LONG[i]*800;
delay(time);
}
while(1);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯