怎么用单片机编写音乐程序

 我来答
zhxiufan
推荐于2016-07-05 · 知道合伙人软件行家
zhxiufan
知道合伙人软件行家
采纳数:14923 获赞数:38379
国家科技进步奖

向TA提问 私信TA
展开全部

单片机比如51系列都有定时器,因此可以利用它来控制扬声器按一定频率发声,以下就是一个例程,用按键的按下和抬起来模拟电子琴的控制程序。

T0H EQU 30H

T0L EQU 31H

ORG 0000H

LJMP MAIN

ORG 000BH

LJMP T0ISR

ORG 0030H

MAIN:

    MOV SP,#5FH

    MOV TMOD,#01H

    SETB ET0

    SETB EA

LOOP:

    MOV DPTR,#FREQ

    MOV A,P2 ;控制高中低音区

    MOV R1,#0

    ANL A,#03H

    JB ACC.0,LOOP01

    MOV R1,#14

    SJMP LOOP0

LOOP01:

    JB ACC.1,LOOP0

    MOV R1,#28

LOOP0:

    JB P0.0,LOOP1 ;控制音频1234567

    MOV A,R1

    MOVC A,@A+DPTR

    MOV T0H,A

    INC DPTR

    MOV A,R1

    MOVC A,@A+DPTR

    MOV T0L,A

    LJMP LOOP

LOOP1:

    JB P0.1,LOOP2

    MOV A,R1

    ADD A,#2

    MOVC A,@A+DPTR

    MOV T0H,A

    INC DPTR

    MOV A,R1

    ADD A,#2

    MOVC A,@A+DPTR

    MOV T0L,A

    LJMP LOOP

LOOP2:

    JB P0.2,LOOP3

    MOV A,R1

    ADD A,#4

    MOVC A,@A+DPTR

    MOV T0H,A

    INC DPTR

    MOV A,R1

    ADD A,#4

    MOVC A,@A+DPTR

    MOV T0L,A

    LJMP LOOP

LOOP3:

    JB P0.3,LOOP4

    MOV A,R1

    ADD A,#6

    MOVC A,@A+DPTR

    MOV T0H,A

    INC DPTR

    MOV A,R1

    ADD A,#6

    MOVC A,@A+DPTR

    MOV T0L,A

    LJMP LOOP

LOOP4:

    JB P0.4,LOOP5

    MOV A,R1

    ADD A,#8

    MOVC A,@A+DPTR

    MOV T0H,A

    INC DPTR

    MOV A,R1

    ADD A,#8

    MOVC A,@A+DPTR

    MOV T0L,A

    LJMP LOOP

LOOP5:

    JB P0.5,LOOP6

    MOV A,R1

    ADD A,#10

    MOVC A,@A+DPTR

    MOV T0H,A

    INC DPTR

    MOV A,R1

    ADD A,#10

    MOVC A,@A+DPTR

    MOV T0L,A

    LJMP LOOP

LOOP6:

    JB P0.6,LOOP7

    MOV A,R1

    ADD A,#12

    MOVC A,@A+DPTR

    MOV T0H,A

    INC DPTR

    MOV A,R1

    ADD A,#12

    MOVC A,@A+DPTR

    MOV T0L,A

    LJMP LOOP

LOOP7:

    CLR TR0

    LJMP LOOP

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

T0ISR:

    CLR TR0

    MOV TH0,T0H

    MOV TL0,T0L

    SETB TR0

    CPL P3.0

    RETI

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

FREQ:

DB 0F2H,03DH ;低音1

DB 0F3H,0BDH ;低音2

DB 0F5H,014H ;低音3

DB 0F5H,0B1H ;低音4

DB 0F6H,0D0H ;低音5

DB 0F7H,0D1H ;低音6

DB 0F8H,0B5H ;低音7


DB 0F9H,01EH ;中音1

DB 0F9H,0DEH ;中音2

DB 0FAH,08AH ;中音3

DB 0FAH,0D8H ;中音4

DB 0FBH,068H ;中音5

DB 0FBH,0E8H ;中音6

DB 0FCH,05AH ;中音7

 

DB 0FCH,08FH ;高音1

DB 0FCH,0EFH ;高音2

DB 0FDH,045H ;高音3

DB 0FDH,06CH ;高音4

DB 0FDH,0B4H ;高音5

DB 0FDH,0F4H ;高音6

DB 0FEH,029H ;高音7

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

END


康哥小窝窝
2011-08-02 · TA获得超过163个赞
知道小有建树答主
回答量:161
采纳率:0%
帮助的人:46.9万
展开全部
#include <reg52.h>
unsigned char Count;
sbit fengming=P3^4 ; //单片机蜂鸣器定义 

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()
{
TMOD = 0x01;
IE = 0x82;
TH0 = 0xD8;
TL0 = 0xEF; //12MZ晶振,10ms
}

void Time0_Int() interrupt 1
{
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 ) //歌曲结束符
{
Addr++;
i++;
if(i==2)
{
i=0;
return;
}
}
else
{
Temp2 = SONG[Addr++];
TR0 = 1;
while(1)
{
fengming= ~fengming;
Delay_xMs(Temp1);
if ( Temp2 == Count )
{
Count = 0;
break;
}
}
}
}
}
/*-------------------------------------------------
功能:主程序
-------------------------------------------------*/
void main()
{
Time0_Init(); //定时器0中断初始化
while(1)
{
Play_Song(0);
}
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式