一、工作原理:
1.利用STM32的定时器PWM输出通道,驱动蜂鸣器以特定频率发声,实现播放音乐的效果。
2.C调音符与频率对照表:
3.以下为常用的七声音阶频率(Hz):
#define MC 262 Do
#define MD 294 Re
#define ME 330 Mi
#define MF 349 Fa
#define MG 392 Sol
#define MA 440 La
#define MB 494 Si
#define MCC 523 Do
二、软件设计:
1.蜂鸣器GPIO和定时器初始化:
void PWM_Init_TIM1(u16 Psc,u16 Per)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB2PeriphClockCmd(PWMA_GPIO_CLK | PWMA_TIMx_CLK,ENABLE);//开启时钟
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP; // 初始化GPIO--PA8、PA11为复用推挽输出
GPIO_InitStruct.GPIO_Pin=PWMA_GPIO_PIN ;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(PWMA_GPIO_PORT,&GPIO_InitStruct);
GPIO_ResetBits(PWMA_GPIO_PORT, PWMA_GPIO_PIN);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct); // 初始化定时器。
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period=Per; //arr
TIM_TimeBaseInitStruct.TIM_Prescaler=Psc;
TIM_TimeBaseInit(PWMA_TIMx,&TIM_TimeBaseInitStruct); // TIM1
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1; // 初始化输出比较
TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse=Per/2;
TIM_OC1Init(PWMA_TIMx,&TIM_OCInitStruct);
TIM_OC1PreloadConfig(PWMA_TIMx,TIM_OCPreload_Enable);// OC1预装载寄存器使能
TIM_ARRPreloadConfig(PWMA_TIMx,ENABLE);// TIM1在ARR上预装载寄存器使能
/*Open PWM output*/
TIM_CtrlPWMOutputs(PWMA_TIMx,ENABLE);// 高级定时器专属!!!--MOE主输出使能
TIM_Cmd(PWMA_TIMx,ENABLE); // 开定时器。
buzzerQuiet(); //关闭蜂鸣器
}
2.控制蜂鸣器发声:
//compare:比较值
void TIM_SetTIM1Compare1(u32 compare)
{
TIM1->CCR2=compare;
}
//设置TIM3通道2的arr
void TIM_SetTIM1Autoreload(u32 Autoreload)
{
TIM1->ARR=Autoreload;
}
void buzzerQuiet(void)//停止发声
{
buzzerSound(20000, 20);
// TIM1->CCR2=0;
// TIM_CtrlPWMOutputs(PWMA_TIMx,DISABLE);
// TIM_Cmd(PWMA_TIMx,DISABLE);
}
void buzzerOpen(void)//发声
{
TIM_CtrlPWMOutputs(PWMA_TIMx,ENABLE);
TIM_Cmd(PWMA_TIMx,ENABLE);
}
void buzzerSound(unsigned short usFraq,unsigned char volume_level)//发声模块
{
unsigned long Autoreload;
buzzerOpen();
if((usFraq<=122)||(usFraq>20000))
{
buzzerQuiet();
}
else
{
Autoreload=(8000000/usFraq)-1;
TIM_SetTIM1Autoreload(Autoreload);
TIM_SetTIM1Compare1(Autoreload>>volume_level);
}
}
3.频率对照表宏定义:
#define LC 262
#define LD 294
#define LE 330
#define LF 349
#define LG 392
#define LA 440
#define LAP_LBN 466
#define LB 494
#define MC 523
#define MD 587
#define ME 659
#define MEP 740
#define MF 698
#define MG 784
#define MA 880
#define MAP_MBN 932
#define MB 988
#define HC 1046
#define HD 1175
#define HE 1318
#define HF 1397
#define HG 1568
#define HA 1760
#define HB 1976
4.播放音乐(爱的罗曼史):
uint16_t romance[96]=
{
ME,ME,ME, ME,MD,MC, MC,LB,LA, LA,MC,ME, MA,MA,MA,
MA,MG,MD, MF,ME,MD, MD,ME,MF, ME,MF,ME, MEP,MF,ME,
ME,MD,MC, MC,LB,LA, LB,LB,LB, LB,MC,LB, LA,LA,LA, LA,20000,20000,
ME,ME,ME, ME,MD,MC, MC,LB,LB, LB,LAP_LBN,LB, MA,MA,MA,
MA,MB,MA, MA,MG,MG, MG,MA,MB, HC,HC,HC, HC,MB,MAP_MBN,
MA,MA,MA, MA,ME,MD, ME,ME,ME, ME,MF,MD, MC,MC,MC, MC,20000,20000
};
void Romance(void)
{
uint16_t i=0;
for(i=0; i<96; i++)
{
buzzerSound(romance[i], 200);
delay_ms(150);
buzzerQuiet();
delay_ms(150);
}
buzzerQuiet();
delay_ms(500);
}
百度网盘:链接:https://pan.baidu.com/s/1O6lrCHNmSDWRfdQL33pcJg?pwd=mzu9
提取码:mzu9
--来自百度网盘超级会员V2的分享文章来源:https://www.toymoban.com/news/detail-465084.html
文章来源地址https://www.toymoban.com/news/detail-465084.html
到了这里,关于STM32-PWM驱动无源蜂鸣器播放音乐(附网盘代码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!