另一种是无缘蜂鸣器(没有正负极),发出的音调可以改变,可以用于播放音乐
你这个是有源蜂鸣器,不可以的,因为它调调是固定的
有源无源的区别方法很简单,可以看蜂鸣器引脚位置,也就是下面,如果有黑色胶封装的话,就是有源蜂鸣器,反之,是无源蜂鸣器
纯手打,请采纳,谢谢
//此程序在硬件上调试通过
//本程序的单片机晶振采用11.0592M
#include <reg51.h>
sbit speaker=P1^2
unsigned char timer0h,timer0l,time
//生日歌
code unsigned char sszymmh[]={5,1,1, 5,1,1, 6,1,2, 5,1,2, 1,2,2, 7,1,4,
5,1,1, 5,1,1, 6,1,2, 5,1,2, 2,2,2, 1,2,4,
5,1,1, 5,1,1, 5,2,2, 3,2,2, 1,2,2, 7,1,2, 6,1,2,
4,2,1, 4,2,1, 3,2,2, 1,2,2, 2,2,2, 1,2,4}
// 音阶频率表 高八位
code unsigned char FREQH[]={
0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,
0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i
0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,
0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,
}
// 音阶频率表 低八位
code unsigned char FREQL[]={
0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,
0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i
0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,
0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,
}
void delay(unsigned char t)
{
unsigned char t1
unsigned long t2
for(t1=0t1<tt1++)
{
for(t2=0t2<8000t2++)
{
}
}
TR0=0
}
void t0int() interrupt 1
{
TR0=0
speaker=!speaker
TH0=timer0h
TL0=timer0l
TR0=1
}
void song()
{
TH0=timer0h
TL0=timer0l
TR0=1
delay(time)
}
void main(void)
{
unsigned char k,i
TMOD=1 //置CT0定时工作方式1
EA=1
ET0=1//IE=0x82 //CPU开中断,CT0开中断
while(1)
{
i=0
while(i<75){ //音乐数组长度 ,唱完从头再来
k=sszymmh[i]+7*sszymmh[i+1]-1
timer0h=FREQH[k]
timer0l=FREQL[k]
time=sszymmh[i+2]
i=i+3
song()
}
}
}
P12接J31的7脚,我在板子上已经测试过了,用的是89S52
花时间编的,希望你能采纳
#include<reg51.h>#define uchar unsigned char
#define uint unsigned int
sbit SPK=P1^0
sbit K1=P1^7
//发声函数,注意这里是用扬声器,不能用普通的蜂鸣器
void Alarm(uchar t)
{
uchari,j
for(i=0i<200i++)
{
SPK=~SPK
for(j=0j<tj++)//由参数t行成不同的频率产生不同的声音
}
}
voidmain()
{
SPK=0
while(1)
{
if(K1==1)
{
Alarm(90)
Alarm(120)
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)