蜂鳴器和弦音電路原理
原理介紹:
BP_PWR控制三極管Q5控制蜂鳴器的供電,Q5開啟後,給C6充電,此時Buzzer産生頻率,蜂鳴器發聲,音節快結束前,BP_PWR關閉Q5,此時蜂鳴器靠C6放電提供能量發聲,實現拖音和弦效果。
以下是基于瑞薩單片機實現代碼:
/********************************************************************************
文件:Beep.c
功能:蜂鳴器和弦音,定義三種音,關機,開機,單音
Author:電子顔工
Time:2013.10.15
Recent Modify:2013.10.16
******************************************************************************/
/*=======================以下是音調定義======================*/
#define Tone_C6 1
#define Tone_CS6 2
#define Tone_D6 3
#define Tone_DS6 4
#define Tone_E6 5
#define Tone_F6 6
#define Tone_FS6 7
#define Tone_G6 8
#define Tone_GS6 9
#define Tone_A6 10
#define Tone_AS6 11
#define Tone_B6 12
#define Tone_C7 13
#define Tone_CS7 14
#define Tone_D7 15
#define Tone_DS7 16
#define Tone_E7 17
#define Tone_F7 18
#define Tone_FS7 19
#define Tone_G7 20
#define Tone_GS7 21
#define Tone_A7 22
#define Tone_AS7 23
#define Tone_B7 24
#define END 0//音頻結束位
#define SYS_FREQ 8 // MHZ 系統時鐘
#define TIMER_CNT_BASE 1000000*SYS_FREQ/2 //定時器計數基數
#define BUZZER_IO P3_bit.no1 //BEEP IO
#define BUZZER_OUT PM3_bit.no1=0 //Mode OUT
#define BUZZER_PULL PU3_bit.no1=1 //Pull up
#define PWR_CTRL P13_bit.no0 //蜂鳴器供電控制腳
#define BUZZERIO_INIT {BUZZER_OUT;BUZZER_PULL;}
/*=========================音調頻率表=======================*/
const uint Tune_Tbl[24]=
{
//C6,CS6, D6 ,DS6 ,E6 F6 ,FS6 ,G6 ,GS6 ,A6 ,AS6 ,B6
1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976,
//C7,CS7, D7 ,DS7 ,E7 F7 ,FS7 ,G7 ,GS7 ,A7 ,AS7 ,B7
2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951,
};
/*======================關機和弦1,2,3...====================*/
const uchar Sound_ShutDown[10]=
{
Tone_C7,2,14,Tone_D7,2,6,Tone_E7,6,60,END
};
/*======================開機和弦3,2,1...====================*/
const uchar Sound_PowerOn[10]=
{
Tone_E7,2,6,Tone_D7,2,14,Tone_C7,6,60,END
};
/*======================單音和弦2...=======================*/
const uchar Sound_Single[10]=
{
Tone_D7,12,60,END,END,END,END,END,END,END,
};
#define S_SHUTDOWN 1//關機音
#define S_POWERON 2//開機音
#define S_SINGLE 3//單音
const uchar (*pSoundSel)[10];//選曲指針
struct BE
{
uint CountFR; //定時器計數值,通過改變計數值改變音頻頻率
uchar PWR_time; //開啟時間,單位為10mS
uchar FREQ_time; //音頻供給時間,單位為10mS
uchar index; //
}Beep;
volatile union { uchar flag4; __BITS8 flag4_bit; };
#define Beep_EN flag4_bit.no0
#define Beep_Finish flag4_bit.no1 //和弦結束标志
/*=======================================================
函數名:Beep_On(uchar SoundSel)
傳入參數:SoundSel
功能:選擇聲音種類
調用:外部調用
=========================================================*/
__callt void Beep_On(uchar SoundSel)
{
Beep_EN=1;
Beep.index=0;
Beep_Finish=0;
switch(SoundSel)
{
case S_SHUTDOWN:
pSoundSel=&Sound_ShutDown;
break;
case S_POWERON:
pSoundSel=&Sound_PowerOn;
break;
case S_SINGLE:
pSoundSel=&Sound_Single;
break;
default:
break;
}
}
/*=======================================================
函數名:Beep_set()
功能:蜂鳴器參數設置
調用:Beep_control()
=========================================================*/
__callt void Beep_set()
{
if ((*pSoundSel)[Beep.index]!=END)
{
Beep.CountFR=(uint)(TIMER_CNT_BASE/Tune_Tbl[(*pSoundSel)[Beep.index]-1]);
Beep.PWR_time=(*pSoundSel)[Beep.index 1];
Beep.FREQ_time=(*pSoundSel)[Beep.index 2];
Beep.index=Beep.index 3;
}else {Beep.index=0; PWR_CTRL=1;Beep_EN=0;Beep_Finish=1;}
return;
}
/*=======================================================
函數名:Beep_control()
功能:蜂鳴器控制
調用:每10ms調用一次
=========================================================*/
__callt void Beep_control()//每10mS調用一次
{
if (Beep_EN)
{
if (Beep.PWR_time)
{
PWR_CTRL=0;//供電
Beep.PWR_time--;
}
else
{
PWR_CTRL=1;//掉電,依靠電容放電實現和弦拖音
}
if (Beep.FREQ_time)
{
Beep.FREQ_time--;
CR000=Beep.CountFR;
TOC00=0x03;
//TMC00=0X0C;//啟動操作
}
else
{
TOC00=0x02;
//TMC00=0X00;//啟動操作
Beep_set();
PWR_CTRL=0;//發聲完畢,打開電源給電容充電
}
}
else
{
BUZZER_IO=0;
TOC00=0x02;
}
return;
}
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!