《蜂鸣器和弦音实现基于瑞萨单片机实现电路代码》由会员分享,可在线阅读,更多相关《蜂鸣器和弦音实现基于瑞萨单片机实现电路代码(5页珍藏版)》请在金锄头文库上搜索。
1、蜂鸣器和弦电路以下是基于瑞萨单片机实现代码:/* 文件:Beep.c 功能:蜂鸣器和弦音,定义三种音,关机,开机,单音 Author:DB.YAN,i_ 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 Ton
2、e_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_FRE
3、Q 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_Tbl24=/C
4、6,CS6, D6 ,DS6 ,E6 F6 ,FS6 ,G6 ,GS6 ,A6 ,AS6 ,B61047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976,/C7,CS7, D7 ,DS7 ,E7 F7 ,FS7 ,G7 ,GS7 ,A7 ,AS7 ,B72093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951,;/*=关机和弦1,2,3.=*/const uchar Sound_ShutDown10= Tone_C7,2,14,Tone_D7,2,6,Tone_E7,6,
5、60,END;/*=开机和弦3,2,1.=*/const uchar Sound_PowerOn10= Tone_E7,2,6,Tone_D7,2,14,Tone_C7,6,60,END;/*=单音和弦2.=*/const uchar Sound_Single10= 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;
6、/定时器计数值,通过改变计数值改变音频频率 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_
7、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 (*
8、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()功能:蜂鸣器控制调用:每10
9、ms调用一次=*/_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;