導航:首頁 > 操作系統 > 51單片機生日歌代碼

51單片機生日歌代碼

發布時間:2023-04-16 21:19:11

㈠ 如何用C語言編程51單片機控制 喇叭 發出生日歌,如何將生日歌編為代碼數組,是什麼原理呢,求解!

#include<reg52.h>

#defineucharunsignedchar

#defineuintunsignedint

sbitBEEP=P3^7;

ucharcodeSONG_TONE[]=

{

212,212,190,212,159,169,212,212,190,212,142,159,212,212,106,126,129,169,190,119,119,126,159,142,159,0

};

ucharcodeSONG_LONG[]=

{

9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0

};

voidDelayMS(uintms)

{

uchart;

while(ms--)

{

for(t=0;t<120;t++);

}

}

voidPlayMusic()

{

uinti=0,j,k;

while(SONG_LONG[i]!=0||SONG_TONE[i]!=0)

{

for(j=0;j<SONG_LONG[i]*20;j++)

{

BEEP=~BEEP;

for(k=0;k<SONG_TONE[i]/3;k++);

}

DelayMS(10);

i++;

}

}

voidmain()

{

while(1)

{

PlayMusic();

DelayMS(500);

}

}

㈡ C51單片機設計一個7音階的電子琴程序,如何彈奏生日歌怎麼按那7個鍵

每個音階是一個子程序,按鍵是調用子程序

㈢ 誰能給我幾首51單片機流行歌曲 的源代碼!

<!--[if !supportLists]-->2. <!--[endif]-->程序代碼:(以下代碼奏出八月桂花香這首曲子) ORG 0000H LJMP START ORG 000BH INC 20H ;中斷服務,中斷計數器加1 MOV TH0, #0DBH MOV TL0, #0FFH ;11.0592M晶振,形成10毫秒中斷 RETISTART: MOV SP, #50H MOV TH0, #0DBH MOV TL0, #0FFH MOV TMOD, #01H MOV IE, #82HMUSIC0: NOP MOV DPTR, #DAT ;表頭地址送DPTR MOV 20H, #00H ;中斷計數器清0MUSIC1: NOP CLR A ;A清零 MOVC A, @A+DPTR ;查表取代碼 JZ END0 ;是00H,則結束 CJNE A, #0FFH, MUSIC5 ;如果不是休止符,往下執行,;以R6作為音符頻率控制,唱R7節拍那麼久。 LJMP MUSIC3MUSIC5: NOP MOV R6, A ;R6=18H音符的頻率 INC DPTR ;DPTR加1 MOV A, #0 MOVC A, @A+DPTR ;取節拍代碼送R7 MOV R7,A ;R7=30H音符發音的時間 SETB TR0 ;啟動計數MUSIC2: NOP CPL P3.2 ;P3.2是音樂輸出引腳 MOV A, R6 MOV R3, A ;R3=R6=18H LCALL DEL MOV A, R7 CJNE A, 20H, MUSIC2 ;中斷計數器(20H)=R7否?;不等,則繼續循環 MOV 20H, #00H ;等於,則取下一代碼 INC DPTR LJMP MUSIC1MUSIC3: NOP ;休止100毫秒 CLR TR0 MOV R2, #0DH ;R2=13MUSIC4: NOP MOV R3, #0FFH ;R3=255 LCALL DEL DJNZ R2, MUSIC4 INC DPTR LJMP MUSIC1END0: NOP MOV R2, #0FFH ;歌曲結束,延時1秒後繼續MUSIC6: MOV R3, #00H LCALL DEL DJNZ R2, MUSIC6 LJMP MUSIC0DEL: NOPDEL3: MOV R4, #02H DEL4: NOP DJNZ R4, DEL4 NOP DJNZ R3, DEL3 RET NOPDAT:DB 18H, 30H, 1CH, 10H, 20H, 40H, 1CH, 10HDB 18H, 10H, 20H, 10H, 1CH, 10H, 18H, 40HDB 1CH, 20H, 20H, 20H, 1CH, 20H, 18H, 20HDB 20H, 80H, 0FFH, 20H,30H, 1CH, 10H, 18HDB 20H, 15H, 20H, 1CH, 20H, 20H, 20H, 26HDB 40H, 20H, 20H, 2BH, 20H, 26H, 20H, 20HDB 20H, 30H, 80H, 0FFH,20H, 20H, 1CH, 10HDB 18H, 10H, 20H, 20H, 26H, 20H, 2BH, 20HDB 30H, 20H, 2BH, 40H, 20H, 20H, 1CH, 10HDB 18H, 10H, 20H, 20H, 26H, 20H, 2BH, 20HDB 30H, 20H, 2BH, 40H, 20H, 30H, 1CH, 10HDB 18H, 20H, 15H, 20H, 1CH, 20H, 20H, 20HDB 26H, 40H, 20H, 20H, 2BH, 20H, 26H, 20HDB 20H, 20H, 30H, 80H, 20H, 30H, 1CH, 10HDB 20H, 10H, 1CH, 10H, 20H, 20H, 26H, 20HDB 2BH, 20H, 30H, 20H, 2BH, 40H, 20H, 15HDB 1FH, 05H, 20H, 10H, 1CH, 10H, 20H, 20HDB 26H, 20H, 2BH, 20H, 30H, 20H, 2BH, 40HDB 20H, 30H, 1CH, 10H, 18H, 20H, 15H, 20HDB 1CH, 20H, 20H, 20H, 26H, 40H, 20H, 20HDB 2BH, 20H, 26H, 20H, 20H, 20H, 30H, 30HDB 20H, 30H, 1CH, 10H, 18H, 40H, 1CH, 20HDB 20H, 20H, 26H, 40H, 13H, 60H, 18H, 20HDB 15H, 40H, 13H, 40H, 18H, 80H, 00HEND

㈣ 怎麼把歌曲用單片機C語言程序進行播放,急急急急~~~~~~

#include <reg51.h> //包括一個51標准內核的頭文件
#define uchar unsigned char //定義一下方便使用
#define uint unsigned int

sbit BEEP=P3^6; //喇叭輸出腳
uchar th0_f; //在中斷中裝載的T0的值高8位
uchar tl0_f; //在中斷中裝載的T0的值低8位

/***************** 延時 1ms *******************/
void delay_1ms(unsigned int t)
{
unsigned char i,j;
for(t;t>0;t--)
{
for(i=34;i>0;i--)
for(j=13;j>0;j--);
}
}

//T0的值,及輸出頻率對照表
static code freq[36][2] = {
0xA9,0xEF, //00220HZ ,1 //0
0x93,0xF0, //00233HZ ,1# //1
0x73,0xF1, //00247HZ ,2 //2
0x49,0xF2, //00262HZ ,2# //3
0x07,0xF3, //00277HZ ,3 //4
0xC8,0xF3, //00294HZ ,4 //5
0x73,0xF4, //00311HZ ,4# //6
0x1E,0xF5, //00330HZ ,5 //7
0xB6,0xF5, //00349HZ ,5# //8
0x4C,0xF6, //00370HZ ,6 //9
0xD7,0xF6, //00392HZ ,6# //10
0x5A,0xF7, //00415HZ ,7 //11
0xD8,0xF7, //00440HZ 1 //12
0x4D,0xF8, //00466HZ 1# //13
0xBD,0xF8, //00494HZ 2 //14
0x24,0xF9, //00523HZ 2# //15
0x87,0xF9, //00554HZ 3 //16
0xE4,0xF9, //00587HZ 4 //17
0x3D,0xFA, //00622HZ 4# //18
0x90,0xFA, //00659HZ 5 //19
0xDE,0xFA, //00698HZ 5# //20
0x29,0xFB, //00740HZ 6 //21
0x6F,0xFB, //00784HZ 6# //22
0xB1,0xFB, //00831HZ 7 //23
0xEF,0xFB, //00880HZ `1 //24
0x2A,0xFC, //00932HZ `1# //25
0x62,0xFC, //00988HZ `2 //26
0x95,0xFC, //01046HZ `2# //27
0xC7,0xFC, //01109HZ `3 //28
0xF6,0xFC, //01175HZ `4 //29
0x22,0xFD, //01244HZ `4# //30
0x4B,0xFD, //01318HZ `5 //31
0x73,0xFD, //01397HZ `5# //32
0x98,0xFD, //01480HZ `6 //33
0xBB,0xFD, //01568HZ `6# //34
0xDC,0xFD, //01661HZ `7 //35
};

static code music1[464] = { // 《隱形的翅膀》
36,36,36,36,36,36,36,36,36,36,36,36,8,8,12,12, //每一
16,16,16,16,16,16,19,19,16,16,16,16,14,14,12,12, //次都在徘徊
12,12,12,12,12,12,9,9,8,8,8,36,9,9,12,12, //孤單中堅強 每一
16,16,16,16,16,16,19,19,19,19,19,19,21,21,19,19, //次就算很受傷
19,19,16,16,14,14,12,12,14,14,14,36,21,21,19,19, //也不閃淚光 我知
16,16,16,16,16,16,19,19,19,19,19,19,21,21,19,19, //道我一直有雙
16,16,14,14,12,12,14,14,9,9,9,36,8,8,9,9, //隱形的翅膀 帶我
12,12,12,12,12,12,16,16,14,14,14,14,16,14,14,14, //飛 飛過絕
12,12,12,12,12,12,12,12,12,12,12,36,8,8,12,12, //望 不去
16,16,16,16,16,16,19,19,16,16,16,16,14,14,12,12, //想他們擁有
12,12,12,12,12,12,9,9,8,8,8,36,8,8,12,12, //美麗的太陽 我看
16,16,16,16,16,16,19,19,19,19,19,19,21,21,19,19, //見每天的夕陽
19,19,16,16,14,14,12,12,14,14,14,36,21,21,19,19, //也會有變化 我知
16,16,16,16,16,16,19,19,19,19,19,19,21,21,19,19, //道我一直有雙
16,16,14,14,12,12,14,14,9,9,9,36,8,8,9,9, //隱形的翅膀 帶我
12,12,12,12,12,12,14,16,14,14,14,14,16,16,12,12, //飛 給我希
12,12,12,12,12,12,12,12,12,12,12,36,16,16,19,19, //望 我終
24,24,24,24,24,36,24,24,24,24,24,24,21,21,19,19, //於看到所有
21,21,24,24,16,16,14,14,12,12,12,36,12,12,12,12, //夢想都開花 追逐
12,12,24,24,24,24,21,21,19,19,16,16,14,14,12,12, //的年輕歌聲多嘹
14,14,14,14,14,14,14,14,14,14,14,36,16,16,19,19, //亮 我終
24,24,24,24,24,36,24,24,24,24,24,24,21,21,19,19, //於翱翔用心
21,21,24,24,16,16,14,14,12,12,12,36,12,12,12,12, //凝望不害怕 哪裡
12,12,24,24,24,24,21,21,19,19,16,16,14,14,12,12, //會有風就飛多遠
12,12,12,12,12,12,12,12,12,12,36,36,16,16,19,19, //吧 隱形
24,24,24,24,24,36,24,24,23,23,23,23,21,21,19,19, //的翅膀讓夢
21,21,24,24,16,16,14,14,12,12,12,36,12,12,12,12, //恆久比天長 留一
12,12,24,24,24,24,21,21,19,19,16,16,14,14,14,14, //個願望讓自己
14,14,14,14,12,12,12,12,12,12,12,12,12,12,12,36, //想像... ...
};

static code music2[336] = { // 《北京歡迎你》
16,16,19,19,16,16,14,14,16,16,14,14,16,16,16,36, //迎接另一個晨曦
16,16,14,14,9,9,12,12,16,16,14,14,14,14,14,36, //帶來全新空氣
14,14,12,12,9,9,12,12,14,14,16,16,19,19,14,14, //氣息改變情味不變
16,16,21,21,19,19,9,9,14,14,12,12,12,12,12,36, //茶香飄滿情誼
16,16,19,19,16,16,14,14,16,16,14,14,16,16,16,36, //我家大門常打開
16,16,14,14,9,9,12,12,16,16,14,14,14,14,14,36, //開放懷抱等你
14,14,12,12,9,9,12,12,14,14,16,16,19,19,14,14, //擁抱過就有了默契
16,16,21,21,19,19,9,9,14,14,12,12,12,12,12,36, //你會愛上這里
14,14,12,12,9,9,12,12,14,14,16,16,19,19,14,14, //不管遠近都是客人
16,16,21,21,19,19,19,19,16,16,16,16,16,16,16,36, //請不用客氣
14,14,16,16,14,14,12,12,19,19,21,21,14,14,14,36, //相約好了在一起
9,9,16,16,14,14,14,14,12,12,12,12,12,12,12,36, //我們歡迎你
36,36,36,36,36,36,36,36,16,16,16,16,19,19,19,19, //北京
24,24,19,19,21,21,21,21,36,36,21,21,19,19,16,16, //歡迎你 為你開
16,16,19,19,19,19,19,19,19,19,19,36,16,16,19,19, //天闢地 流動
21,21,24,24,26,26,24,24,19,19,16,16,14,14,19,19, //中的魅力充滿著朝
16,16,16,16,16,16,16,16,16,16,16,36,16,16,19,19, //氣 北京
24,24,19,19,21,21,21,21,36,36,24,24,26,26,24,24, //歡迎你 在太陽
19,19,16,16,19,19,23,23,21,21,21,21,36,36,16,16, //下分享呼吸 在
14,14,16,16,19,19,28,28,26,26,26,26,26,26,23,23, //黃土地刷新成
24,24,24,24,24,24,24,24,24,24,24,24,24,24,36,36, //績
};

static code music3[444] = { // 《仙劍奇俠傳》
28,28,26,26,28,28,28,28,28,28,21,21,
26,26,26,26,24,24,23,23,21,21,21,23,21,21,19,19,
28,28,28,28,16,16,19,19,24,24,24,24,16,16,19,19,
26,26,26,26,24,24,23,23,21,21,21,21,21,21,21,21,
21,21,21,21,28,28,26,26,36,36,36,36,28,28,26,26,
21,21,21,21,24,24,26,26,28,28,28,28,28,28,28,28,
28,28,31,31,28,28,24,24,26,26,26,26,26,26,26,26,
21,21,21,21,24,24,28,28,26,26,26,26,24,24,24,24,
21,21,21,21,21,21,21,21,21,21,21,21,36,36,36,36,
28,28,26,26,28,28,28,28,28,28,21,21,
26,26,26,26,24,24,23,23,21,21,21,23,21,21,19,19,
28,28,28,28,16,16,19,19,24,24,24,24,16,16,19,19,
26,26,26,26,28,24,23,23,21,21,21,21,21,21,21,21,
21,21,21,21,28,28,26,26,36,36,36,36,28,28,26,26,
28,28,28,28,28,28,21,21,
26,26,26,26,24,24,23,23,21,21,21,23,21,21,19,19,
16,16,16,16,16,16,19,19,24,24,24,24,16,16,19,19,
26,26,26,26,28,24,23,23,21,21,21,21,21,21,21,21,
21,21,21,21,23,23,23,23,23,23,23,23,23,23,23,23,
21,21,21,21,24,24,26,26,28,28,28,28,28,28,28,28,
31,33,31,31,28,28,24,24,26,26,26,26,26,26,26,26,
21,21,21,21,24,24,28,28,26,26,26,26,24,24,24,24,
21,21,21,21,21,21,21,21,21,21,21,21,36,36,36,36,
28,28,26,26,28,28,28,28,28,28,21,21,
26,26,26,26,24,24,23,23,21,21,21,23,21,21,19,19,
28,28,28,28,16,16,19,19,24,24,24,24,16,16,19,19,
26,26,26,26,28,24,23,23,21,21,21,21,21,21,21,21,
23,23,23,23,23,23,23,23,23,23,23,23,21,21,19,19,
21,21,21,21,21,21,21,21,36,36,36,36,36,36,36,36,
};

static code music4[448] = { // 《笑傲江湖》
16,16,16,16,21,21,21,16,19,19,19,19,19,19,16,16,
14,14,16,16,19,19,21,21,16,16,16,16,16,16,16,36,
16,16,16,16,21,21,21,16,19,19,19,19,19,19,16,16,
14,14,16,16,19,19,21,19,16,16,16,16,36,16,16,14,
12,12,12,12,14,14,16,16,14,14,14,14,14,14,14,12,
9,9,9,12,14,14,16,16,19,19,19,21,19,19,16,14,
12,12,12,14,14,14,16,16,14,14,14,14,14,12,12,12,
9,9,9,12,14,16,16,19,21,21,21,21,21,21,21,36,
16,16,16,16,21,21,21,16,19,19,19,19,19,19,16,16,
14,14,16,16,19,19,21,19,16,16,16,16,16,16,16,36,
16,16,16,16,21,21,21,16,19,19,19,19,19,19,16,16,
14,14,14,16,19,19,21,19,16,16,16,16,36,16,16,14,
12,12,12,12,14,14,16,16,14,14,14,14,14,14,14,12,
9,9,9,12,14,14,16,16,19,19,19,21,19,19,16,14,
12,12,12,14,14,14,16,16,14,14,14,14,14,12,12,12,
9,9,9,12,14,16,16,19,21,21,21,21,21,21,21,36,
14,14,14,14,14,16,16,14,12,12,12,12,12,9,9,12,
14,14,14,14,14,16,16,14,12,12,9,9,36,12,9,12,
14,14,14,14,14,16,16,14,12,12,12,12,36,12,9,12,
14,14,14,14,14,19,19,16,16,16,16,16,16,16,16,36,
9,9,9,9,36,9,9,9,14,14,14,14,14,14,16,14,
12,12,12,12,36,12,12,14,9,9,9,9,9,9,7,7,
9,9,9,9,36,9,7,7,4,4,4,4,36,4,7,7,
2,2,2,2,36,2,2,2,4,4,4,4,4,4,4,36,
9,9,9,9,9,9,9,9,14,14,14,14,14,14,16,16,
12,12,12,12,12,12,14,14,9,9,9,9,12,9,
7,7,7,7,7,7,9,9,4,4,4,2,4,4,7,7,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
};

//定時中斷0,用於產生唱歌頻率
timer0() interrupt 1
{
TL0=tl0_f;TH0=th0_f; //調入預定時值
BEEP=~BEEP; //取反音樂輸出IO
P2=~P2;
}

//音階聲音自動輸出試驗
void main(void) // 主程序
{
uint i;
TMOD = 0x01; //使用定時器0的16位工作模式
TR0 = 1;
ET0 = 1;
EA = 1;
while(1)
{
for(i=0;i<336;i++) //播放音樂
{
if(music2[i] == 36) //空音
{
TR0 = 0;
delay_1ms(190);
TR0 = 1;
}
else
{
tl0_f=freq[music2[i]][0]; //置一個音符的值
th0_f=freq[music2[i]][1];
delay_1ms(190);
}
}
TR0 = 0;
delay_1ms(3000);
TR0 = 1;
}
}

㈤ 關於C51單片機通過sound播放音樂的問題

SONG_TONE是培州每個音符的頻率,212就是頻率;SONG_LONG是頻率的長度,這個配桐蔽不是用輪敗中斷完成的,是軟體延時做的

㈥ 求: 用51單片機c語言操作使蜂鳴器奏出「祝你生日快樂」音樂的全部程序!

#include <REGX51.H>
unsigned char num1=0;
unsigned char num2=0;
//sbit P34 = P3^4; //定義用來軟體復位

//**************************************************************************

#define SYSTEM_OSC 12000000//11059200// //定義晶振頻率12000000HZ
#define SOUND_SPACE 4/5 //定義普通音符演奏的長度分率,//每4分音符間隔
//sbit BeepIO = P3^4; //定義輸出管腳
sbit BeepIO = P1^5; //定義輸出管腳

unsigned int code FreTab[12] = { 262,277,294,311,330,349,369,392,415,440,466,494 }; //原始頻率表
unsigned char code SignTab[7] = { 0,2,4,5,7,9,11 }; //1~7在頻率表中的位置
unsigned char code LengthTab[7]= { 1,2,4,8,16,32,64 };
unsigned char code led[8]= { 0xff,0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01 };
unsigned char Sound_Temp_TH0,Sound_Temp_TL0; //音符定時器初值暫存
unsigned char Sound_Temp_TH1,Sound_Temp_TL1; //音長定時器初值暫存
//**************************************************************************

//生日快樂
unsigned char code Music_birth[]={ 0x0F,0x03, 0x0F,0x03, 0x10,0x02, 0x0F,0x02, 0x15,0x02,
0x11,0x01, 0x0F,0x02, 0x0F,0x02, 0x10,0x02, 0x0F,0x02,
0x16,0x02, 0x15,0x01, 0x0F,0x03, 0x0F,0x03, 0x19,0x02,
0x17,0x02, 0x15,0x02, 0x11,0x0C, 0x10,0x02, 0x18,0x03,
0x18,0x03, 0x17,0x02, 0x15,0x02, 0x16,0x02, 0x15,0x0B,
0x00,0x00 };

void InitialSound(void)
{
BeepIO = 1;
Sound_Temp_TH1 = (65535-(1/1200)*SYSTEM_OSC)/256; // 計算TL1應裝入的初值 (10ms的初裝值)
Sound_Temp_TL1 = (65535-(1/1200)*SYSTEM_OSC)%256; // 計算TH1應裝入的初值
TH1 = Sound_Temp_TH1;
TL1 = Sound_Temp_TL1;
TMOD |= 0x11; //兩個定時器都工作在16位計數/計時器模式
ET0 = 1;
ET1 = 0;
TR0 = 0;
TR1 = 0;
EA = 1;
}

void BeepTimer0(void) interrupt 1 using 1 //音符發生中斷
{
BeepIO = !BeepIO;
TH0 = Sound_Temp_TH0;
TL0 = Sound_Temp_TL0;
}
//**************************************************************************
void Play(unsigned char *Sound,unsigned char Signature,unsigned Octachord,unsigned int Speed)
{
unsigned int NewFreTab[12]; //新的頻率表
unsigned char i,j,k,l;
unsigned int Point,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength;
unsigned char Tone,Length,SL,SH,SM,SLen,XG,FD;
for(i=0;i<12;i++) // 根據調號及升降八度來生成新的頻率表
{
j = i + Signature;
if(j > 11)
{
j = j-12;
NewFreTab[i] = FreTab[j]*2;
}
else
NewFreTab[i] = FreTab[j];

if(Octachord == 1)
NewFreTab[i]>>=2;
else if(Octachord == 3)
NewFreTab[i]<<=2;
}

SoundLength = 0;
while(Sound[SoundLength] != 0x00) //計算歌曲長度
{
SoundLength+=2;
}

Point = 0;
Tone = Sound[Point];
Length = Sound[Point+1]; // 讀出第一個音符和它時時值

LDiv0 = 12000/Speed; // 算出1分音符的長度(幾個10ms)
LDiv4 = LDiv0/4; // 算出4分音符的長度
LDiv4 = LDiv4-LDiv4*SOUND_SPACE; // 普通音最長間隔標准
TR0 = 0;
TR1 = 1;
while(Point < SoundLength)
{
SL=Tone%10; //計算出音符
SM=Tone/10%10; //計算出高低音
SH=Tone/100; //計算出是否升半
CurrentFre = NewFreTab[SignTab[SL-1]+SH]; //查出對應音符的頻率
if(SL!=0)
{
if (SM==1) CurrentFre >>= 2; //低音
if (SM==3) CurrentFre <<= 2; //高音
Temp_T = 65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);//計算計數器初值
Sound_Temp_TH0 = Temp_T/256;
Sound_Temp_TL0 = Temp_T%256;
TH0 = Sound_Temp_TH0;
TL0 = Sound_Temp_TL0 + 12; //加12是對中斷延時的補償
//音樂彩燈的閃爍
k=l;
k=Temp_T%8;
if(k==l) k=k+2;
P0=led[k];

}
SLen=LengthTab[Length%10]; //算出是幾分音符
XG=Length/10%10; //算出音符類型(0普通1連音2頓音)
FD=Length/100;
LDiv=LDiv0/SLen; //算出連音音符演奏的長度(多少個10ms)
if (FD==1)
LDiv=LDiv+LDiv/2;
if(XG!=1)
if(XG==0) //算出普通音符的演奏長度
if (SLen<=4)
LDiv1=LDiv-LDiv4;
else
LDiv1=LDiv*SOUND_SPACE;
else
LDiv1=LDiv/2; //算出頓音的演奏長度
else
LDiv1=LDiv;
if(SL==0) LDiv1=0;
LDiv2=LDiv-LDiv1; //算出不發音的長度
if (SL!=0)
{
TR0=1;
for(i=LDiv1;i>0;i--) //發規定長度的音
{
while(TF1==0);
TH1 = Sound_Temp_TH1;
TL1 = Sound_Temp_TL1;
TF1=0;
}
}
if(LDiv2!=0)
{
TR0=0; BeepIO=1;
for(i=LDiv2;i>0;i--) //音符間的間隔
{
while(TF1==0);
TH1 = Sound_Temp_TH1;
TL1 = Sound_Temp_TL1;
TF1=0;
}
}
Point+=2;
Tone=Sound[Point];
Length=Sound[Point+1];
}
BeepIO = 1;
}
//**************************************************************************
void delay() //為了顯示的延遲
{
unsigned int j;
// for (i=0;i<50;i++)
for (j=0;j<30;j++);
}

void main()
{
IT0=1; IT1=1; //外部中斷下降沿觸發
EX0=1;EX1=1;
EA=1; //開總中斷

InitialSound();
Play(Music_birth,4,2,300);
}

㈦ 求單片機LED燈閃爍生日快樂代碼。謝謝

生日快樂歌曲C代碼
/*******************************************/
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit beep = P1^5;
uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159,
212,212,106,126,159,169,190,119,119,126,159,142,159,0};

uchar code SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,
9,3,12,12,12,12,12,9,3,12,12,12,24,0};

//延時
void DelayMS(uint x)
{
uchar t;
while(x--) for(t=0;t<120;t++);
}

void PlayMusic()
{
uint i=0,j,k;
while(SONG_LONG[i]!=0||SONG_TONE[i]!=0)
{ //播放各個音符,SONG_LONG 為拍子長度
for(j=0;j<SONG_LONG[i]*20;j++)
{
beep=~beep;
//SONG_TONE 延時表決定了每個音符的頻率
for(k=0;k<SONG_TONE[i]/3;k++);
}
DelayMS(10);
i++;
}
}

void main()
{

beep=0;
while(1)
{
PlayMusic(); //播放生日快樂
DelayMS(500); //播放完後暫停一段時間
}
}

㈧ 51music單片機音樂代碼生成器中的蜂鳴介面是什麼意思還有1---後面的四節拍怎麼輸入

#include<reg51.h>
sbit speaker=P1^6; //(單片機音樂代碼生成器中的蜂鳴介面,就是一個IO腳)
unsigned char timer0h,timer0l,time;
//生日歌,這三個位元組一組,分別定義:旋律(1234567)、高低音(低音、中音、高音)、節奏(長度)
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 //定時器0中斷服務程序,控制聲音打頻率
{ TR0=0;
speaker=!speaker;
TH0=timer0h;
TL0=timer0l;
TR0=1;
}
void song() //啟動演奏當前頻率
{ TH0=timer0h; //當前頻率的高八位
TL0=timer0l; //當前頻率的低八位
TR0=1; //啟動定時器0
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(); //演奏當前頻率,共75/3,25個曲調。
}
}
}
//程序的設計思路就是將一首樂曲分成三個變數來控制,分別是曲調、所在音區、時長。而控制曲調的因素也就是頻率又分成高八位和第八位兩個表,每演奏一個曲調,就根據該曲調的三個變數來取值。

㈨ 單片機的蜂鳴器演奏 生日歌

你一個分也沒給,不想給你說,還是做下好人吧!
要想用模擬歌聲首先要定義1234567不同的音符,做個h文件放這里,,你後做其他的歌曲可以共享。這里網上可以找到,我給你一個。。然後建立個歌曲主程序,根據歌譜子,結合你的音符定義給他他編碼,把編的碼放到一個數組裡面然後輸出就好了,,下面我給你一首歌作為例子,,我也沒做個你那個什麼生日歌,你自己去編碼吧,,有譜子很好編碼的
/**************************************************************************

SOUND PLAY FOR 51MCU

COPYRIGHT (c) 2005 BY JJJ.
-- ALL RIGHTS RESERVED --

File Name: SoundPlay.h
Author: Jiang Jian Jun
Created: 2005/5/16
Modified: NO
Revision: 1.0

*******************************************************************************/

/*說明**************************************************************************
曲譜存貯格式 unsigned char code MusicName{音高,音長,音高,音長...., 0,0}; 末尾:0,0 表示結束(Important)

音高由三位數字組成:
個位是表示 1~7 這七個音符
十位是表示音符所在的音區:1-低音,2-中音,3-高音;
百位表示這個音符是否要升半音: 0-不升,1-升半音。

音長最多由三位數字組成:
個位表示音符的時值,其對應關系是:
|數值(n): |0 |1 |2 |3 | 4 | 5 | 6
|幾分音符: |1 |2 |4 |8 |16 |32 |64 音符=2^n
十位表示音符的演奏效果(0-2): 0-普通,1-連音,2-頓音
百位是符點位: 0-無符點,1-有符點

調用演奏子程序的格式
Play(樂曲名,調號,升降八度,演奏速度);
|樂曲名 : 要播放的樂曲指針,結尾以(0,0)結束;
|調號(0-11) : 是指樂曲升多少個半音演奏;
|升降八度(1-3) : 1:降八度, 2:不升不降, 3:升八度;
|演奏速度(1-12000): 值越大速度越快;

***************************************************************************/
#ifndef __SOUNDPLAY_H_REVISION_FIRST__
#define __SOUNDPLAY_H_REVISION_FIRST__

//**************************************************************************

#define SYSTEM_OSC 12000000 //定義晶振頻率12000000HZ
#define SOUND_SPACE 4/5 //定義普通音符演奏的長度分率,//每4分音符間隔
sbit BeepIO = P3^7; //定義輸出管腳

unsigned int code FreTab[12] = { 262,277,294,311,330,349,369,392,415,440,466,494 }; //原始頻率表
unsigned char code SignTab[7] = { 0,2,4,5,7,9,11 }; //1~7在頻率表中的位置
unsigned char code LengthTab[7]= { 1,2,4,8,16,32,64 };
unsigned char Sound_Temp_TH0,Sound_Temp_TL0; //音符定時器初值暫存
unsigned char Sound_Temp_TH1,Sound_Temp_TL1; //音長定時器初值暫存
//**************************************************************************
void InitialSound(void)
{
BeepIO = 0;
Sound_Temp_TH1 = (65535-(1/1200)*SYSTEM_OSC)/256; // 計算TL1應裝入的初值 (10ms的初裝值)
Sound_Temp_TL1 = (65535-(1/1200)*SYSTEM_OSC)%256; // 計算TH1應裝入的初值
TH1 = Sound_Temp_TH1;
TL1 = Sound_Temp_TL1;
TMOD |= 0x11;
ET0 = 1;
ET1 = 0;
TR0 = 0;
TR1 = 0;
EA = 1;
}

void BeepTimer0(void) interrupt 1 //音符發生中斷
{
BeepIO = !BeepIO;
TH0 = Sound_Temp_TH0;
TL0 = Sound_Temp_TL0;
}
//**************************************************************************
void Play(unsigned char *Sound,unsigned char Signature,unsigned Octachord,unsigned int Speed)
{
unsigned int NewFreTab[12]; //新的頻率表
unsigned char i,j;
unsigned int Point,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength;
unsigned char Tone,Length,SL,SH,SM,SLen,XG,FD;
for(i=0;i<12;i++) // 根據調號及升降八度來生成新的頻率表
{
j = i + Signature;
if(j > 11)
{
j = j-12;
NewFreTab[i] = FreTab[j]*2;
}
else
NewFreTab[i] = FreTab[j];

if(Octachord == 1)
NewFreTab[i]>>=2;
else if(Octachord == 3)
NewFreTab[i]<<=2;
}

SoundLength = 0;
while(Sound[SoundLength] != 0x00) //計算歌曲長度
{
SoundLength+=2;
}

Point = 0;
Tone = Sound[Point];
Length = Sound[Point+1]; // 讀出第一個音符和它時時值

LDiv0 = 12000/Speed; // 算出1分音符的長度(幾個10ms)
LDiv4 = LDiv0/4; // 算出4分音符的長度
LDiv4 = LDiv4-LDiv4*SOUND_SPACE; // 普通音最長間隔標准
TR0 = 0;
TR1 = 1;
while(Point < SoundLength)
{
SL=Tone%10; //計算出音符
SM=Tone/10%10; //計算出高低音
SH=Tone/100; //計算出是否升半
CurrentFre = NewFreTab[SignTab[SL-1]+SH]; //查出對應音符的頻率
if(SL!=0)
{
if (SM==1) CurrentFre >>= 2; //低音
if (SM==3) CurrentFre <<= 2; //高音
Temp_T = 65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);//計算計數器初值
Sound_Temp_TH0 = Temp_T/256;
Sound_Temp_TL0 = Temp_T%256;
TH0 = Sound_Temp_TH0;
TL0 = Sound_Temp_TL0 + 12; //加12是對中斷延時的補償
}
SLen=LengthTab[Length%10]; //算出是幾分音符
XG=Length/10%10; //算出音符類型(0普通1連音2頓音)
FD=Length/100;
LDiv=LDiv0/SLen; //算出連音音符演奏的長度(多少個10ms)
if (FD==1)
LDiv=LDiv+LDiv/2;
if(XG!=1)
if(XG==0) //算出普通音符的演奏長度
if (SLen<=4)
LDiv1=LDiv-LDiv4;
else
LDiv1=LDiv*SOUND_SPACE;
else
LDiv1=LDiv/2; //算出頓音的演奏長度
else
LDiv1=LDiv;
if(SL==0) LDiv1=0;
LDiv2=LDiv-LDiv1; //算出不發音的長度
if (SL!=0)
{
TR0=1;
for(i=LDiv1;i>0;i--) //發規定長度的音
{
while(TF1==0);
TH1 = Sound_Temp_TH1;
TL1 = Sound_Temp_TL1;
TF1=0;
}
}
if(LDiv2!=0)
{
TR0=0; BeepIO=0;
for(i=LDiv2;i>0;i--) //音符間的間隔
{
while(TF1==0);
TH1 = Sound_Temp_TH1;
TL1 = Sound_Temp_TL1;
TF1=0;
}
}
Point+=2;
Tone=Sound[Point];
Length=Sound[Point+1];
}
BeepIO = 0;
}
//**************************************************************************
#endif

同一首歌編碼輸出
#include <REG52.H>
#include "SoundPlay.h"

void Delay1ms(unsigned int count)
{
unsigned int i,j;
for(i=0;i<count;i++)
for(j=0;j<120;j++);
}

//*****************************Music******************************************************

//同一首歌
unsigned char code Music_Same[]={ 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x66, 0x18,0x03,
0x17,0x02, 0x15,0x02, 0x16,0x01, 0x15,0x02, 0x10,0x02,
0x15,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x02,
0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x02, 0x18,0x66,
0x17,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x16,0x00,
0x17,0x01, 0x19,0x02, 0x1B,0x02, 0x1B,0x70, 0x1A,0x03,
0x1A,0x01, 0x19,0x02, 0x19,0x03, 0x1A,0x03, 0x1B,0x02,
0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x18,0x66, 0x18,0x03,
0x19,0x02, 0x1A,0x02, 0x19,0x0C, 0x18,0x0D, 0x17,0x03,
0x16,0x01, 0x11,0x02, 0x11,0x03, 0x10,0x03, 0x0F,0x0C,
0x10,0x02, 0x15,0x00, 0x1F,0x01, 0x1A,0x01, 0x18,0x66,
0x19,0x03, 0x1A,0x01, 0x1B,0x02, 0x1B,0x03, 0x1B,0x03,
0x1B,0x0C, 0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x1F,0x01,
0x1A,0x01, 0x18,0x66, 0x19,0x03, 0x1A,0x01, 0x10,0x02,
0x10,0x03, 0x10,0x03, 0x1A,0x0C, 0x18,0x0D, 0x17,0x03,
0x16,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x70,
0x18,0x03, 0x17,0x02, 0x15,0x03, 0x15,0x03, 0x16,0x66,
0x16,0x03, 0x16,0x02, 0x16,0x03, 0x15,0x03, 0x10,0x02,
0x10,0x01, 0x11,0x01, 0x11,0x66, 0x10,0x03, 0x0F,0x0C,
0x1A,0x02, 0x19,0x02, 0x16,0x03, 0x16,0x03, 0x18,0x66,
0x18,0x03, 0x18,0x02, 0x17,0x03, 0x16,0x03, 0x19,0x00,
0x00,0x00 };

//***********************************************************************************
main()
{
InitialSound();
while(1)
{
Play(Music_Girl,0,3,360);
Delay1ms(500);
Play(Music_Same,0,3,360);
Delay1ms(500);
Play(Music_Two,0,3,360);
Delay1ms(500);
}
}

閱讀全文

與51單片機生日歌代碼相關的資料

熱點內容
程序員試用期匯報問題協助怎麼寫 瀏覽:127
抖音演算法到底是什麼 瀏覽:126
哪個vlan技術對報文加密 瀏覽:570
單片機定時電路 瀏覽:672
山西平台伺服器雲主機 瀏覽:700
按摩肚臍解壓視頻 瀏覽:989
php55安裝教程 瀏覽:137
雲伺服器怎麼查找本機域名 瀏覽:22
qd123y壓縮機參數 瀏覽:385
程序員媽媽懷孕 瀏覽:490
金普國際編程 瀏覽:537
java什麼是引用類型 瀏覽:944
這是命令嗎txt 瀏覽:314
支付寶android包名 瀏覽:154
eclipsemaven命令 瀏覽:68
24路伺服器配什麼cpu 瀏覽:466
壓縮文件和解壓文件哪個快 瀏覽:675
亞馬遜雲伺服器視頻通話 瀏覽:912
金融知識app哪個好 瀏覽:978
農行理財app收益在哪裡 瀏覽:969