你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】stm32单片机 代码实现歌曲——红尘情歌

[复制链接]
STMCU小助手 发布时间:2022-5-26 21:01
mian.c

  1. #include "sys.h"        
  2. #include "delay.h"        
  3. #include "led.h"
  4. #include "beep.h"
  5. u8 music[]={
  6.         5,10,10,5,5,9,9,16,8,8,8,9,10,5,5,16, //???????,???????
  7.         6,8,8,6,5,10,10,16,9,8,8,6,9,16, //???????,?????
  8.         5,10,10,5,5,9,9,16,8,8,8,6,5,10,10,16, //???????,???????
  9.         6,11,11,6,5,10,10,16,9,8,8,6,8,16, //???????,?????
  10.         5,12,5,5,12,5,9,16,8,6,8,8,8,10,12,16, //???????,???????!
  11.         8,6,8,8,8,13,12,10,9,8,6,8,8,10,9,16, //??????,?????,?????
  12.         5,12,5,5,12,5,9,16,8,6,8,8,13,12,16, //???????,??????
  13.         8,8,8,13,12,10,9,8,6,8,8,9,8,16, //?????????,??????
  14. };
  15. u8 time[] = {
  16.         4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, //???????,???????
  17.         4,4,4,4,4,4,4,4,4,4,4,4,8,4, //???????,?????
  18.         4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, //???????,???????
  19.         4,4,4,4,4,4,4,4,4,4,4,4,8,4, //???????,?????
  20.         4,4,2,2,4,4,4,4,4,4,2,2,4,4,8,4, //???????,???????!
  21.         4,4,2,2,4,4,4,4,4,4,4,4,4,4,8,4, //??????,?????,?????
  22.         4,4,2,2,4,4,4,4,4,4,4,4,4,8,4, //???????,??????
  23.         4,4,4,4,4,4,4,4,4,4,4,4,8,4, //?????????,??????
  24. };
  25. void Sound(u16 frq)
  26. {   
  27.     u32 n;
  28.     BEEP = 0;
  29.     if(frq != 1000)
  30.     {
  31.         n = 500000/((u32)frq);
  32.         BEEP = 0;
  33.         delay_us(n);
  34.         BEEP = 1;
  35.         delay_us(n);
  36.     }
  37.     else
  38.     {
  39.             delay_us(1000);
  40.     }         
  41. }
  42. void play(void)
  43. {
  44.          uc16 tone[] = {247,262,294,330,349,392,440,494,523,587,659,698,784,1000};//?????
  45. u8 music[]={      
  46.         5,5,6,8,7,6,5,6,13,13,//??
  47.         5,5,6,8,7,6,5,3,13,13,
  48.         2,2,3,5,3,5,6,3,2,1,
  49.         6,6,5,6,5,3,6,5,13,13,
  50.         5,5,6,8,7,6,5,6,13,13,
  51.         5,5,6,8,7,6,5,3,13,13,
  52.         2,2,3,5,3,5,6,3,2,1,
  53.         6,6,5,6,5,3,6,1,   
  54.         13,8,9,10,10,9,8,10,9,8,6,
  55.         13,6,8,9,9,8,6,9,8,6,5,
  56.         13,2,3,5,5,3,5,5,6,8,7,6,
  57.         6,10,9,9,8,6,5,6,8
  58. };     
  59. u8 time[] = {      
  60.         2,4,2,2,2,2,2,8,4, 4, //??
  61.         2,4,2,2,2,2,2,8,4, 4,
  62.         2,4,2,4,2,2,4,2,2,8,
  63.         2,4,2,2,2,2,2,8,4 ,4,
  64.         2,4,2,2,2,2,2,8,4, 4,
  65.         2,4,2,2,2,2,2,8,4, 4,
  66.         2,4,2,4,2,2,4,2,2,8,
  67.         2,4,2,2,2,2,2,8,
  68.         4, 2,2,2, 4, 2,2,2, 2,2,8,
  69.         4, 2,2,2,4,2,2,2,2,2,8,
  70.         4, 2,2,2,4,2,2,5,2,6,2,4,
  71.         2,2 ,2,4,2,4,2,2,12
  72. };     
  73.     u32 yanshi;
  74.     u16 i,e;
  75.     yanshi = 10;
  76.     for(i=0;i<sizeof(music)/sizeof(music[0]);i++)
  77.     {
  78.          for(e=0;e<((u16)time<i>)*tone[music<i>]/yanshi;e++)
  79.          {
  80.                  Sound((u32)tone[music<i>]);
  81.          }      
  82.     }
  83. }
  84. int main(void)
  85. {
  86.         delay_init();                     
  87.         LED_Init();                          
  88.         BEEP_Init();                 
  89.         BEEP = 0;
  90.         play();
  91.     while(1);                        
  92. }</i></i></i>
复制代码

beep.c

  1. #include "beep.h"
  2. void BEEP_Init(void)
  3. {
  4.         GPIO_InitTypeDef  GPIO_InitStructure;
  5.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);        
  6.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;                                 
  7.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                 
  8.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         
  9.         GPIO_Init(GPIOB, &GPIO_InitStructure);         
  10.         GPIO_ResetBits(GPIOB,GPIO_Pin_8);
  11. }
复制代码

beep.h

  1. #ifndef __BEEP_H
  2. #define __BEEP_H         
  3. #include "sys.h"
  4. #define BEEP PBout(8)
  5. void BEEP_Init(void);                           
  6. #endif
复制代码



收藏 评论0 发布时间:2022-5-26 21:01

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版