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

QQ登錄

只需一步,快速開始

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

51單片機(jī)播放音樂代碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
51單片機(jī)實(shí)現(xiàn)音樂播放




#include<reg52.h>
#include<intrins.h>

#define uchar unsigned char
#define uint unsigned int

sbit SPK = P2^7;

uchar tone_h;                //高音
uchar tone_l;                //低音
uint note = 1;                //音符
uchar beat;                        //節(jié)拍


uchar code chuzhi[3][16]={  //音調(diào)對(duì)應(yīng)的計(jì)數(shù)初值
         0xff,0xff,//占0位
         0xf8,0x8c,//低1  
         0xf9,0x5b,//  2                 
         0xfa,0x15,//  3                 
         0xfa,0x67,//  4                 
         0xfb,0x04,//  5                 
         0xfb,0x90,//  6                 
         0xfc,0x0c,//低7

         0xff,0xff,//占0位
         0xfc,0x44,//中1   
         0xfc,0xac,//  2
         0xfd,0x09,//  3
         0xfd,0x34,//  4
         0xfd,0x82,//  5
         0xfd,0xc8,//  6
         0xfe,0x06,//中7   

         0xff,0xff,//占0位
         0xfe,0x22,//高1                           
         0xfe,0x56,//        2               
         0xfe,0x85,//        3               
         0xfe,0x9a,//        4               
         0xfe,0xc1,//        5               
         0xfe,0xe4,//        6               
         0xff,0x03 //高7                             
        };        

//****** 測試曲譜 ****** 個(gè)位:音調(diào) ,十位:低中高音 對(duì)應(yīng) 0 1 2,千百位:節(jié)拍 1~16
uint code test2[] = {        120, //曲速 暫時(shí)未用到
        401,402,403,404,405,406,407,
        611,612,613,614,615,616,617,
        821,822,823,824,825,826,827,
        0xffff        //0xffff表示結(jié)尾
};

//****** 生日快樂,蟲兒飛 ******
uint code test[] = { 120,
       413,213,213,414,415,813,812,
                411,211,211,412,413,313,107,807,//加點(diǎn)40X,只有加橫為11X,兩個(gè)連起來加橫21X.
                406,413,812,406,413,812,406,413,312,811,410,
                213,412,815,214,413,812,
                215,414,213,412,315,113,812,
                406,413,812,
                406,413,812,
                214,213,214,213,811,
                214,213,214,213,811,612,211,411,410,
        0xffff        
};


void delayms(uint ms)          /*ms 等于1時(shí),延時(shí)1ms 等于2時(shí),延時(shí)2ms,以此類推*/
{        
        uchar a,b,c;                                   //+3
        while(ms--)
    {
         for(c=1;c>0;c--)                        //         1+2 = 3
        for(b=142;b>0;b--)                  //        1*( 1+142*2)) = 285        
            for(a=2;a>0;a--);        //         142*(1+2*2) = 710
        }
}

void delay125ms(uint pai)  //延時(shí) ?*125ms 即?個(gè)節(jié)拍
{
    uchar a,b,c;
        while(pai--)
        {
        for(c=239;c>0;c--)
                for(b=104;b>0;b--)
                        for(a=1;a>0;a--);
        }
}
void timer0() interrupt 1
{
        TH0 = tone_h;
        TL0 = tone_l;
        SPK = ~SPK;
}


void timer_init()
{
        EA = 1;
        ET0 = 1;
        PT0 = 1;
        TMOD = 0x11;
        TH0 = 255;
        TL0 = 255;
}
void main()
{        
        timer_init();
        note = 1;

        while(1)
        {        
                if(test[note] == 0xffff)
                {        
                        note = 1;
                }               
                if(test[note]%10 != 0)        //音調(diào)不為0 【音調(diào)為0時(shí)表示不發(fā)聲,但有節(jié)拍】
                {
                        tone_h = TH0 = chuzhi[test[note]%100/10][(test[note]%10)*2 ];                //音調(diào)高位 【二維數(shù)組 第1維表示低中高音,第二維表示音調(diào)】
                        tone_l = TL0 = chuzhi[test[note]%100/10][(test[note]%10)*2 + 1];        //音調(diào)低位
                        TR0 = 1;
                }
                delay125ms(test[note]/100);        //節(jié)拍
                TR0 = 0;
                delayms(30);
                note++;                                                //下一個(gè)音符

        }

}

//      411,411,415,415,416,416,815,
//                414,414,413,413,412,412,811,
//                415,415,414,414,413,413,812,
//                415,415,414,414,413,413,812,
//                411,411,415,415,416,416,815,
//                411,411,415,415,416,416,815,
//                414,414,413,413,412,412,811,410,//一閃一閃亮晶晶

//生日歌//
//      205,205,406,405,411,807,
//        205,205,406,405,412,811,
//        205,205,415,413,411,407,406,
//        314,114,413,411,413,812,
//        305,105,406,405,411,807,
//        305,105,406,405,412,811,
//        305,105,415,413,411,407,406,
//                  314,114,413,411,412,811,410,

//歡樂頌//
//        413,413,414,415,415,414,413,412,411,411,412,413,313,412,812,
//        413,413,414,415,415,414,413,412,411,411,412,413,312,411,811,
//        412,412,413,411,412,213,214,213,211,412,213,214,213,212,411,412,415,413,413,413,414,415,
//                  215,214,213,214,412,411,411,412,413,312,411,811,410,

//大海//
//205,206,111,411,111,411,206,205,111,411,112,411,206,211,112,412,112,412,211,206,112,412,113,412,213,215,
//116,415,115,416,215,213,212,213,212,211,406,205,206,111,411,111,411,406,811,410,
//213,215,116,416,116,416,211,216,115,415,116,415,213,212,111,411,111,411,412,813,410,
//213,212,111,411,111,411,211,216,115,415,116,415,213,215,316,211,411,216,215,815,410,
//213,215,116,416,116,416,211,216,115,415,116,415,213,212,111,411,111,411,415,813,410,
//213,212,111,411,111,411,212,213,115,415,113,415,213,215,806,410,210,206,113,412,111,411,410,
//205,206,111,411,111,411,206,205,111,411,112,411,206,211,112,412,112,412,211,206,112,412,113,412,213,215,
//116,415,115,416,215,213,212,213,212,211,406,205,206,111,411,112,411,212,211,411,
//213,215,116,416,116,416,211,216,115,415,116,415,213,212,111,411,111,411,412,813,410,
//213,212,111,411,111,411,211,216,115,415,116,415,213,215,316,211,411,216,215,815,410,
//213,215,116,416,116,416,211,216,115,415,116,415,213,212,111,411,111,411,415,813,410,
//213,212,111,411,111,411,212,213,115,415,113,415,213,215,806,410,210,206,113,412,111,411,410,
//大海舒緩版本//
//205,206,211,411,211,411,206,205,211,411,212,411,206,211,212,412,212,412,211,206,212,412,213,412,213,215,
//216,415,215,416,215,213,212,213,212,211,406,205,206,211,411,211,411,406,811,410,
//213,215,216,416,216,416,211,216,215,415,216,415,213,212,211,411,211,411,412,813,410,
//213,212,211,411,211,411,211,216,215,415,216,415,213,215,316,211,411,216,215,815,410,
//213,215,216,416,216,416,211,216,215,415,216,415,213,212,211,411,211,411,415,813,410,
//213,212,211,411,211,411,212,213,215,415,213,415,213,215,806,410,210,206,213,412,211,411,410,
//205,206,211,411,211,411,206,205,211,411,212,411,206,211,212,412,212,412,211,206,212,412,213,412,213,215,
//216,415,215,416,215,213,212,213,212,211,406,205,206,211,411,212,411,212,211,411,
//213,215,216,416,216,416,211,216,215,415,216,415,213,212,211,411,211,411,412,813,410,
//213,212,211,411,211,411,211,216,215,415,216,415,213,215,316,211,411,216,215,815,410,
//213,215,216,416,216,416,211,216,215,415,216,415,213,212,211,411,211,411,415,813,410,
//213,212,211,411,211,411,212,213,215,415,213,415,213,215,806,410,210,206,213,412,211,411,410,


評(píng)分

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

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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