找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 10311|回復(fù): 6
打印 上一主題 下一主題
收起左側(cè)

單片機(jī)按鍵控制蜂鳴器演奏音樂

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:143558 發(fā)表于 2016-11-20 21:03 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
按鍵控制蜂鳴器演奏音樂

下載:
單片機(jī)演奏音樂.rar (44.26 KB, 下載次數(shù): 94)
  1. #include<reg52.h>
  2. sbit spek=P3^7;  //聲音輸出端  
  3. sbit set=P3^2;  //設(shè)定及退出鍵,按奇數(shù)次為設(shè)定功能;偶數(shù)次為退出功能
  4. sbit up=P3^1;  //上一曲
  5. sbit down=P3^0;  //下一曲  
  6. bit flag1,flag2;  //按鍵掃描標(biāo)志位,新鍵值標(biāo)志位  
  7. unsigned char f_t,n,i,key,k_max; //音符頻率對(duì)應(yīng)的二分之一周期值,節(jié)拍常數(shù)值,查表用計(jì)數(shù)器,鍵值,最大鍵值
  8. unsigned char *p_music;  //指向音樂數(shù)據(jù)數(shù)組的指針
  9. void timer_ms(unsigned char tim);  //毫秒級(jí)延時(shí)函數(shù)聲明
  10. void timer_us(unsigned char t);  //微秒級(jí)延時(shí)函數(shù)
  11. void key_scan(void);  //按鍵掃描函數(shù)
  12. void key_manage();  //鍵值處理函數(shù)
  13. //格式為:頻率常數(shù),節(jié)拍常數(shù),頻率常數(shù),節(jié)拍常數(shù)  
  14. unsigned char code music_table1[]={
  15.         0x18,0x30,0x1c,0x10,0x20,0x40,0x1c,0x10,0x18,  
  16.         0x10,0x20,0x10,0x1c,0x10,0x18,0x40,0x1c,0x20,
  17.                 0x20,0x20,0x1c,0x20,0x18,0x20,0x20,0x80,0xff,
  18.                 0x20,0x30,0x1c,0x10,0x18,0x20,0x15,0x20,0x1c,
  19.                 0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2b,0x20,
  20.                 0x26,0x20,0x20,0x20,0x30,0x80,0xff,0x20,0x20,
  21.                 0x1c,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2b,
  22.                 0x20,0x30,0x20,0x2b,0x40,0x20,0x20,0x1c,0x10,
  23.                 0x18,0x10,0x20,0x20,0x26,0x20,0x2b,0x20,0x30,
  24.                 0x20,0x2b,0x40,0x20,0x30,0x1c,0x10,0x18,0x20,
  25.                 0x15,0x20,0x1c,0x20,0x20,0x20,0x26,0x40,0x20,
  26.                 0x20,0x2b,0x20,0x26,0x20,0x20,0x20,0x30,0x80,
  27.                 0x20,0x30,0x1c,0x10,0x20,0x10,0x1c,0x10,0x20,
  28.                 0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,
  29.                 0x20,0x15,0x1f,0x05,0x20,0x10,0x1c,0x10,0x20,
  30.                 0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,
  31.                 0x20,0x30,0x1c,0x10,0x18,0x20,0x15,0x20,0x1c,
  32.                 0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2b,0x20,
  33.                 0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1c,
  34.                 0x10,0x18,0x40,0x1c,0x20,0x20,0x20,0x26,0x40,
  35.                 0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,
  36.                 0x80,0x00}; //八月桂花香  
  37. unsigned char code music_table2[]={
  38.         0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,
  39.                 0x10,0x20,0x80,0x26,0x20,0x30,0x20,0x30,0x20,
  40.                 0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,
  41.                 0x20,0x20,0x20,0x1c,0x20,0x20,0x80,0x2b,0x20,
  42.                 0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,
  43.                 0x80,0x26,0x20,0x30,0x20,0x30,0x20,0x39,0x10,
  44.                 0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,
  45.                 0x20,0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,
  46.                 0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,0x2b,
  47.                 0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,
  48.                 0x2b,0x20,0x2b,0x40,0x40,0x20,0x20,0x10,0x20,
  49.                 0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,
  50.                 0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x40,0x26,
  51.                 0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,
  52.                 0x20,0x20,0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,
  53.                 0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
  54.                 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,
  55.                 0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,0x18,0x20,
  56.                 0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,
  57.                 0x10,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,
  58.                 0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,
  59.                 0x10,0x30,0x20,0x39,0x20,0x26,0x10,0x2b,0x10,
  60.                 0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,
  61.                 0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,
  62.                 0x00};  //祝你平安  
  63. unsigned char code music_table3[]={
  64.         0x20,40,0x26,20,0x20,20,0x17,80,0x1c,40,0x17,40,
  65.                 0x20,80,0x20, 40,0x2f,20,0x2a,20,0x26,40,0x2a,20,
  66.                 0x2f,20,0x2a,80,0xff,0x20,40,0x26,20,0x20,20,0x17,60,
  67.                 0x19,20, 0x1c,40,0x17,40,0x20,80,0x20,40,0x2a,20,
  68.                 0x26,20,0x24,60,0x32,20,0x2f,80,0xff,0x1c,40,0x17,40,
  69.                 0x17, 80,0x19,40,0x1c,20,0x19,20,0x17,80,0x1c,20,
  70.                 0x19,20,0x17,20,0x1c,20,0x1c,20,0x20,20,0x26,20,0x2f,20,
  71.                 0x2a,80,0xff,0x20,40,0x26,20,0x20,20,0x17,60,0x19,20,
  72.                 0x1c,40,0x17,40,0x20,80,0x20,40,0x2a,20,0x26, 20,
  73.                 0x24,60,0x32,20,0x2f,80,0xff,0x20,40,0x26,20,0x20,20,
  74.                 0x17,80,0x1c,40,0x17,40,0x20,80,0x20,40, 0x2f,20,0x2a,20,
  75.                 0x26,40,0x2a,20,0x2f,20,0x2a,80,0xff,0x20,40,0x26,20,
  76.                 0x20,20,0x17,60,0x19,20,0x1c, 40,0x17,40,0x20,80,0x20,40,
  77.                 0x2a,20,0x26,20,0x24,60,0x32,20,0x2f,80,0};  //送別
  78. main()
  79. {
  80.      spek=0;     
  81.          P3=0x7f;      
  82.          TMOD=0x01;  //T0工作于方式1,即16位定時(shí)器模式     
  83.          IE=0x82;  //開總中斷,T0中斷     
  84.          flag2=0;  //     
  85.          flag1=0;     
  86.          key=1;        
  87.          k_max=3;  //最大鍵值(取值范圍1~254)     
  88.          n=0;     
  89.          i=0;
  90.          p_music=music_table1;  //指針賦初值     
  91.          while (1)     
  92.          {
  93.            switch (p_music[i])           
  94.                  {     
  95.                    case 0x00: i=0;  //歌曲完,則停止0.5S后重新開始            
  96.                    timer_ms(250);  //延時(shí)0.25ms            
  97.                    timer_ms(250);            
  98.                    break;         
  99.                    case 0xff: i++;  //遇到休止符則停止100us            
  100.                    timer_ms(250);   
  101.                    timer_ms(250);            
  102.                    break;         
  103.                 default:   f_t=p_music[i++];            
  104.                 n=p_music[i++];            
  105.                 spek=1;            
  106.                 TR0=1;            
  107.                 while (n!=0)            
  108.                 {              
  109.                     spek=~spek;                  
  110.                         timer_us(4*f_t);  //改變f_t前面的系數(shù)可以調(diào)整音符頻率的高低
  111.                                     }              
  112.                        TR0=0;            
  113.                            spek=0;   
  114.                            if(set==0)   
  115.                            {     
  116.                            key_scan();     
  117.                            key_manage();   
  118.                            }         
  119.                          }     
  120.         }
  121. }   
  122. /**************************************************************
  123. 函數(shù)名稱: 定時(shí)器0的中斷處理程序 函數(shù)功能: 完成10毫秒定時(shí) 入口參數(shù):  
  124. 出口參數(shù):  其      它:   **************************************************************/
  125. void timer0() interrupt 1 using 2
  126. {  
  127.      TH0=(65536-10000)/256;  //定時(shí)10毫秒     
  128.          TL0=(65536-10000)%256;     
  129.          n--; }  
  130. /**************************************************************
  131. 函數(shù)名稱: 延時(shí)程序  函數(shù)功能: 完成微秒級(jí)的延時(shí)  入口參數(shù):
  132. t為需延時(shí)的時(shí)間(取值范圍1~255;延時(shí)時(shí)間5~1275us) 出口參數(shù):  
  133. 其      它:   **************************************************************/
  134. void timer_us(unsigned char t)
  135. {
  136.      while (--t);  //本語句執(zhí)行一次需5us的時(shí)間
  137.          }  
  138. /**************************************************************
  139. 函數(shù)名稱: 按鍵掃描函數(shù) 函數(shù)功能: 完成按鍵掃描處理  入口參數(shù):
  140. 應(yīng)為在中斷函數(shù)中被調(diào)用,所以不能有參數(shù)傳遞 出口參數(shù):  其      它:   
  141. **************************************************************/
  142. void key_scan(void)
  143. {
  144.       timer_ms(10);  //延時(shí)10ms等待按鍵狀態(tài)穩(wěn)定
  145.      if (set==0)  //再次確認(rèn)     
  146.          {      
  147.                while (set==0);  //等待按鍵松開         
  148.                    flag1=1;  //該位為1,表示進(jìn)入按鍵掃描處理;為0,表示本次按鍵掃描處理完成         
  149.                    while (flag1)         
  150.                    {              
  151.                       if (up==0)  //加一鍵            
  152.                            {              
  153.                                timer_ms(10);               
  154.         if (up==0)                 
  155.          {                     
  156.                 while (!up);                     
  157.                         key++;                     
  158.                         if (key>k_max) key=1;                 
  159.                         }            
  160.                         }              
  161.           if (down==0)  //減一鍵            
  162.           {                 
  163.                  timer_ms(10);                    
  164.                          if (down==0)                 
  165.                          {                  
  166.                             while (!down);                     
  167.                                 key--;                     
  168.                                 if (key<1) key=k_max;              
  169.                                    }            
  170.                  }   
  171.                  if (set==0)  //退出鍵            
  172.                  {               
  173.                     timer_ms(10);                 
  174.                         if (set==0)                 
  175.                         {                     
  176.                              while (set==0);                     
  177.                                  flag1=0;  //表示本次按鍵掃描處理完成                     
  178.                                  flag2=1;  //該位置1,表示有新鍵值要處理                 
  179.                                  }            
  180.                  }//以上鍵值處理的范圍為1到254個(gè)鍵值         
  181.         }      
  182. }
  183. }  
  184. /**************************************************************
  185. 函數(shù)名稱: 鍵值處理函數(shù)  函數(shù)功能: 完成按鍵鍵值的處理  
  186. 入口參數(shù): 應(yīng)為在中斷函數(shù)中被調(diào)用,所以不能有參數(shù)傳遞
  187. 出口參數(shù):  其      它:   
  188. **************************************************************/
  189. void key_manage()
  190. {
  191.      flag2=0;     
  192.          switch (key)     
  193.          {   
  194.             case 1: p_music=music_table1;  //鍵值為1 播放第一首樂曲            
  195.                 goto kh1;      
  196.                 case 2: p_music=&music_table2[0];  //鍵值為2 播放第2首樂曲            
  197.                 goto kh1;  
  198.                 case 3: p_music=&music_table3[0];  //鍵值為3 播放第3首樂曲            
  199.                 goto kh1;      
  200.                 default:  key=1;               
  201.                 p_music=music_table1;
  202.                 kh1: TH0=(65536-10000)/256;      
  203.                 TL0=(65536-10000)%256;     
  204.                  break;     
  205.                  }
  206.                   n=0;     
  207.                   i=0;
  208. }   
  209. /*----------------------------------------------------------------------------------------------------
  210. 函數(shù)名稱: timer_ms  函數(shù)說明: 1ms延時(shí)子程序  入口參數(shù): tim為接收主調(diào)傳來需延時(shí)的毫秒數(shù),取值范圍1~256,定時(shí)時(shí)間1~256ms
  211. 出口參數(shù): 其    它:  ----------------------------------------------------------------------------------------------------*/   
  212. void timer_ms(unsigned char tim)
  213. {  
  214.         unsigned char j;   
  215.                 while(tim--)   
  216.                 {      
  217.                      for(j=0;j<200;j++);   //延時(shí)1ms   
  218.                          }
  219.         }
復(fù)制代碼


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂1 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:148964 發(fā)表于 2016-11-20 22:25 | 只看該作者
不錯(cuò)。。。。。。。。。。。。。。
回復(fù)

使用道具 舉報(bào)

板凳
ID:190307 發(fā)表于 2017-4-18 18:02 | 只看該作者
按鍵不管用啊
回復(fù)

使用道具 舉報(bào)

地板
ID:170476 發(fā)表于 2017-4-19 20:22 | 只看該作者
按鍵怎么接啊
回復(fù)

使用道具 舉報(bào)

5#
ID:60515 發(fā)表于 2017-4-19 20:26 來自手機(jī) | 只看該作者
請(qǐng)問是接有源蜂鳴器還是嗽叭?
回復(fù)

使用道具 舉報(bào)

6#
ID:204652 發(fā)表于 2017-5-25 11:30 | 只看該作者
按鍵怎么不管用啊
回復(fù)

使用道具 舉報(bào)

7#
ID:219500 發(fā)表于 2017-7-25 10:23 | 只看該作者
不錯(cuò),學(xué)習(xí)學(xué)習(xí)
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表