【学习心得】蜂鸣器播放音乐
以下是学习STM32控制蜂鸣器时的一些心得体会,我也是综合各种资料写出来的。
蜂鸣器是一种很常见的电子元件,一般也就发出滴滴的声音。
但自从在网上看到各种用蜂鸣器播放音乐的实例,我就对蜂鸣器产生了浓厚的兴趣。
我了解到,蜂鸣器可以根据频率改变发出声音的音调,具体音符对应频率如下:
| 编号 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 音符 | 低1 | 低2 | 低3 | 低4 | 低5 | 低6 | 低7 | 中1 | 中2 | 中3 | 中4 | 中5 | 中6 | 中7 | 高1 | 高2 | 高3 | 高4 | 高5 | 高6 | 高7 | 不发声 |
| 频率 | 262 | 294 | 330 | 349 | 392 | 440 | 494 | 523 | 578 | 659 | 698 | 784 | 880 | 988 | 1045 | 1175 | 1318 | 1397 | 1568 | 1760 | 1976 | 2000 |
那么还需要知道一首曲子的节拍,就可以完成一首音乐了。具体节拍对应编号如下:
| 编号 | 1 | 2 | 3 | 4 |
| 节拍 | 1/4拍 | 1/2拍 | 1拍 | 2拍 |
那么只需要找到一首歌曲的简谱,写出对应音符的数组和对应节拍的数组,就算完成第一步了。
接下来进行STM32 的库函数程序编写。我使用的是正点原子的精英版。基本库函数也是配套的。
我选取的歌曲是《动物世界》部分和《see you again》部分。
需要编写的库函数如下:
#include "stm32f10x.h"
#include "beep.h"
#include "sys.h"
#include "delay.h"void beep_init(void)
{GPIO_InitTypeDef GPIO_InitStruct;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStruct);GPIO_ResetBits(GPIOB,GPIO_Pin_8);}void beepon(void)
{GPIO_SetBits(GPIOB,GPIO_Pin_8);
}void beepoff(void)
{GPIO_ResetBits(GPIOB,GPIO_Pin_8);
}void sound(u16 frq)
{u32 n;if(frq!=2000){n=500000/((u32)frq);beepoff();delay_us(n);beepon();delay_us(n);}else{beepoff();delay_us(1000);}}void music(void)
{// 低1 低2 低3 低4 低5 低6 低7 中1 中2 中3 中4 中5 中6 中7 高1 高2 高3 高4 高5 高6 高7 不放声u16 tone[]={262,294,330,349,392,440,494,523,578,659,698,784,880,988,1046,1175,1318,1397,1568,1760,1976,2000};// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 //歌曲《动物世界》简谱u8 playmusic[]={7,6,5,6,7,9,9,6,6,7,6,5,6,7,9,9,5,5,5,4,2,5,4,2,5,4,2,5,4,1,2,2,2,21,21,21,3,2,1,2,3,7,7,6,6,1,2,2,1,1,6,6,5,5,5,4,2,5,4,2,5,6,7,6,6,5,6,4,4,4, 7,6,5,6,7,9,9,6,6,7,6,5,6,7,9,9,5,5,5,4,2,5,4,2,5,4,2,5,4,9,10,10,9,9,9,21,21,21,8,8,8,8,8,9,10,9,6,8,7,6,7,7,7,5,9,4,12,12,12,5,7,9,12,12,11,12,11,8,9,11,8,7,7,7,5,11,12,11,12,11,8,9,9,9,5,7,9,12,12,11,12,11,8,9,11,8,7,7,5,5,5,5,9,8,7,6,6,6,6,5,7,9,12,12,11,12,11,8,9,11,8,7,7,7,5,12,12,12,14,13,12,11,11,11,11,11,11,21,12,11,9,12,11,9,12,11,11,8,7,6,7,6,4,5,11,9,8,8,8,8,9,8,7,5,5,5,5,21,21,21};//1是1/4拍 2是半拍,4是一拍,8是两拍 u8 time[]={2,2,2,2,2,2,4,4,4,2,2,2,2,2,2,4,4,4,2,2,2,2,2,2,2,2,2,2,2,2,4,4,4,4,4,4,2,2,2,2,2,2,4,4,4,2,2,2,2,2,2,4,4,4,2,2,2,2,2,2,4,4,4,4,4,2,2,4,4,4,2,2,2,2,2,2,4,4,4,2,2,2,2,2,2,4,4,4,2,2,2,2,2,2,2,2,2,2,2,1,1,2,2,4,4,4,4,4,2,2,2,2,2,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,2,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,2,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,2,2,4,4,4,4,4,4,4,4,2,2,4,4,4,4,4,4,2,2,2,2,2,2,4,4,4,2,2,2,2,2,2,4,4,2,2,4,4,4,4,4,2,2,4,4,4, 4,4,4};//歌曲《see you again》简谱//u8 playmusic[]={11,15,14,11,11,14,15,16,15,14,15,// 11,15,14,11,11,14,15,16,15,14,15,// 11,15,14,11,11,14,15,16,15,14,15,// 11,15,14,11,11,7,9,11,// 12,11,11,21,7,// 8,8,7,8,9,21,8,11,// 12,13,12,11,9,8,8,7,// 8,8,8,7,21,7,9,11,// 12,11,11,21,7,// 8,8,7,9,21,8,9,11,// 12,14,15,16,15,14,11,12,14,// 16,16,16,15,21,11,12,14,// 16,16,16,15,21,21,// 21,21,21// };//1是1/4拍 2是半拍,4是一拍,8是两拍 //u8 time[]={2,2,2,2,2,1,1,1,1,1,1,// 2,2,2,2,2,1,1,1,1,1,1,// 2,2,2,2,2,1,1,1,1,1,1,// 2,2,2,2,2,2,2,2,// 4,2,2,4,4,// 2,2,2,2,4,2,1,1,// 2,2,2,2,2,2,2,2,// 2,2,2,2,4,1,1,1,1,// 8,2,2,2,2,// 2,2,2,2,4,1,1,2,// 2,2,2,2,2,2,1,1,2,// 2,2,2,2,4,1,1,2,// 2,2,2,2,4,4,// 4,4,4// };u32 delaytime;u16 i,e;delaytime=10;for(i=0;i<(sizeof(playmusic)/sizeof(playmusic[0]));i++){for(e=0;e<((u16)time[i])*tone[playmusic[i]]/delaytime;e++){sound((u32)tone[playmusic[i]]);} }}
#ifndef __BEEP_H
#define __BEEP_H#include "stm32f10x.h"
#include "sys.h"void beep_init(void);
void beepon(void);
void beepoff(void);
void sound(u16 frq);
void music(void);#endif
需要编写的主函数如下:
#include "stm32f10x.h"
#include "beep.h"
#include "delay.h"
int main()
{delay_init(72);beep_init();while(1){music();delay_ms(1000);}}
接下来将程序烧进单片机即可完成使用蜂鸣器播放音乐了。
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
