未使用到用定时器、PWM,也能实现播放歌曲.....
STM32蜂鸣器模拟播放歌曲--《世间美好与你环环相扣》《生日歌》 - /**********************************************************************
- *函数名:BEEP_Init
- *功 能:蜂鸣器初始化
- *参 数:无
- *返 回:无
- *备 注:
- 引脚 点蜂鸣器电平 模式 速度
- PC3 高电平 通用推挽输出 50M
- **********************************************************************/
- void BEEP_Init(void)
- {
-
- #if 1
- RCC->APB2ENR |= 1<<4;//开启PC口时钟
- GPIOC->CRL &=~(0XF<<4*3);//清PC3
- GPIOC->CRL |=(0X3<<4*3);//通用推挽输出 50M
- GPIOC->ODR &=~(3<<1);//关蜂鸣器
- #else
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能C端口时钟
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
- GPIO_Init(GPIOC, &GPIO_InitStructure);//PC3
- GPIO_ResetBits(GPIOC,GPIO_Pin_3);//关蜂鸣器
- #endif
- }
- //控制发声
- void Sound(u16 frq)
- {
- u32 time;
- if(frq != 1000)//休止符
- {
- time = 500000/((u32)frq);
- BEEP = 1;
- delay_us(time);
- BEEP = 0;
- delay_us(time);
- }else
- delay_us(1000);
- }
- //生日歌
- void play_Music_1(void)
- {
- //音谱 低1 2 3 4 5 6 7 中1 2 3 4 5 6 7 高1 2 3 4 5 6 7 不发音
- uc16 tone[] = {262,294,330,349,392,440,494,523,587,659,698,784,880,988,1047,1175,1319,1397,1568,1760,1967,1000};//音频数据表
- //编号 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
-
- //音谱
- u8 music[]={4,4,5,4,
- 7,6,21,
- 4,4,5,4,
- 8,7,21,
- 4,4,11,9,
- 7,6,5,21,
- 3,3,9,7,
- 8,7,21,
- };
- //节拍
- u8 time[] = {2,2,4,4, //时间--2代表半拍(100ms) 4代表一拍(200ms) 8代表两拍(400ms)
- 4,4,4,
- 2,2,4,4,
- 4,4,4,
- 2,2,4,4,
- 4,4,4,4,
- 2,2,4,4,
- 4,4,4,
- };
- u32 delayShow;
- u16 i,j;
- delayShow = 10;//控制播放快慢
- for(i=0;i<sizeof(music)/sizeof(music[0]);i++)//放歌
- {
- for(j=0;j<((u16)time[i])*tone[music[i]]/delayShow;j++)
- {
- Sound((u32)tone[music[i]]);
- }
- }
- }
- //世间美好与你环环相扣
- void play_Music_2(void)
- {
- // 低1 2 3 4 5 6 7 中1 2 3 4 5 6 7 高1 2 3 4 5 6 7 不发音
- uc16 tone[] = {262,294,330,349,392,440,494,523,587,659,698,784,880,988,1047,1175,1319,1397,1568,1760,1967,1000};//音频数据表
- // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
- //音调
- u8 music[]={11,11,11,10,9,9,8,8,7,8,
- 9,9,11,11,9,8,8,9,9,
- 7,7,7,7,7,9,9,9,8,8,8,7,8,
- 9,9,9,11,8,21,
- 11,11,11,11,10,9,8,7,8,
- 9,9,11,11,9,8,8,7,7,
- 7,7,7,7,7,9,9,9,8,8,8,7,8,
- 9,11,11,21,21,9,
- 8,21,21,4,5,4,
- 9,9,12,9,10,9,9,7,8,9,8,8,7,7,
- 7,7,9,7,8,7,7,5,6,7,6,6,
- 9,9,9,4,5,4,
- 9,9,9,9,11,9,9,7,8,9,8,8,7,7,
- 7,7,9,9,9,7,5,6,6,7,6,6,
- 5,7,8,8,7,7,8,8,9,8,
- 7,7,7,21,21
- };
- //节拍
- u8 time[] = {2,2,2,1,1,2,2,2,1,1, //时间--2代表半拍(100ms) 4代表一拍(200ms) 8代表两拍(400ms)
- 1,2,1,2,1,1,2,2,4,
- 1,1,1,1,2,1,1,2,2,1,1,1,1,
- 3,1,2,2,4,4,
- 1,2,1,2,1,1,6,1,1,
- 1,2,1,2,1,1,2,2,4,
- 1,1,1,1,2,1,1,2,2,1,1,1,1,
- 2,2,4,4,2,2,
- 4,4,2,2,2,2,
- 1,1,1,1,1,1,1,1,1,2,1,2,1,1,
- 1,1,1,1,1,1,1,1,2,1,1,4,
- 3,1,6,2,2,2,
- 1,1,1,1,1,1,1,1,2,1,1,2,1,1,
- 1,1,1,1,1,1,1,1,2,1,1,4,
- 2,1,1,2,2,2,1,1,2,2,
- 2,2,4,4,4,
- };
- u32 delayShow;
- u16 i,j;
- delayShow = 7;//播放速度
- for(i=0;i<sizeof(music)/sizeof(music[0]);i++)//放歌
- {
- for(j=0;j<((u16)time[i])*tone[music[i]]/delayShow;j++)
- {
- Sound((u32)tone[music[i]]);
- }
- }
- }
复制代码 |