如何用c语言编写8051单片机音乐程序

如何用c语言编写8051单片机音乐程序,第1张

#define uchar unsigned char //定义一下方便使用

#define uint unsigned int

#define ulong unsigned long

#include <reg52h> //包括一个52标准内核的头文件

char code dx516[3] _at_ 0x003b;//这是为了仿真设置的

sbit BEEP=P1^7; //喇叭输出脚

sbit P10=P1^0;

sbit K1= P3^2;

sbit K2= P3^5;

sbit K3= P2^4;

sbit K4= P2^5;

uchar th0_f; //在中断中装载的T0的值高8位

uchar tl0_f; //在中断中装载的T0的值低8位

//T0的值,及输出频率对照表

uchar code freq[362]={

0xA9,0xEF,//00220HZ ,1 //0

0x93,0xF0,//00233HZ ,1#

0x73,0xF1,//00247HZ ,2

0x49,0xF2,//00262HZ ,2#

0x07,0xF3,//00277HZ ,3

0xC8,0xF3,//00294HZ ,4

0x73,0xF4,//00311HZ ,4#

0x1E,0xF5,//00330HZ ,5

0xB6,0xF5,//00349HZ ,5#

0x4C,0xF6,//00370HZ ,6

0xD7,0xF6,//00392HZ ,6#

0x5A,0xF7,//00415HZ ,7

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

0x2A,0xFC,//00932HZ `1#

0x62,0xFC,//00988HZ `2

0x95,0xFC,//01046HZ `2#

0xC7,0xFC,//01109HZ `3

0xF6,0xFC,//01175HZ `4

0x22,0xFD,//01244HZ `4#

0x4B,0xFD,//01318HZ `5

0x73,0xFD,//01397HZ `5#

0x98,0xFD,//01480HZ `6

0xBB,0xFD,//01568HZ `6#

0xDC,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 xdata diaodata[112]; //音调缓冲

uchar xdata jiedata[112]; //音长缓冲

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++) //按照音长延时

{

for(n=0;n<32000;n++);

if((!K1)||(!K2)||(!K3)||(!K4))//发现按键,立即退出播放

{

TR0=0;

return;

}

}

TR0=0;

for(n=0;n<500;n++); //音符间延时

TR0=1;

}

TR0=0;

}

//仙剑

uchar code xianjian[]={

"|3_3_3_2_3-|2_3_2_2_,6,6_,7_|12_1_,7,6_,5_|,6---|"

"3_3_3_2_36_|5_6_5_5_22_3_|45_4_32_1_|3--3_|"

"67_6_55_3_|5--3_5_|26_5_32_3_|3---|"

"26_6_6-|16_6_66_7_|`17_6_76_7_|3--3_|"

"67_6_55_3_|5--3_5_|67_6_76_7_|3---|"

"26_6_6-|16_6_66_7_|`17_6_75_|6---|"

};

uchar code song3[]={

"5-5_3_2_1_|3---|6-6_4_2_1_"

",7--,5_|13_51_|,73_5 5_|"

"67_`16_|6_5_5-3_2_|11_13_2_|"

"11_12_3_|21_,62_3_|2-- ,5_|"

"13_51_|,73_55_|67_`16_|"

"6_5_5-3_2_|11_13_2_|11_12_3_"

"2,6_,71_2_|1--"

};

//世上只有妈妈好

uchar code mamahao[]={

"65_35|`16_5_6-|35_6_53_2_|1_,6_5_3_2-|"

"23_55_6_|321-|53_2_1_,6_1_|,5--"

};

//三个按键选择三首不同的音乐播放,一个键停止播放

void main(void) // 主程序

{

TMOD = 0x01; //使用定时器0的16位工作模式

TR0 = 0;

ET0 = 1; //定时器0中断

EA = 1; //打开总中断

while(1)

{

if(!K1)

{

while(!K1);

play(xianjian); //播放音乐

}

if(!K2)

{

while(!K2);

play(song3); //播放音乐

}

if(!K3)

{

while(!K3);

play(mamahao); //播放音乐

}

}

}

NUM EQU 30H

UP EQU P36

DN EQU P37

ORG     0000H

AJMP    START           ;Fosc=12MHz

ORG     000BH           ;T0 中断入口地址

AJMP    INT_T0

ORG     001BH           ;T1 中断入口地址

AJMP    INT_T1

START:

MOV  SP, #60H

MOV TMOD, #11H

MOV IE, #8AH 

MOV IP, #08H          ;T1中断优先于T0中断

MOV TH1, #3CH

MOV TL1, #0BH        ;以上为系统初始化,T=60ms

W1: MOV P2, #01110001B    ;等待提示"F" ,01110001

LCALL GETKEY

CJNE  A, #0FFH, W1  

MOV  A, B

CJNE    A, #0AH, W2

LCALL   KEY_A            ;输入键是'A',转KEY_A

SJMP W1

W2: CJNE A, #0BH, W3

LCALL   KEY_B           ;输入键是'B',转KEY_B

W3: CJNE A, #0CH, W4

LCALL   KEY_C           ;输入键是'C',转KEY_C

W4: CJNE A, #0DH, W5

LCALL   KEY_D

    SJMP W1                ;输入键是'D',转KEY_D

W5: CJNE A,#0EH,W6 ;下一首

INC NUM

MOV A,NUM

CJNE A,#4,W51

W51:

JC W52

MOV NUM,#0

W52:

MOV A,NUM

SJMP D0

W6: CJNE A,#0FH,W7 ;上一首

MOV A,NUM

JNZ W61

MOV NUM,#4

W61:

DEC NUM

MOV A,NUM

SJMP D0

W7:

SJMP W1

KEY_A:                        ;从1~4按顺序播放

LCALL MUSIC1      ;乐曲《我和你》

LCALL MUSIC2       ;乐曲《万水千山总是情》

LCALL MUSIC3               ;乐曲《送别》

LCALL MUSIC4               ;乐曲《北京欢迎您》

RET

KEY_B:                         ;从1~4循环播放

T11: LCALL KEY_A

SJMP T11

RET

KEY_C:                        ;循环播放某一乐曲

T3: LCALL GETWORD

CJNE A, #0FFH, T3

MOV A, B

D0:

JNZ D1

SJMP E0

D1: DEC A

JNZ D2

SJMP E1

D2: DEC A

JNZ E3

SJMP E2

E0: LCALL MUSIC1            ;乐曲《我和你》

SJMP E0

E1: LCALL MUSIC2           ;乐曲《万水千山总是情》

SJMP E1

E2: LCALL MUSIC3           ;乐曲《送别》

SJMP E2

E3: LCALL MUSIC4           ;乐曲《北京欢迎您》

SJMP E3

RET

KEY_D:                      ;从某一乐曲开始播放,播放完后停止

T2: LCALL GETWORD

CJNE A, #0FFH, T2

MOV A, B

CJNE A, #00H, B1

SJMP C0

B1: CJNE A, #01H, B2

SJMP C1

B2: CJNE A, #02H, C3

SJMP C2

C0: LCALL MUSIC1               ;乐曲《我和你》

C1: LCALL MUSIC2               ;乐曲  《万水千山总是情》 

C2: LCALL MUSIC3               ;乐曲《送别》

C3: LCALL MUSIC4               ;乐曲《北京欢迎您》

RET

MUSIC1:                         ;第一首《我和你》

MOV  P2, #00000110B  ;显示1,00000110

MOV  R5, #11111001B

MOV  52H, #HIGH TABLE10

    MOV  53H, #LOW TABLE10

MOV NUM,#0

LCALL  music

RET

MUSIC2:     ;第二首《万水千山总是情》

MOV P2, #01011011B           ;显示2,01011011

MOV R5, #10100100B

MOV  52H, #HIGH TABLE20

    MOV  53H, #LOW TABLE20 

MOV NUM,#1

    LCALL  music

RET

MUSIC3:                   ;第三首《送别》

MOV P2, #01001111B   ;显示3,01001111

MOV R5, #10110000B

MOV  52H, #HIGH TABLE30

    MOV  53H, #LOW TABLE30 

MOV NUM,#2

    LCALL  music

RET

MUSIC4:                         ;第四首《北京欢迎您》

MOV P2, #01100110B     ;显示4,01100110

MOV R5, #10011001B

MOV  52H, #HIGH TABLE40

    MOV  53H, #LOW TABLE40 

MOV NUM,#3

    LCALL  music

RET 

music: MOV R3,#00H           ;音乐解码器

NEXT20:

MOV A,P3

ORL A,#0C0H

MOV P3,A

MOV A,P3

CPL A

ANL A,#0C0H

JZ NEXT21

POP ACC

POP ACC

LJMP W1

RET

NEXT21:

     MOV   A, R3

 MOV  DPH, 52H

 MOV  DPL, 53H

 MOVC A, @A+DPTR

 MOV  R2, A

 JZ  STOP

 ANL  A, #0FH

 MOV  R1, A

 MOV  A, R2

 SWAP  A

 ANL  A, #0FH

 JNZ  SING

 CLR  TR0

 JMP  W11

;----------------------------------------------------------

SING:

DEC  A

MOV  22H, A

RL   A

MOV  DPTR, #TABLE00

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

SETB   TR1

W11: 

LCALL  DELAY3

INC  R3

LJMP  NEXT20

;-----------------------------------------------------------

STOP:

CLR  TR0

CLR  TR1

    RET

;

;定时器0中断子程序

;

INT_T0: 

CLR  TR0

MOV  TL0, 20H

MOV  TH0, 21H

CPL  P30

SETB TR0

RETI

;

;定时器1中断子程序

;

INT_T1: 

CLR  TR1

CLR  TR0

LCALL KEY_E

MOV  TH1, #3CH

  MOV  TL1, #0BH

DJNZ  60H, OUT

    MOV  60H, #20

OUT:

SETB TR1

RETI                      

KEY_E:

JB P31, L2              ;中断键盘检测

LCALL DELAY2           ;消除抖动

JB  P31, L2           ;如果是高电平就跳到L2

JNB   P31, $           ;如果仍然是低电平就执行中断程序

MOV P2, #0111100B        ;让LED显示E,01111001

L3: JB P31, $           ;检测低电平,直到低电平时才跳出该指令

LCALL DELAY2

JB P31, L3

JNB  P31, $           ;如果再来一个高电平才跳出中断

MOV P2,R5

L2: SETB  TR0

RET

GETKEY:                ;读A B C D键盘子程序 

SA: JB P10, SB            ;读A,是高电平表示没有按键,跳转到SB

LCALL DELAY2

JB P10, SA

JNB  P10, $

MOV P2, #01110111B     ;显示A,01110111

MOV R5, #10001000B

MOV A, #0FFH

MOV B, #0AH

RET

SB: JB P11, C11              ;读B,是高电平表示没有按键按下,跳转到C11

LCALL DELAY2

JB P11, SB

JNB P11, $

MOV P2, #01111100B        ;显示B,01111100

MOV R5, #10000011B

MOV A, #0FFH

MOV B, #0BH

RET

C11:JB P12, ED1        ;读C

LCALL DELAY2

JB  P12, C11

JNB  P12, $

MOV P2, #00111001B ;显示C,00111001

MOV R5, #11000110B

MOV A, #0FFH

MOV B, #0CH

RET

ED1:

JB P13, L0             ;读D

LCALL DELAY2

JB  P13, ED1

JNB   P13, $

MOV P2, #01011110B   ;显示D,01011110

MOV R5, #10100001B

MOV A, #0FFH

MOV B,  #0DH

L0: JB UP,L01

LCALL DELAY2

JB UP,L01

JNB UP,$

MOV A,#0FFH

MOV B,#0EH

RET

L01:

JB DN,L02

LCALL DELAY2

JB DN,L02

JNB DN,$

MOV A,#0FFH

MOV B,#0FH

L02:

RET

GETWORD:                     ;读音乐序号子程序

S1: JB P14, S2         ;读1键

LCALL DELAY2

JB P14, S1

JNB P14, $

MOV P2, #00000110B    ;显示1

MOV R5, #11111001B

MOV A, #0FFH

MOV B, #00H

RET

S2: JB P15, S3            ;读2键

LCALL DELAY2

JB P15, S2

JNB P15, $

MOV P2, #01011011B    ;显示2

MOV R5, #10100100B

MOV A, #0FFH

MOV B, #01H

RET

S3: JB P16,S4           ;读3键

LCALL DELAY2

JB P16,S3

JNB P16,$

MOV P2,#01001111B    ;显示3

MOV R5,#10110000B

MOV A,#0FFH

MOV B,#02H

RET

S4: JB P17, L1           ;读4键

LCALL DELAY2

JB P17, S1

JNB P17, $

MOV P2, #01100110B ;显示4

MOV R5, #10011001B

MOV A, #0FFH

MOV B, #03H

L1: RET

DELAY:                          ;延时子程序

        PUSH    0               ;保存现场

        PUSH    1

        MOV     0, #0H

DELAY1: MOV     1, #0H

        DJNZ    1, $

        DJNZ    0, DELAY1

        POP     1               ;恢复现场

        POP     0

        RET

DELAY2: MOV 41H, #100        ;延时子程序2

F1: MOV 42H, #250

DJNZ 42H, $

DJNZ 41H, F1

RET

DELAY3:           ;延时子程序3 

MOV  R7, #2

W22: MOV  R4, #125

W33: MOV  R0, 248

DJNZ  R0, $

DJNZ  R4, W33

DJNZ  R7, W22

DJNZ  R1, DELAY3

RET

TABLE00:                       ;频率表

DW 64260,64400,64524,64580          ;简码值 低音5, 低音6, 低音7,1

DW 64684,64777,64820,64898          ; 简码值 2,3,4,5

DW 64968,65030,65058,65110          ; 简码值 6,7,高音1,高音

        DW 65157,65178,65217            ;简码值 高音3,高音4,高音5

;--------------------------------------------------------------------

;第一首《我和你》

;--------------------------------------------------------------------

TABLE10: DB 64H,82H,42H,48H

DB 54H,64H,18H

DB 44H,54H,64H,84H

DB 5CH

DB 64H,84H,44H,04H,42H

DB 54H,64H,28H

DB 54H,14H,54H,64H

DB 4CH

DB 98H,88H

DB 98H,48H

DB 64H,24H,64H,84H

DB 5CH

DB 64H,82H,42H,48H

DB 54H,64H, 28H

DB 54H,14H,54H,34H

DB 4CH

DB 00H

;--------------------------------------------------------------------

;第二首《万水千山总是情》

;--------------------------------------------------------------------

TABLE20: DB 64H,74H,84H,04H,82H,94H,84H,6CH

DB 54H,44H,04H,62H,54H,44H,2CH

DB 24H,18H, 44H,54H,64H,84H,94H,64H,5CH

DB 64H,74H,84H,04H,82H,94H,84H,6CH

DB 54H,44H,04H,62H,54H,44H,2CH

DB 24H,18H, 44H,64H,54H,04H,42H,24H,44H,4CH

DB 64H,84H,9CH,0A4H,94H,74H,84H,04H,92H,8CH,64H,88H

DB 64H,84H,9CH,0A4H,0A4H,94H,84H,64H,5CH

DB 64H,74H, 84H,04H,82H,94H,84H,6CH

DB 54H,44H,04H,62H,54H,44H,2CH

DB 24H,18H,44H,64H,54H,42H,24,44H,4CH

DB 00H

;--------------------------------------------------------------------

;第三首《送别》

;--------------------------------------------------------------------

TABLE30:   DB 84H,62H,82H,0B8H

DB 94H,0B2H,92H,88H

DB 84H,42H,52H,64H,52H,44H

        DB 5CH

        DB 84H,62H,82H,0B4H,04H,82H

DB 94H,0B4H, 88H

DB 84H,52H,62H,74H,04H,32H

DB 4CH

DB 94H,0B4H,0B8H

DB 0A4H,92H,0A2H,0B8H

DB 92H,0A2H,0B2H,92H,92H,82H,62H,42H

DB 5CH

DB 84H,62H,82H,0B4H,04H,0A2H

DB 94H,0B4H,88H

DB 84H,52H,62H,74H,04H,32H

DB 4CH

DB 00H

;--------------------------------------------------------------------

;第四首《北京欢迎您》

;--------------------------------------------------------------------

TABLE40:    DB 62H,82H,62H,52H,12H

DB 62H,52H,12H,64H

DB 62H,52H,22H,42H 

DB 62H,54H

DB 52H,42H,22H,42H

DB 52H,62H,82H,52H

DB 62H,92H,82H,12H

DB 52H,44H

DB 52H,42H,22H,42H

DB 52H,62H,82H,52H

DB 62H,92H,82H,82H

DB 68H

DB 52H,62H,52H,42H

DB 82H,92H,64H

DB 22H,62H,64H

DB 54H,64H

DB 64H,84H

DB 0B2H,82H,94H

DB 02H,92H,82H,62H

DB 62H, 82H,88H

DB 00H

END

#include <REG52H>

#include <INTRINSH>

//本例采用89C52, 晶振为110592MHZ

//关于如何编制音乐代码, 其实十分简单,各位可以看以下代码

//频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍;

//所以拿出谱子, 试探编吧!

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=3m;

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

}

}

你参考这个吧

#include "reg51h"

unsigned char Count;

sbit _Speak =P2^3 ; //蜂鸣器控制脚

unsigned char code SONG[] ={ //祝你平安,以下为编码

0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,0x30,

0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,0x20,0x80,

0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,0x30,0x20,0x30,

0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,0x30,0x20,0x30,0x20,

0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,0x2b,0x20,0x30,0x10,0x39,

0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,0x20,0x10,0x20,0x10,0x2b,0x10,

0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x40,0x26,0x20,0x2b,

0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,

0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,

0x10,0x26, 0x10,0x2b,0x10,0x2b,0x80,0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,

0x26,0x10,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,

0x10,0x30,0x10,0x30,0x20,0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,

0x40,0x10,0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,

//路边的野华不要采 , 以下为这首歌的编码

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 Time0_Init() //定时器 0 初始化函数

{

TMOD = 0x01; //工作模式选择

IE = 0x82; //中断设置

TH0 = 0xD8; //装初值

TL0 = 0xEF; //12MZ 晶振,10ms

}

void Time0_Int() interrupt 1 //定时器 0 中断子函数

{

TH0 = 0xD8;

TL0 = 0xEF;

Count++; //长度加 1

}

/-------------------------------------------------

1MS 延时子程序

-------------------------------------------------/

void Delay_xMs(unsigned int x)

{

unsigned int i,j;

for( i =0;i < x;i++ )

{

for( j =0;j<3;j++ );

}

}

/-------------------------------------------------

功能:歌曲播放子程序 i 为播放哪一段曲目

-------------------------------------------------/

void Play_Song(unsigned char i)

{

unsigned char Temp1,Temp2;

unsigned int Addr;

Count = 0; //中断计数器清 0

Addr = i 217;

while(1)

{

Temp1 = SONG[Addr++];

if ( Temp1 == 0xFF ) //休止符

{

TR0 = 0;

Delay_xMs(100);

}

else if ( Temp1 == 0x00 ) //歌曲结束符

{

return;

}

else

{

Temp2 = SONG[Addr++];

TR0 = 1;

while(1)

{

_Speak = ~_Speak;

Delay_xMs(Temp1);

if ( Temp2 == Count )

{

Count = 0;

break;

}

}

}

}

}

/-------------------------------------------------

功能:主程序

-------------------------------------------------/

void main()

{

Time0_Init(); //定时器 0 中断初始化

while(1)

{

Play_Song(0); //播放

}

}

程序名: MCS51U实验板配套程序-歌曲:兰花草

;电路介绍:蜂鸣器接P20;实验前要连接J12

;

OUT BIT P20

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语言编写8051单片机音乐程序全部的内容,包括:如何用c语言编写8051单片机音乐程序、单片机设计音乐播放器程序和仿真图(用汇编语言)急求~谢谢!!!、求用stc89c52单片机控制蜂鸣器发出音乐的c语言程序。最好有详解。谢谢。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9314056.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存