电路图
.C文件
#include "music.h" #include "pwm.h" //=====蜂鸣器声音结构定义===== typedef struct tBUZHX { unsigned int BzFre ; //蜂鸣器 频率 unsigned char T_BzO ; //蜂鸣间 频率输出时间 unsigned char T_BzV ; //蜂鸣器 VCC供电时间 }tBuzHx ; tBuzHx *tBzHx ; #define VTB1 20 //20 #define VTB2 6 //20 #define VTB3 102 //50 tBuzHx Bzx0[]={{VFR0,0,0}} ; tBuzHx Bzx1[]={{VFR2,VTB3,VTB2},{VFR0,0,0}} ; //const tBuzHx Bzx2[]={{VP2K3,20,20},{VP2K6,20,20},{VP2K9,210,10},{VFR0,0,0}} ; //const tBuzHx Bzx3[]={{VP2K9,20,20},{VP2K6,20,20},{VP2K3,210,10},{VFR0,0,0}} ; tBuzHx Bzx2[]={{VFR1,VTB1,VTB2},{VFR2,VTB1,VTB2},{VFR3,VTB3,VTB2},{VFR0,0,0}} ; tBuzHx Bzx3[]={{VFR3,VTB1,VTB2},{VFR2,VTB1,VTB2},{VFR1,VTB3,VTB2},{VFR0,0,0}} ; bit FLAG_BUZZER_ON; unsigned char RBzStatus = 0; //*************************************** // 函数名称:SetupBzhx // 函数功能:设置蜂鸣器模式 // 入口參数:该模式下的蜂鸣次数 蜂鸣相关数据清0 // 出口參数:无 // 返 回 值:无 //*************************************** void BUZZER_DRIVE(unsigned char MBz) { RBzStatus = MBZ_P1 ; BuzVcc_OFF ; BUZ_OFF ; // TIMER5_IE_DIS ; SET_PWM1_OFF(); switch(MBz) { case M_BZ_KEY: tBzHx = Bzx1 ; break ; case M_BZ_ON: tBzHx = Bzx2 ; break ; case M_BZ_OFF: tBzHx = Bzx3 ; break ; default : tBzHx = Bzx0 ; RBzStatus = MBZ_RST ; break ; } } //************************************* // 函数名称:CmdBzhx // 函数功能:蜂鸣器控制- // 入口參数:无 // 出口參数:无 // 返 回 值:无 //*************************************** void CmdBzhx(void) { static tBuzHx Bx1 ; switch(RBzStatus) { case MBZ_P1 : Bx1 = *tBzHx ; SET_PWM1_FREQUENCY(Bx1.BzFre) ;//设置PWM频率 if(Bx1.BzFre != VFR0) //非结束符 { RBzStatus = MBZ_P2 ; if(Bx1.T_BzV != 0) { Bx1.T_BzV -- ; BuzVcc_ON ; } else { // BuzVcc_OFF ; } if(Bx1.T_BzO != 0) { Bx1.T_BzO -- ; SET_PWM1_ON() ; } else { // TIMER5_IE_DIS ; } } else //为结束符 { RBzStatus = MBZ_RST ; } break ; case MBZ_P2 : if(Bx1.T_BzV != 0) { Bx1.T_BzV -- ; BuzVcc_ON ; } else { BuzVcc_OFF ; } if(Bx1.T_BzO != 0) { Bx1.T_BzO -- ; // TIMER5_IE_ENB ; SET_PWM1_ON(); } else { // TIMER5_IE_DIS ; SET_PWM1_OFF(); BUZ_OFF ; tBzHx ++ ; //取下一个音调 RBzStatus = MBZ_P1 ; } break ; default : break ; } }
.H文件
#ifndef _BUZ_H_ #define _BUZ_H_ #include "io.h" extern bit FLAG_BUZZER_ON; //SetupBzhx(M_BZ_KEY) ; //=====buz 驱动频率定义============== //声音结束符定义== #define VFR0 0 #define VFR1 VP3K3 #define VFR2 VP4K1 #define VFR3 VP4K9 #define VP2K3 110 //2k #define VP2K6 100 #define VP2K9 85 // #define VP3K3 75//12000/2/3.3 //4K #define VP4K1 60//12000/2/4.1 #define VP4K9 50//12000/2/4.9 // //=====BUZ port操作================== #define BUZ_ON FLAG_BUZZER_ON = 1 #define BUZ_OFF FLAG_BUZZER_ON = 0// #define BuzVcc_ON BUZZER_EN_IO = 1 #define BuzVcc_OFF BUZZER_EN_IO = 0 //=====蜂鸣器声音种类================ #define M_BZ (unsigned char)0 //无 #define M_BZ_KEY (unsigned char)1 //按键音 #define M_BZ_ON (unsigned char)2 //开机音 #define M_BZ_OFF (unsigned char)3 //关机音 //=====蜂鸣器工作过程状态========== #define MBZ_RST (unsigned char)0 #define MBZ_P1 (unsigned char)1 #define MBZ_P2 (unsigned char)2 #define MBZ_P3 (unsigned char)3 #define MBZ_P4 (unsigned char)4 //====函数声明===== extern void BUZZER_DRIVE(unsigned char MBz) ; extern void CmdBzhx(void) ; #endif