找回密码
 注册

QQ登录

只需一步,快速开始

搜索

STM32蜂鸣器模拟播放歌曲--《世间美好与你环环相扣》《生日歌》

[复制链接]
路漫漫 发表于 2020-6-7 15:21:55 | 显示全部楼层 |阅读模式
未使用到用定时器、PWM,也能实现播放歌曲.....
STM32蜂鸣器模拟播放歌曲--《世间美好与你环环相扣》《生日歌》
  1. /**********************************************************************
  2. *函数名:BEEP_Init
  3. *功  能:蜂鸣器初始化
  4. *参  数:无
  5. *返  回:无
  6. *备  注:
  7. 引脚          点蜂鸣器电平                模式                                                速度
  8. PC3                        高电平                                        通用推挽输出                50M
  9. **********************************************************************/
  10. void BEEP_Init(void)
  11. {  
  12.       
  13.    #if 1
  14.    RCC->APB2ENR |= 1<<4;//开启PC口时钟
  15.    GPIOC->CRL &=~(0XF<<4*3);//清PC3
  16.    GPIOC->CRL |=(0X3<<4*3);//通用推挽输出 50M
  17.    GPIOC->ODR &=~(3<<1);//关蜂鸣器      
  18.    #else
  19.     GPIO_InitTypeDef  GPIO_InitStructure;
  20.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能C端口时钟
  21.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;         
  22.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通用推挽输出
  23.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
  24.     GPIO_Init(GPIOC, &GPIO_InitStructure);//PC3
  25.     GPIO_ResetBits(GPIOC,GPIO_Pin_3);//关蜂鸣器      
  26.    #endif
  27. }

  28. //控制发声
  29. void Sound(u16 frq)
  30. {
  31.         u32 time;
  32.         if(frq != 1000)//休止符
  33.         {
  34.                 time = 500000/((u32)frq);
  35.                 BEEP = 1;
  36.                 delay_us(time);
  37.                 BEEP = 0;
  38.                 delay_us(time);
  39.         }else
  40.                 delay_us(1000);
  41. }

  42. //生日歌
  43. void play_Music_1(void)
  44. {
  45.         //音谱         低1  2   3   4   5   6   7  中1  2   3   4   5   6   7  高1    2   3     4    5    6    7  不发音
  46.         uc16 tone[] = {262,294,330,349,392,440,494,523,587,659,698,784,880,988,1047,1175,1319,1397,1568,1760,1967,1000};//音频数据表
  47.         //编号          0   1   2   3   4   5   6   7   8   9   10  11  12  13  14   15   16    17   18   19   20  21
  48.       
  49.         //音谱
  50.         u8 music[]={4,4,5,4,
  51.                                                         7,6,21,
  52.                                                         4,4,5,4,
  53.                                                   8,7,21,
  54.                                                         4,4,11,9,
  55.                                                         7,6,5,21,
  56.                                                         3,3,9,7,
  57.                                                         8,7,21,
  58.                                                         };
  59.         //节拍
  60.         u8 time[] = {2,2,4,4,        //时间--2代表半拍(100ms) 4代表一拍(200ms) 8代表两拍(400ms)
  61.                                                          4,4,4,
  62.                                                          2,2,4,4,
  63.                                                          4,4,4,
  64.                                                          2,2,4,4,
  65.                                                          4,4,4,4,
  66.                                                          2,2,4,4,
  67.                                                          4,4,4,
  68.                };      
  69.         u32 delayShow;
  70.         u16 i,j;
  71.         delayShow = 10;//控制播放快慢
  72.         for(i=0;i<sizeof(music)/sizeof(music[0]);i++)//放歌
  73.         {
  74.                 for(j=0;j<((u16)time[i])*tone[music[i]]/delayShow;j++)
  75.                 {
  76.                         Sound((u32)tone[music[i]]);
  77.                 }      
  78.         }
  79. }

  80. //世间美好与你环环相扣
  81. void play_Music_2(void)
  82. {
  83.         //             低1  2   3   4   5   6   7  中1  2   3   4   5   6   7  高1    2   3     4    5    6    7  不发音
  84.         uc16 tone[] = {262,294,330,349,392,440,494,523,587,659,698,784,880,988,1047,1175,1319,1397,1568,1760,1967,1000};//音频数据表
  85.         //              0   1   2   3   4   5   6   7   8   9   10  11  12  13  14   15   16    17   18   19   20  21
  86.         //音调
  87.         u8 music[]={11,11,11,10,9,9,8,8,7,8,
  88.                                                         9,9,11,11,9,8,8,9,9,
  89.                                                         7,7,7,7,7,9,9,9,8,8,8,7,8,
  90.                                                         9,9,9,11,8,21,
  91.                                                         11,11,11,11,10,9,8,7,8,
  92.                                                         9,9,11,11,9,8,8,7,7,
  93.                                                         7,7,7,7,7,9,9,9,8,8,8,7,8,
  94.                                                         9,11,11,21,21,9,
  95.                                                         8,21,21,4,5,4,
  96.                                                         9,9,12,9,10,9,9,7,8,9,8,8,7,7,
  97.                                                         7,7,9,7,8,7,7,5,6,7,6,6,
  98.                                                         9,9,9,4,5,4,
  99.                                                         9,9,9,9,11,9,9,7,8,9,8,8,7,7,
  100.                                                         7,7,9,9,9,7,5,6,6,7,6,6,
  101.                                                         5,7,8,8,7,7,8,8,9,8,
  102.                                                         7,7,7,21,21                       
  103.                                                         };      
  104.         //节拍
  105.         u8 time[] = {2,2,2,1,1,2,2,2,1,1,        //时间--2代表半拍(100ms) 4代表一拍(200ms) 8代表两拍(400ms)
  106.                                                          1,2,1,2,1,1,2,2,4,
  107.                                                         1,1,1,1,2,1,1,2,2,1,1,1,1,
  108.                                                         3,1,2,2,4,4,
  109.                                                         1,2,1,2,1,1,6,1,1,
  110.                                                         1,2,1,2,1,1,2,2,4,
  111.                                                         1,1,1,1,2,1,1,2,2,1,1,1,1,
  112.                                                         2,2,4,4,2,2,
  113.                                                         4,4,2,2,2,2,
  114.                                                         1,1,1,1,1,1,1,1,1,2,1,2,1,1,
  115.                                                         1,1,1,1,1,1,1,1,2,1,1,4,
  116.                                                         3,1,6,2,2,2,
  117.                                                         1,1,1,1,1,1,1,1,2,1,1,2,1,1,
  118.                                                         1,1,1,1,1,1,1,1,2,1,1,4,
  119.                                                         2,1,1,2,2,2,1,1,2,2,
  120.                                                         2,2,4,4,4,                       
  121.                };      
  122.         u32 delayShow;
  123.         u16 i,j;
  124.         delayShow = 7;//播放速度
  125.         for(i=0;i<sizeof(music)/sizeof(music[0]);i++)//放歌
  126.         {
  127.                 for(j=0;j<((u16)time[i])*tone[music[i]]/delayShow;j++)
  128.                 {
  129.                         Sound((u32)tone[music[i]]);
  130.                 }      
  131.         }
  132. }
复制代码



您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-3-29 02:19

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表