導航:首頁 > 編程語言 > 51音樂程序

51音樂程序

發布時間:2023-01-27 05:16:40

❶ 求51單片機蜂鳴器播放音樂的教程 C語言

這是我學校電子設計時做的,可以彈琴,也有幾首歌在裡面。。你應該看得懂#include <reg52.h> #define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit BEEP=P2^0; //喇叭輸出腳uchar th0_f; //在中斷中裝載的T0的值高8位
uchar tl0_f; //在中斷中裝載的T0的值低8位
uchar key;/*------------------------------------------------
函數聲明
------------------------------------------------*/
uchar keyscan(void);//鍵盤掃描程序
void delay(uint i); //延時子程序/*-------- T0的值,及輸出頻率對照表 --------------*/
uchar code freq[36*2]={
0xA9,0xEF, //0x3F,0XEE,//00220HZ ,1 //0
0x93,0xF0, //0X3D,0XEF,//00233HZ ,1#
0x73,0xF1, //0X30,0XF0,//00247HZ ,2
0x49,0xF2, //0X18,0XF1,//00262HZ ,2#
0x07,0xF3, //0XE6,0XF1,//00277HZ ,3
0xC8,0xF3, //0XB7,0XF2,//00294HZ ,4
0x73,0xF4, //0X71,0XF3,//00311HZ ,4#
0x1E,0xF5, //0X2A,0XF4,//00330HZ ,5
0xB6,0xF5, //0XCF,0XF4,//00349HZ ,5#
0x4C,0xF6, //0X72,0XF5,//00370HZ ,6
0xD7,0xF6, //0X09,0XF6,//00392HZ ,6#
0x5A,0xF7, //0X97,0XF6,//00415HZ ,7
0xD8,0xF7, //0X20,0XF7,//00440HZ 1 //12
0x4D,0xF8, //0X9F,0XF7,//00466HZ 1# //13
0xBD,0xF8, //0X18,0XF8,//00494HZ 2 //14
0x24,0xF9, //0X88,0XF8,//00523HZ 2# //15
0x87,0xF9, //0XF3,0XF8,//00554HZ 3 //16
0xE4,0xF9, //0X59,0XF9,//00587HZ 4 //17
0x3D,0xFA, //0X05,0XFA,//00622HZ 4# //18
0x90,0xFA, //0X13,0XFA,//00659HZ 5 //19
0xDE,0xFA, //0X66,0XFA,//00698HZ 5# //20
0x29,0xFB, //0XB9,0XFA,//00740HZ 6 //21
0x6F,0xFB, //0X05,0XFB,//00784HZ 6# //22
0xB1,0xFB, //0X4D,0XFB,//00831HZ 7 //23
0xEF,0xFB, //0X90,0XFB,//00880HZ `1
0x2A,0xFC, //0XD0,0XFB,//00932HZ `1#
0x62,0xFC, //0X0C,0XFC,//00988HZ `2
0x95,0xFC, //0X44,0XFC,//01046HZ `2#
0xC7,0xFC, //0X7B,0XFC,//01109HZ `3
0xF6,0xFC, //0XAD,0XFC,//01175HZ `4
0x22,0xFD, //0XDD,0XFC,//01244HZ `4#
0x4B,0xFD, //0X0A,0XFD,//01318HZ `5
0x73,0xFD, //0X35,0XFD,//01397HZ `5#
0x98,0xFD, //0X5D,0XFD,//01480HZ `6
0xBB,0xFD, //0X83,0XFD,//01568HZ `6#
0xDC,0xFD, //0XA6,0XFD,//01661HZ `7 //35
};//定時中斷0,用於產生唱歌頻率
timer0() interrupt 1
{
TL0=tl0_f;TH0=th0_f; //調入預定時值
BEEP=~BEEP; //取反音樂輸出IO
}//音樂符號串解釋函數
//入口:要解釋的音樂符號串,輸出的音調串,輸出的時長串
changedata(uchar *song,uchar *diao,uchar *jie)
{
uchar i,i1,j;
char gaodi; //高低+/-12音階
uchar banyin;//有沒有半個升音階
uchar yinchang;//音長
uchar code jie7[8]={0,12,14,16,17,19,21,23}; //C調的7個值
*diao=*song;
for(i=0,i1=0;;)
{
gaodi=0; //高低=0
banyin=0;//半音=0
yinchang=4;//音長1拍
if((*(song+i)=='|') || (*(song+i)==' ')) i++;
//拍子間隔和一個空格過濾
switch(*(song+i))
{
case ',': gaodi=-12;i++;//低音
break;
case '`': gaodi=12;i++; //高音
break;
}
if(*(song+i)==0) //遇到0結束
{
*(diao+i1)=0; //加入結束標志0
*(jie+i1)=0;
return;
}
j=*(song+i)-0x30; i++; //取出基準音
j=jie7[j]+gaodi; //加上高低音
yinc: switch(*(song+i))
{
case '#': //有半音j加一個音階
i++;j++;
goto yinc;
case '-': //有一個音節加長
yinchang+=4;
i++;
goto yinc;
case '_': //有一個音節縮短
yinchang/=2;
i++;
goto yinc;
case '.': //有一個加半拍
yinchang=yinchang+yinchang/2;
i++;
goto yinc;
}
*(diao+i1)=j; //記錄音符
*(jie+i1)=yinchang; //記錄音長
i1++;
}
}//奏樂函數
//入口:要演奏的音樂符號串
void play(uchar *songdata)
{
uchar i,c,j=0;
uint n;
uchar diaodata[48]; //音調緩沖
uchar jiedata[48]; //音長緩沖
changedata(songdata,diaodata,jiedata); //解釋音樂符號串
TR0=1;
for(i=0;diaodata[i]!=0;i++) //逐個符號演奏
{
tl0_f=freq[diaodata[i]*2]; //取出對應的定時值送給T0
th0_f=freq[diaodata[i]*2+1];
for(c=0;c<jiedata[i];c++) //按照音長延時
{ key = keyscan();
for(n=0;n<29500;n++); // 29500
if(key != 0xff)//((!K1)||(!K2)||(!K3)||(!K4))//發現按鍵,立即退出播放
{
TR0=0;
return;
}
}
TR0=0;
for(n=0;n<460;n++); //460音符間延時
TR0=1;
}
TR0=0;
}//一分錢
uchar code yifenqian[]={
"5`1|6_`1_5|3_5_2_3_|5-|"
"3_5_6_`1_|5_6_5_3_|1.3__|2-|"
"3_2_1_2_|3-|6_5_3_5_|6-|"
"5_`1_6_5_|3_5_2|5_2_3_2_|1-|"
}; //世上只有媽媽好
uchar code mamahao[]={
"6.5_35|`16_5_6-|35_6_53_2_|1_,6_5_3_2-|"
"2.3_55_6_|321-|5.3_2_1_,6_1_|,5--"
};
//找朋友
uchar code zhaopengyou[]={ "5_6_5_6_|5_6_5|5_`1_7_6_|5 3|"
"5_5_3_4_|5_5_3|1_4_3_2_|1_2_1|"
};
//茉莉花
uchar code molihua[]={"33_5_6_`1_`1_6_|55_6_5-|33_5_6_`1_`1_6_|55_6_5-|"
"5553_5_|665-|32_3_53_2_|11_2_1|"
//"3_2_1_3_2.3_|56_`1_5-|23_5_2_3_1_,6_|,5-,61|"
//"2.3_1_2-1_,6_|,5--"
};
//新年好
uchar code xinnianhao[]={
"1_1_1 ,5|3_3_3 1|1_3_5 5|4_3_2-|"
"2_3_4-|3_2_3 1|1_3_2 ,5|,7_2_1-|"
};
//小星星
uchar code xingxing[]={
"1155|665-|4433|221-|"
"5544|332-|5544|332-|"
"1155|665-|4433|221-|"
};
//外婆的澎湖灣
uchar code waipodephw[]={
"3_5_5_5_6_`1_6_5_|`1_`1_`1_6_5-|"
"`3_`3_`3_`3_`4_`3_`2_`1_|`2_`2_`2_`3_`2-|"
"`3_`3_`3_`3_`4_`3_`2_`1_|6_`1_`1_6_5-|"
};
}

❷ 跪求基於51單片機的匯編語言音樂盒程序

OUT BIT P2.3 //該引腳接蜂鳴器
ORG 0000H
LJMP START
ORG 000BH
INC 20H ;中斷服務,中斷計數器加1
MOV TH0,#0D8H
MOV TL0,#0EFH ;12M晶振,形成10毫秒中斷
RETI
START:
MOV SP,#50H
MOV TH0,#0D8H
MOV TL0,#0EFH
MOV TMOD,#01H
MOV IE,#82H
MUSIC0:
NOP
MOV DPTR,#DAT ;表頭地址送DPTR
MOV 20H,#00H ;中斷計數器清0
MOV B,#00H ;表序號清0
MUSIC1:
NOP
CLR A
MOVC A,@A+DPTR ;查表取代碼
JZ END0 ;是00H,則結束
CJNE A,#0FFH,MUSIC5
LJMP MUSIC3
MUSIC5:
NOP
MOV R6,A
INC DPTR
MOV A,B
MOVC A,@A+DPTR ;取節拍代碼送R7
MOV R7,A
SETB TR0 ;啟動計數
MUSIC2:
NOP
CPL OUT
MOV A,R6
MOV R3,A
LCALL DEL
MOV A,R7
CJNE A,20H,MUSIC2 ;中斷計數器(20H)=R7否?
;不等,則繼續循環
MOV 20H,#00H ;等於,則取下一代碼
INC DPTR
; INC B
LJMP MUSIC1
MUSIC3:
NOP
CLR TR0 ;休止100毫秒
MOV R2,#0DH
MUSIC4:
NOP
MOV R3,#0FFH
LCALL DEL
DJNZ R2,MUSIC4
INC DPTR
LJMP MUSIC1
END0:
NOP
MOV R2,#64H ;歌曲結束,延時1秒後繼續
MUSIC6:
MOV R3,#00H
LCALL DEL
DJNZ R2,MUSIC6
LJMP MUSIC0
DEL:
NOP
DEL3:
MOV R4,#02H
DEL4:
NOP
DJNZ R4,DEL4
NOP
DJNZ R3,DEL3
RET
NOP
DAT:
db 26h,20h,20h,20h,20h,20h,26h,10h,20h,10h,20h,80h,26h,20h,30h,20h
db 30h,20h,39h,10h,30h,10h,30h,80h,26h,20h,20h,20h,20h,20h,1ch,20h
db 20h,80h,2bh,20h,26h,20h,20h,20h,2bh,10h,26h,10h,2bh,80h,26h,20h
db 30h,20h,30h,20h,39h,10h,26h,10h,26h,60h,40h,10h,39h,10h,26h,20h
db 30h,20h,30h,20h,39h,10h,26h,10h,26h,80h,26h,20h,2bh,10h,2bh,10h
db 2bh,20h,30h,10h,39h,10h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,20h
db 20h,10h,20h,10h,2bh,10h,26h,30h,30h,80h,18h,20h,18h,20h,26h,20h
db 20h,20h,20h,40h,26h,20h,2bh,20h,30h,20h,30h,20h,1ch,20h,20h,20h
db 20h,80h,1ch,20h,1ch,20h,1ch,20h,30h,20h,30h,60h,39h,10h,30h,10h
db 20h,20h,2bh,10h,26h,10h,2bh,10h,26h,10h,26h,10h,2bh,10h,2bh,80h
db 18h,20h,18h,20h,26h,20h,20h,20h,20h,60h,26h,10h,2bh,20h,30h,20h
db 30h,20h,1ch,20h,20h,20h,20h,80h,26h,20h,30h,10h,30h,10h,30h,20h
db 39h,20h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,10h,40h,10h,20h,10h
db 20h,10h,2bh,10h,26h,30h,30h,80h,00H

END

❸ 如何編寫51單片機音樂程序

設計的相關音樂說明

要產生音頻脈沖,只要算出某一音頻的周期(1/頻率),然後將此周期除以2,即為半周期時間。利用半周期時間定時這個半周期時間,每當計時到後就將輸出的I/O反向,然後重復計時此半周期再對I/O反向,就可以在I/O腳上得到此頻率的脈沖。

記數脈沖值與頻率的關系公式如:N=Fi/2/Fr。N:記數值;Fi:內部計時依次為1us,故其頻率為1 MHZ;Fr:要產生的頻率。

其記數值的求法如:T=65536-N=65536-Fi/2/Fr。例:設K=65536,F=1000000=Fi=1 MHZ。求低音DO(26HZ),中音DO(523HZ),高音DO(1046HZ)的記數值。

每個音符使用1個音節,位元組的高四位代表音符的高低,低四位代表音符的節拍。如果1拍為0.4秒,1/4拍為0.1秒,假設1/4拍為 DELAY,則1拍為4 DELAY。

(3)51音樂程序擴展閱讀

功能特性

1,可以模擬63K程序空間,接近64K 的16位地址空間;

2,可以模擬64Kxdata 空間,全部64K 的16位地址空間;

3,可以真實模擬全部32 條IO腳;

4,完全兼容keilC51 UV2 調試環境,可以通過UV2 環境進行單步,斷點, 全速等操作;

5,可以使用C51語言或者ASM匯編語言進行調試 ;

6,可以非常方便地進行所有變數觀察,包括滑鼠取值觀察,即滑鼠放在某 變數上就會立即顯示出它此的值;

7,可選 使用用戶晶振,支持0-40MHZ晶振頻率;

8,片上帶有768位元組的xdata,您可以在模擬時選 使用他們,進行xdata 的模擬;

9,可以模擬雙DPTR 指針;

10,可以模擬去除ALE 信號輸出. ;

11,自適應300-38400bps 的所有波特率通訊;

12,體積非常細小,非常方便插入到用戶板中.插入時緊貼用戶板,沒有連接電纜,這樣可以有效地減少運行中的干擾,避免模擬時出現莫名其妙的故障;

13,模擬插針採用優質鍍金插針,可以有效地防止日久生銹,選擇優質園腳IC插座,保護模擬插針,同時不會損壞目標板上的插座. ;

14,模擬時監控和用戶代碼分離,不可能產生不能模擬的軟故障;

15,RS-232介面不計成本採用MAX202集成電路,串列通訊穩定可靠,絕非一般三極體的簡易電路可比。

❹ 使用51單片機蜂鳴器播放音樂 求keil程序

//主程序查音符碼,將不同的初值裝入定時器,定時器每中斷一次,P3.7取反一次,形成不同頻率的方波
//11代表低音哆 21代表高音哆 1代表中音哆
//12代表低音來 22代表高音來 2代表中音來,是不是翻譯樂譜很方便
#include<reg52.h>
sbit SPK = P3^7; //P3.7外接揚聲器,可以改成你所說的P1^5

#define uint unsigned int;
#define uchar unsigned char;
uint x,y;
uint i,j,k;
//以下是根據歌譜轉來的音樂代碼 3代表中音咪 1個全音被分成4個1/4 音符
//小燕子第一句 3,5,21,6,5,5,5,5, 21是高音哆 ,後面4個5,是因為這個音節佔4拍
uchar code MUSIC[]={3,5,21,6,5,5,5,5, 3,5,6,21,5,5,5,5, 21,21,21,23,22,22,21,21, 22,21,6,21,5,5,5,5, 3,3,5,6,6,5,6, 21,21,22,5,6,6,6,6, 3,3,1,1,2,2,2,2,2,
2,2,2,3,5,5,5,5, 21,21,2,3,5,5,5,5, 2,1,16,1,15,15,15,15, 0,0,0,0,16,16,1,1, 15,15,16,16,1,1,1,1, 5,5,6,6,4,4,3,3, 2,2,2,2,2,2,5,5, 5,5,2,2,4,4,3,3, 2,2,1,2,4,4,4,4,
16,16,1,1,2,2,4,4, 2,1,16,1,15,15,15,15, 16,16,16,16,15,15,15,15, 15,15,15,15,15,15,15,15, 0,0,5,5,5,5,0,0, 5,5,5,5,4,4,4,4,
5,5,5,5,2,2,2,2, 2,2,5,5,4,4 ,4,4, 16,16,1,1,2,2,2,2, 4,4,2,2,2,2,4,4, 5,5,5,5,5,5,6,6, 21,21,21,21,6,6,21,21,
21,21,6,6,5,5,5,5, 5,5,6,6,5,5,4,4, 2,2,2,2,2,2,5,5, 5,5,2,2,4,4,3,3, 2,2,1,2,4,4,4,4, 16,16,1,1,2,2,4,4,
2,1,16,1,15,15,15,15, 16,16,16,16,15,15,15,15, 5,5,6,6,5,5,6,6, 5,5,4,4,5,5,5,5, 1,1,2,2,1,1,2,2, 15,15,16,16,1,1,1,1,
5,5,6,6,5,5,6,6, 21,21,6,6,5,5,5,5, 1,1,2,2,1,1,2,2, 15,15,16,16,1,1,1,1, 5,5,6,6,5,5,4,4, 5,5,5,5,1,1,2,2, 15,15,16,16,1,1,1,1,
5,5,6,6,5,5,5,5, 1,1,2,2,1,1,1,1, 5,5,6,6,5,5,5,5, 1,1,2,2,1,1,1,1, 5,5,5,5,1,1,1,1, 5,5,5,5,1,1,1,1, 5,5,5,5,1,1,5,5,
1,1,5,5,1,1,5,5, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 0,0,1,1,1,1,0,0, 5,5,5,5,4,4,4,4, 5,5,5,5,2,2,2,2, 2,2,5,5,4,4,4,4,
6,6,1,1,2,2,2,2, 4,4,2,2,2,2,4,4, 5,5,5,5,5,5,6,6, 21,21,21,21,6,6,21,21, 21,21,6,6,5,5,5,5, 5,5,6,6,5,5,4,4, 2,2,2,2,2,2,5,5,
5,5,2,2,4,4,3,3, 2,2,1,22,4,4,4,4, 16,16,1,1,2,2,4,4, 4,4,6,6,5,5,5,5, 5,5,5,5,5,5,5,5, 6,6,6,6,5,5,5,5};

uchar code table[] = { 0,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfe,0,0,0, //定時半周期的初始值中音123 4 5 67高8位
//三個0是補位碼,為了使表格整齊,便於按規律查詢 首個0是休止符11個
0xf8,0xf9,0xfa,0xfa,0xfb,0xfb,0xfc,0,0,0, //低音1 2 3 4 5 67高8位 10個
0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xff, //高音1 2 3 4 5 67高8位 7個
0,0x44,0xad,0x0a,0x34,0x82,0xc8,0x06,0,0,0, //中音123 4 5 67低8位 10個
0x89,0x59,0x13,0x68,0x04,0x90,0x0c,0 ,0,0, //低音1 2 3 4 5 6 7低8位10個
0x22,0x56,0x85,0x9a,0xc1,0xe4,0x03}; //高音1 2 3 4 5 6 7低8位 7個

void delay(i) //晶振12MHZ時延時1ms
{
for(j=i;j>0;j--)
for(k=121;k>0;k--);
}

void TIM0_ISR() interrupt 1
{
SPK=!SPK;//定時取反,形成方波
TH0=table[x]; //重新載入定時半周期的初始值
TL0=table[x+28];//為什麼加28 11+10+7=28
}

void main(void)
{
TMOD|=0x01;
EA=1;
ET0=1;
while(1)
{
y=0;
while(y<592)//此樂譜裡面有592個音符
{
x=MUSIC[y];//查樂譜表
TH0=table[x]; //查不同音節的定時器初值
TL0=table[x+28];
TR0=1; //打開定時器
if(y>72)//兩首曲子不同節奏,延時不同
{
delay(125); //延時125ms
}
else
{
delay(250); //延時250ms
}
y++;
TR0=0;
}
}
}

❺ 跪求單片機51_C語言音樂程序,音樂是國際歌!謝謝!

/*
成都理工大學工程技術學院通信工程--漆洋
本程序用於播放音樂,採用定時器1控制時間
從而改變頻率實現音節播放
*/
#include<AT89X52.H>
#define uchar unsigned char
#define uint unsigned int
#define buz P3_1
code uint ton[][7]= //定音所有音階
{
{0xE600,0xE8D5,0xEB5E,0xEC86,0xEEA7,0xF08B,0xF23C},
{0xF2E6,0xF46D,0xF5BF,0xF61C,0xF750,0xF866,0xF905},
{0xf91E,0xf9DE,0xFA89,0xfaD8,0xfb68,0xfbe8,0xfc5a}, //中音C
{0xFC8F,0xFCEF,0xFD51,0xFD80,0xFDB5,0xFDF4,0xFE2D},
{0xFE47,0xFE77,0xFEA2,0xFEB6,0xFEDA,0xFEF3,0xFF17},
{0xFF24,0xFF3C,0xFF51,0xFF5B,0xFF6D,0xFF7D,0xFF8B},
};
/*
格式:音調+音階+拍數
*/
code uint m1[]=
{ //《最炫民族風》
262,261,251,262,261,311,312,321,311,261,0,
312,311,252,311,322,331,351,351,331,322,331,0,
361,361,361,351,331,331,312,261,261,261,332,321,0,
331,332,351,331,321,331,321,311,262,251,261,0,
262,261,251,262,261,311,312,321,311,261,0,
312,311,252,311,322,331,351,351,331,322,331,0,
361,361,361,351,331,331,312,261,261,261,332,321,0,
331,331,351,331,331,351,351,361,362,352,362,0,
262,261,252,261,312,321,331,321,311,321,332,0,
332,361,361,352,331,321,312,321,333,0,
312,261,261,321,262,331,351,331,321,312,0,
262,311,321,332,321,311,261,251,263,0,
8
};
code uint m2[]=
{ //《愛情買賣》
262,261,231,221,232,0,
222,231,221,211,222,0,
252,251,251,231,251,251,251,231,262,231,231,221,232,0,
262,261,231,221,231,0,
221,231,221,211,221,0,
251,251,251,231,251,251,251,231,271,251,251,231,251,0,
231,261,311,311,232,261,311,0,
271,261,271,261,221,231,0,
222,221,221,161,221,231,251,231,271,271,251,232,0,
232,261,311,311,231,261,311,0,
331,321,331,321,311,321,0,
331,331,321,311,321,321,311,271,251,231,251,261,263,0,
8
};
code uint m3[]=
{
//《北京愛情故事》滴答滴答
252,262,311,322,331,312,331,323,0,
322,322,321,0,321,311,311,0,262,263,0,
252,261,312,322,332,351,332,323,0,
322,321,322,321,311,312,261,331,333,0,
362,361,362,361,351,351,331,323,0,
321,321,322,321,312,311,352,351,332,0,
362,361,362,411,332,321,332,321,0,
321,321,321,321,311,261,261,311,313,0,
8
};
code uint m4[]=
{ //《同桌的你》
252,251,251,251,231,241,251,272,0,
262,261,261,261,241,262,252,0,
252,251,251,251,271,261,251,241,0,
241,241,241,241,241,231,221,212,0,
252,251,251,251,231,241,251,272,0,
262,261,261,261,241,262,252,0,
252,251,251,251,271,261,251,241,0,
241,241,241,241,241,231,221,212,0,
311,311,311,312,251,261,311,311,332,0,
322,321,321,321,311,271,262,0,
272,271,271,271,311,321,252,0,
271,271,311,321,311,271,313,0,
311,311,311,312,251,261,311,311,332,0,
322,321,321,321,311,271,262,0,
272,271,271,271,311,321,252,0,
271,271,311,321,311,271,313,0,
8
};
code uint m5[]=
{ //《荷塘月色》
211,212,161,152,161,211,211,221,233,0,
221,222,211,221,221,252,251,231,231,221,233,0,
211,212,161,152,252,231,221,231,221,212,0,
222,221,211,221,221,232,221,211,161,221,212,0,
211,212,161,152,161,211,211,221,233,0,
221,222,211,221,221,252,251,231,231,221,233,0,
211,212,161,152,252,231,221,231,221,212,0,
222,221,211,221,221,232,221,211,161,221,212,0,
231,252,251,252,251,261,251,231,221,212,0,
261,311,261,251,231,221,211,162,221,221,232,221,0,
231,252,251,252,251,261,251,231,221,212,0,
162,211,161,151,222,231,212,0,
8
};
code uint m6[]= //《豬八戒背媳婦》
{
262,331,351,332,261,312,0,
261,311,261,311,332,331,321,332,311,262,0,
332,351,361,361,361,331,352,0,
332,351,332,351,361,361,362,331,352,0,
352,261,352,261,331,331,312,0,
321,0,321,0,321,311,321,332,351,361,432,331,432,0,
331,431,331,431,331,331,313,0,
321,0,321,0,322,311,321,331,351,364,0,
8
};
void musicdelay(int q)
{
while(--q);
}
/*音樂播放程序*/
void musicplay(uint *a)
{
uint b,i,j,k,l,m,p;
uchar KEY=0;
TMOD=0x11;
p=0;
buz=1;
while(1) //如果其它鍵,停止播放音樂
{
if(a[p]==8)break; //判斷休止符,結束播放
if(a[p]!=8) //音頻識別與轉換
{
i=a[p]/100;
j=a[p]%100/10;
l=a[p]%10;
if(a[p]!=0){b=ton[i][j-1];}; //轉換後存入變數b中
if(a[p]==0){b=0;};
p++;
}

k=(230414/(0xffff-b))*l; //拍數
/******************播放當前音節*******************/
if(b!=0) //判斷為正常音符,播放
{
TH1=0;TL1=0;TR1=1;
for(m=0;m<k;m++)
{
TH1=((b&0xff00)>>8); //裝定時器初值
TL1=(b&0x00ff);
while(TF1==0);
buz=~buz; //實現一次蜂鳴器震動
TF1=0;
}
TR1=0;
buz=1;
}
/************************************************/
if(b==0) //判斷為0(停頓),延時一節拍
{
buz=1;
for(m=0;m<80;m++)
{
musicdelay(300);};
}
}
}
void musicplayc(uchar i)
{
if(i==1){musicplay(m1);};
if(i==2){musicplay(m2);};
if(i==3){musicplay(m3);};
if(i==4){musicplay(m4);};
if(i==5){musicplay(m5);};
if(i==6){musicplay(m6);};
}

❻ 51單片機寫音樂的一個程序,求詳解,以下是程序。

SONG_TONE[i]/3
產生一個短延時,延時的時間正好是音符的頻率

延時時間越短產生的頻率就越高,發出的聲音也越高

SONG_LONG[i]*20
是用於音符的持續時間。

就相當於長按跟短按一個按鍵一樣

❼ 51單片機蜂鳴器程序,音樂的隨時跳轉

這里修改:
sbitkey1=P1^0;
sbitkey2=P1^1;
voidPlay_Song(unsignedchari)
{
unsignedcharTemp1,Temp2;
unsignedintAddr;
Count=0;//中斷計數器清0
Addr=i*217;
while(1)
{
if(key2==0)break;//插入這一句
Temp1=SONG[Addr++];
if(Temp1==0xFF)//休止符
{
TR0=0;
Delay_xMs(100);
}
elseif(Temp1==0x00)//歌曲結束符
{
return;
}
else
{
Temp2=SONG[Addr++];
TR0=1;
while(1)
{
_Speak=~_Speak;
Delay_xMs(Temp1);
if(Temp2==Count)
{
Count=0;
break;
}
}
}
}
}
PlaySong2(................
{
....
whlie(1)
{
if(key1==0)break;//插入這一句
.....
}
}

voidmain()
{
Time0_Init();//定時器0中斷初始化
while(1)
{
Play_Song(0);//播放1
Play_Song2(0);//播放2
}
}

❽ 求一個用C語言編寫的51單片機音樂程序代碼。

#include <reg52.h>
sbit speaker=P2^2;
unsigned char timer0h,timer0l,time;
//生日歌
code unsigned char sszymmh[]={5,1,1, 5,1,1, 6,1,2, 5,1,2, 1,2,2, 7,1,4,
5,1,1, 5,1,1, 6,1,2, 5,1,2, 2,2,2, 1,2,4,
5,1,1, 5,1,1, 5,2,2, 3,2,2, 1,2,2, 7,1,2, 6,1,2,
4,2,1, 4,2,1, 3,2,2, 1,2,2, 2,2,2, 1,2,4};
// 音階頻率表 高八位
code unsigned char FREQH[]={
0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,
0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i
0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,
0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,
} ;
// 音階頻率表 低八位
code unsigned char FREQL[]={
0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,
0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i
0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,
0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,
};
void delay(unsigned char t)
{
unsigned char t1;
unsigned long t2;
for(t1=0;t1<t;t1++)
{
for(t2=0;t2<8000;t2++)
{
;
}
}
TR0=0;
}
void t0int() interrupt 1
{
TR0=0;
speaker=!speaker;
TH0=timer0h;
TL0=timer0l;
TR0=1;
}
void song()
{
TH0=timer0h;
TL0=timer0l;
TR0=1;
delay(time);
}

void main(void)
{
unsigned char k,i;
TMOD=1; //置CT0定時工作方式1
EA=1;
ET0=1;//IE=0x82 //CPU開中斷,CT0開中斷
while(1)
{
i=0;
while(i<75){ //音樂數組長度 ,唱完從頭再來
k=sszymmh[i]+7*sszymmh[i+1]-1;
timer0h=FREQH[k];
timer0l=FREQL[k];
time=sszymmh[i+2];
i=i+3;
song();
}
}
}

❾ 51單片機控制喇叭的程序(c語言)

/************************************************************************
[文件名] C51音樂程序(八月桂花)
[功能] 通過單片機演奏音樂
注意:通過了74HC14控制ULN2003 驅動晶元驅動蜂鳴器 *
/**********************************************************************/
#include <REG52.H>
#include <INTRINS.H>
//本例採用89C52, 晶振為11.0592MHZ
//關於如何編制音樂代碼, 其實十分簡單,各位可以看以下代碼.
//頻率常數即音樂術語中的音調,而節拍常數即音樂術語中的多少拍;
//所以拿出譜子, 試探編吧!

sbit Beep = P1^5 ;

unsigned char n=0; //n為節拍常數變數
unsigned char code music_tab[] ={
0x18, 0x30, 0x1C , 0x10, //格式為: 頻率常數, 節拍常數, 頻率常數, 節拍常數,
0x20, 0x40, 0x1C , 0x10,
0x18, 0x10, 0x20 , 0x10,
0x1C, 0x10, 0x18 , 0x40,
0x1C, 0x20, 0x20 , 0x20,
0x1C, 0x20, 0x18 , 0x20,
0x20, 0x80, 0xFF , 0x20,
0x30, 0x1C, 0x10 , 0x18,
0x20, 0x15, 0x20 , 0x1C,
0x20, 0x20, 0x20 , 0x26,
0x40, 0x20, 0x20 , 0x2B,
0x20, 0x26, 0x20 , 0x20,
0x20, 0x30, 0x80 , 0xFF,
0x20, 0x20, 0x1C , 0x10,
0x18, 0x10, 0x20 , 0x20,
0x26, 0x20, 0x2B , 0x20,
0x30, 0x20, 0x2B , 0x40,
0x20, 0x20, 0x1C , 0x10,
0x18, 0x10, 0x20 , 0x20,
0x26, 0x20, 0x2B , 0x20,
0x30, 0x20, 0x2B , 0x40,
0x20, 0x30, 0x1C , 0x10,
0x18, 0x20, 0x15 , 0x20,
0x1C, 0x20, 0x20 , 0x20,
0x26, 0x40, 0x20 , 0x20,
0x2B, 0x20, 0x26 , 0x20,
0x20, 0x20, 0x30 , 0x80,
0x20, 0x30, 0x1C , 0x10,
0x20, 0x10, 0x1C , 0x10,
0x20, 0x20, 0x26 , 0x20,
0x2B, 0x20, 0x30 , 0x20,
0x2B, 0x40, 0x20 , 0x15,
0x1F, 0x05, 0x20 , 0x10,
0x1C, 0x10, 0x20 , 0x20,
0x26, 0x20, 0x2B , 0x20,
0x30, 0x20, 0x2B , 0x40,
0x20, 0x30, 0x1C , 0x10,
0x18, 0x20, 0x15 , 0x20,
0x1C, 0x20, 0x20 , 0x20,
0x26, 0x40, 0x20 , 0x20,
0x2B, 0x20, 0x26 , 0x20,
0x20, 0x20, 0x30 , 0x30,
0x20, 0x30, 0x1C , 0x10,
0x18, 0x40, 0x1C , 0x20,
0x20, 0x20, 0x26 , 0x40,
0x13, 0x60, 0x18 , 0x20,
0x15, 0x40, 0x13 , 0x40,
0x18, 0x80, 0x00
};

void int0() interrupt 1 //採用中斷0 控制節拍
{ TH0=0xd8;
TL0=0xef;
n--;
}

void delay (unsigned char m) //控制頻率延時
{
unsigned i=3*m;
while(--i);
}

void delayms(unsigned char a) //豪秒延時子程序
{
while(--a); //採用while(--a) 不要採用while(a--); 各位可編譯一下看看匯編結果就知道了!
}

void main()
{ unsigned char p,m; //m為頻率常數變數
unsigned char i=0;
TMOD&=0x0f;
TMOD|=0x01;
TH0=0xd8;TL0=0xef;
IE=0x82;
play:
while(1)
{
a: p=music_tab[i];
if(p==0x00) { i=0, delayms(1000); goto play;} //如果碰到結束符,延時1秒,回到開始再來一遍
else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a;} //若碰到休止符,延時100ms,繼續取下一音符
else {m=music_tab[i++], n=music_tab[i++];} //取頻率常數 和 節拍常數
TR0=1; //開定時器1
while(n!=0) Beep=~Beep,delay(m); //等待節拍完成, 通過P1口輸出音頻(可多聲道哦!)
TR0=0; //關定時器1
}
}

❿ 51單片機音樂程序為什麼Beep=~Beep

因為聲波是震動的,以頻率來達到不同的聲律,只有透過反向的指令才能做到類似的東西,只是還要有間隔跟響應時間的長短來模擬聲波

閱讀全文

與51音樂程序相關的資料

熱點內容
linux文件只讀不能復制 瀏覽:597
開關代碼 瀏覽:91
word繪圖板 瀏覽:359
蘋果minecraftpe注冊 瀏覽:775
ps怎麼存儲文件卡 瀏覽:728
微信清除緩存圖片恢復 瀏覽:305
安卓app會閃退怎麼解決 瀏覽:429
哪些app背單詞是免費的 瀏覽:889
cf擊殺圖標放哪個文件夾 瀏覽:208
為什麼我的爐石不能查看完整數據 瀏覽:179
蘋果7音量按鍵不回彈 瀏覽:296
u盤兩個系統可以放一個文件夾嗎 瀏覽:538
linuxshell變數累加 瀏覽:386
win10控制面板怎麼看 瀏覽:574
如何編程換裝游戲程序 瀏覽:269
怎麼登錄沭陽縣民政局網站 瀏覽:451
iphone6降級ios7 瀏覽:92
怎麼隱藏三星應用程序圖標不見了 瀏覽:203
可以兼職的app 瀏覽:493
iphone圓角圖標製作 瀏覽:659

友情鏈接