導航:首頁 > 操作系統 > 單片機c語言歌曲

單片機c語言歌曲

發布時間:2023-03-07 22:24:49

『壹』 單片機蜂鳴器c語言《蘭花草》音樂代碼

程序名: MCS51U實驗板配套程序-歌曲:蘭花草
;電路介紹:蜂鳴器接P2.0;實驗前要連接J12

;***********************************************************************
OUT BIT P2.0

ORG 0
AJMP START
ORG 0BH
AJMP TIM0
START: MOV TMOD,#1
MOV IE,#10000010B
START0: MOV 30H,#0
NEXT: MOV A,30H
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV R2,A
JZ END0
ANL A,#0FH
MOV R5,A
MOV A,R2
SWAP A
ANL A,#0FH
JNZ SING
CLR TR0
AJMP D1
SING: DEC A
MOV 22H,A
RL A
MOV DPTR,#TABLE1
MOVC A,@A+DPTR
MOV TH0,A
MOV 21H,A
MOV A,22H
RL A
INC A
MOVC A,@A+DPTR
MOV TL0,A
MOV 20H,A
SETB TR0
D1: CALL DELAY
INC 30H
AJMP NEXT
END0: CLR TR0
AJMP START0
TIM0: PUSH ACC
PUSH PSW
MOV TL0,20H
MOV TH0,21H
CPL OUT
POP PSW
POP ACC
RETI
DELAY: MOV R7,#2
D2: MOV R4,#125
D3: MOV R3,#248
DJNZ R3,$
DJNZ R4,D3
DJNZ R7,D2
DJNZ R5,DELAY
RET
;====================================
TABLE1: DW 64021,64103,64260,64400
DW 64524,64580,64684,64777
DW 64820,64898,64968,65030
DW 64934
TABLE: ;1
DB 42H,82H,82H,82H,84H,02H,72H
DB 62H,72H,62H,52H,48H
DB 0B2H,0B2H,0B2H,0B2H,0B4H,02H,0A2H
;2
DB 12H,0A2H,0D2H,92H,88H
DB 82H,0B2H,0B2H,0A2H,84H,02H,72H
DB 62H,72H,62H,52H,44H,02H,12H
;3
DB 12H,62H,62H,52H,44H,02H,82H
DB 72H,62H,52H,32H,48H
DB 00H

END

『貳』 怎麼把歌曲用單片機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;
}
}

『叄』 單片機蜂鳴器c語言《蘭花草》音樂代碼

;程序名: MCS51U實驗板配套程序-歌曲:蘭花草 ;電路介紹:蜂鳴器接P2.0;實驗前要連接J12 ;*********************************************************************** OUT BIT P2.0 ORG 0 AJMP START ORG 0BH AJMP TIM0 START: MOV TMOD,#1 MOV IE,#10000010B START0: MOV 30H,#0 NEXT: MOV A,30H MOV DPTR,#TABLE MOVC A,@A+DPTR MOV R2,A JZ END0 ANL A,#0FH MOV R5,A MOV A,R2 SWAP A ANL A,#0FH JNZ SING CLR TR0 AJMP D1 SING: DEC A MOV 22H,A RL A MOV DPTR,#TABLE1 MOVC A,@A+DPTR MOV TH0,A MOV 21H,A MOV A,22H RL A INC A MOVC A,@A+DPTR MOV TL0,A MOV 20H,A SETB TR0 D1: CALL DELAY INC 30H AJMP NEXT END0: CLR TR0 AJMP START0 TIM0: PUSH ACC PUSH PSW MOV TL0,20H MOV TH0,21H CPL OUT POP PSW POP ACC RETI DELAY: MOV R7,#2 D2: MOV R4,#125 D3: MOV R3,#248 DJNZ R3,$ DJNZ R4,D3 DJNZ R7,D2 DJNZ R5,DELAY RET ;==================================== TABLE1: DW 64021,64103,64260,64400 DW 64524,64580,64684,64777 DW 64820,64898,64968,65030 DW 64934 TABLE: ;1 DB 42H,82H,82H,82H,84H,02H,72H DB 62H,72H,62H,52H,48H DB 0B2H,0B2H,0B2H,0B2H,0B4H,02H,0A2H ;2 DB 12H,0A2H,0D2H,92H,88H DB 82H,0B2H,0B2H,0A2H,84H,02H,72H DB 62H,72H,62H,52H,44H,02H,12H ;3 DB 12H,62H,62H,52H,44H,02H,82H DB 72H,62H,52H,32H,48H DB 00H END

『肆』 求: 用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);
}

『伍』 51單片機曲譜編寫音樂程序,我看不懂曲譜,誰能教下我!

//《世上只有媽媽好》51單片機C語言程序和音樂采燈程序
//此程序在硬體上調試通過
//本程序的單片機晶振採用11.0592MHZ
//此程序的流水燈隨音樂的節拍的快慢而動

#include <reg51.h>
sbit speaker=P2^0; //接音頻放大電路,
//sbit speaker=P3^2;
sbit sw=P3^0; //電後位後,sw合上後就開始放音樂,燈開始閃動,sw斷開,音樂停 止, 燈也同時停止閃動
unsigned char timer0h,timer0l,time,led=1,j=0;
unsigned char flagd=0;
//世上只有媽媽好數據表
code unsigned char sszymmh[]={ 6,2,3, 5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1,
6,2,4, 3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1,
6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2,
5,2,1, 6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1,
2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0
} ;
// 音階頻率表 高八位
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;
time=1;
sw=1;
while(time)
{
if(sw)
{P1=0;i=0;continue;}
if(j==8)
{
//led=1;
j=0;flagd=~flagd;
if(flagd)
{
led=0x80;
}
else
{
led=1;
}
}

else
{
P1=~led;
if(flagd)
{
led=led>>1;
}
else
{
led=le d<<1;
}
j++;
}
k=sszymmh[i]+7*sszymmh[i+1]-1;
timer0h=FREQH[k];
timer0l=FREQL[k];
time=sszymmh[i+2];
i=i+3;
song();
}
}
}

『陸』 單片機蜂鳴器c語言《蘭花草》音樂代碼

程序名: MCS51U實驗板配套程序-歌曲:蘭花草
;電路介紹:蜂鳴器接P2.0;實驗前要連接J12

;***********************************************************************
OUT BIT P2.0

ORG 0
AJMP START
ORG 0BH
AJMP TIM0
START: MOV TMOD,#1
MOV IE,#10000010B
START0: MOV 30H,#0
NEXT: MOV A,30H
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV R2,A
JZ END0
ANL A,#0FH
MOV R5,A
MOV A,R2
SWAP A
ANL A,#0FH
JNZ SING
CLR TR0
AJMP D1
SING: DEC A
MOV 22H,A
RL A
MOV DPTR,#TABLE1
MOVC A,@A+DPTR
MOV TH0,A
MOV 21H,A
MOV A,22H
RL A
INC A
MOVC A,@A+DPTR
MOV TL0,A
MOV 20H,A
SETB TR0
D1: CALL DELAY
INC 30H
AJMP NEXT
END0: CLR TR0
AJMP START0
TIM0: PUSH ACC
PUSH PSW
MOV TL0,20H
MOV TH0,21H
CPL OUT
POP PSW
POP ACC
RETI
DELAY: MOV R7,#2
D2: MOV R4,#125
D3: MOV R3,#248
DJNZ R3,$
DJNZ R4,D3
DJNZ R7,D2
DJNZ R5,DELAY
RET
;====================================
TABLE1: DW 64021,64103,64260,64400
DW 64524,64580,64684,64777
DW 64820,64898,64968,65030
DW 64934
TABLE: ;1
DB 42H,82H,82H,82H,84H,02H,72H
DB 62H,72H,62H,52H,48H
DB 0B2H,0B2H,0B2H,0B2H,0B4H,02H,0A2H
;2
DB 12H,0A2H,0D2H,92H,88H
DB 82H,0B2H,0B2H,0A2H,84H,02H,72H
DB 62H,72H,62H,52H,44H,02H,12H
;3
DB 12H,62H,62H,52H,44H,02H,82H
DB 72H,62H,52H,32H,48H
DB 00H

END

『柒』 讓單片機唱歌的c語言程序是怎麼弄的

其實原理很簡單,就是控制單片機的某個引腳,輸出一定頻率的方波信號,而輸出方波信號的方法,是最基礎的,最簡單的編程了,用定時器定時,根據信號頻率算出信號周期,然後計算出定時的時間。那單片機演奏歌曲的程序,也是同樣的原理,只是事先根據歌曲的簡譜查出每個音階的信號頻率,再根據各音階頻率計算出定時器的初值。演奏時,按簡譜的各音階順序輸出不同的頻率的信號就行了。

下表是音階與頻率對應關系表,給出常用音階對應的定時常數。

『捌』 求幾首單片機經典歌曲樂譜,及蜂鳴器驅動原理和 C語言程序。

建議用小喇叭,效果比蜂鳴器好很多,驅動就是正負極啊,一接就好了,當時做這個不是難點。C語言程序網上可以搜到的,這里歌曲樂譜都是一些數組組成,你換歌相當於換數組里的數,所以意思就是程序模板是不變的。定時器可能會用到,你說的中斷切換歌曲也是可以實現的。

閱讀全文

與單片機c語言歌曲相關的資料

熱點內容
歐姆龍plc編程第36講 瀏覽:907
我的世界如何將一個伺服器弄崩 瀏覽:6
php網站訪問量代碼 瀏覽:431
怠速壓縮機咔咔響 瀏覽:176
怎麼才能修改APP中的數據 瀏覽:688
哪裡有搶單的app 瀏覽:462
演算法概率題 瀏覽:465
長方形拉伸的命令 瀏覽:279
python代碼函數編程技術 瀏覽:194
java正則式 瀏覽:429
外包程序員好進嗎 瀏覽:384
雲伺服器服務模型架構 瀏覽:901
刪文件夾什麼指令 瀏覽:509
極速抖音已加密怎麼辦 瀏覽:603
matlab拉格朗日演算法框圖 瀏覽:430
華為公司計算機視覺演算法顧問 瀏覽:254
夏老師講的單片機 瀏覽:298
在編程中如何將圖片放大 瀏覽:163
appstore怎麼看是否付費 瀏覽:603
程序員和碩士 瀏覽:951