导航:首页 > 编程语言 > 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音乐程序相关的资料

热点内容
有线电视升级失败 浏览:560
火绒安全把文件删掉了在哪里找 浏览:503
手机qq网络状态方框 浏览:225
哪里有文件纸袋 浏览:873
复制的东西能不能粘贴到空文件夹 浏览:876
酒店没有网络如何缴费 浏览:380
win10开机滚动很久 浏览:520
可对元数据实例进行的操作有什么 浏览:934
什么后缀的文件kit 浏览:295
word行书字体库下载 浏览:579
iosuc版本历史版本 浏览:14
电影字幕文件制作软件 浏览:723
windows10免密码登录 浏览:762
iphone5s跑步记步 浏览:978
手机网站设计怎么做好 浏览:322
中兴路由器修改密码 浏览:391
小米忘记压缩文件密码 浏览:716
cad哪些字体是形文件 浏览:2
word2007宝典pdf 浏览:46
lg电视如何连接网络 浏览:392

友情链接