51單片機(jī)之簡(jiǎn)單的音樂(lè)播放器
/*************************************************************
音樂(lè)播放器:
按鍵0-9對(duì)應(yīng)最多10首樂(lè)曲,當(dāng)一首樂(lè)曲播放完畢后,
響應(yīng)按鍵的動(dòng)作去播放相應(yīng)的音樂(lè),另外,右、下這兩個(gè)按鍵
對(duì)應(yīng)著下一首,左上這兩個(gè)按鍵對(duì)應(yīng)著上一首。
作者:寧?kù)o致遠(yuǎn)
*************************************************************/
#include
#include
#defineRldTmr(fr)65536-(11059200/12)/((fr)<<1)
#defineFuDian(n)((n)<<1)/3//附點(diǎn)n分音符的換算
typedefunsignedcharuchar;
typedefunsignedintuint;
typedefunsignedlongulong;
typedefstructmusic{
char*pNote;
char*pDur;
}Music;
sbitBUZZ=P1^6;
sbitKEY_OUT_3=P2^0;
sbitKEY_OUT_2=P2^1;
sbitKEY_OUT_1=P2^2;
sbitKEY_OUT_0=P2^3;
sbitKEY_IN_0=P2^4;
sbitKEY_IN_1=P2^5;
sbitKEY_IN_2=P2^6;
sbitKEY_IN_3=P2^7;
uintcodenoteFreq[]={//中音1-7和高音1-7對(duì)應(yīng)的頻率列表低音
523,587,659,698,784,880,988,
1047,1175,1319,1397,1568,1760,1976,
//261,293,329,349,392,440,494
};
uintcodetmrRld[]={//中音1-7和高音1-7對(duì)應(yīng)的定時(shí)器重載值
RldTmr(523),RldTmr(587),RldTmr(659),RldTmr(698),RldTmr(784),RldTmr(880),RldTmr(988),
RldTmr(1047),RldTmr(1175),RldTmr(1319),RldTmr(1397),RldTmr(1568),RldTmr(1760),RldTmr(1976),
//RldTmr(261),RldTmr(293),RldTmr(329),RldTmr(349),RldTmr(392),RldTmr(440),RldTmr(494)
};
ucharcodekeyCodeMap[4][4]={//矩陣按鍵編號(hào)到標(biāo)準(zhǔn)鍵盤(pán)鍵碼的映射表
{0x31,0x32,0x33,0x26},//數(shù)字鍵1、數(shù)字鍵2、數(shù)字鍵3、向上鍵
{0x34,0x35,0x36,0x25},//數(shù)字鍵4、數(shù)字鍵5、數(shù)字鍵6、向左鍵
{0x37,0x38,0x39,0x28},//數(shù)字鍵7、數(shù)字鍵8、數(shù)字鍵9、向下鍵
{0x30,0x1B,0x0D,0x27}//數(shù)字鍵0、ESC鍵、回車(chē)鍵、向右鍵
};
ucharkeyState[4][4]={//全部矩陣按鍵的當(dāng)前狀態(tài)
{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
};
bitenable=1,tmrFlg=0;
ucharT0RH=0XFF,T0RL=0X00;
ucharT1RH,T1RL;
ucharmusicIdx=0;
ucharmusicSpeed=72;
ucharnowMusic=0;
//bitenKey=1;
ucharcodetwoTigerNote[]={//音名
1,2,3,1,
1,2,3,1,
3,4,5,
3,4,5,
5,6,5,4,3,1,
5,6,5,4,3,1,
1,5,1,
1,5,1,
'