求救!stc89c52为什么播放不了音乐?求大神搭救
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define allsong 1
sbit beep=P3^0;
sbit key_sound=P3^1;
unsigned char music_l;
unsigned char music_h;
unsigned char tone_h;
unsigned char tone_l;
unsigned char beat;//节拍
unsigned char tone;//音调
unsigned char out = 0;
uint which = 2 ; //简谱表里的第几个音 从第2个开始 第0个用来存曲速
void delayms(int ms);
void delay(int pai);
void play();
uchar code chuzhi[]={
0xfa,0x67,// 低音4 d
0xfb,0x04,// 低音5 e
0xfb,0x90,// 低音6 f
0xfc,0x0c,// 低音70x07g
0xfc,0x44,// 中音1 0x11 (17)
0xfc,0xac,// 2
0xfd,0x09,// 3
0xfd,0x34,// 4
0xfd,0x82,// 5
0xfd,0xc8,// 6
0xfe,0x06,//中音7 0x17 (23)
0xfe,0x22,//高音1 0x211. (33)
0xfe,0x56,// 2 2.
0xfe,0x85,// 3 3.
0xfe,0x9a,// 4 4.
0xfe,0xc1,// 高音5 5.
};
uchar code chuntianli[]={
0x78,//曲速0x78, 表示 120 拍/分
0,0x06,2,0x02,3,0x02,5,0x02,
7,0x02,7,0x02,7,0x02,7,0x02,7,0x02,6,0x02,
5,0x06,2,0x02,3,0x02,5,0x02,
7,0x02,7,0x02,7,0x02,7,0x02,7,0x02,6,0x02,
5,0x06,7,0x02,6,0x02,5,0x02,
5,0x02,5,0x02,5,0x02,5,0x02,5,0x02,3,0x02,//第六行
5,0x06,5,0x02,5,0x02,5,0x02,5,0x02,
6,0x02,6,0x02,6,0x02,6,0x02,6,0x02,5,0x02,//第八行
6,0x04,0,0x02,5,0x02,6,0x02,7,0x02,
9,0x02,9,0x02,9,0x02,9,0x02,9,0x02,7,0x02,
7,0x04,0,0x02,5,0x02,6,0x02,7,0x02,
10,0x02,10,0x02,10,0x02,10,0x02,10,0x02,9,0x02,//第十二行
7,0x04,0,0x02,7,0x02,7,0x02,9,0x02,
10,0x02,10,0x02,9,0x02,10,0x02,10,0x02,12,0x02,
12,0x04,0,0x02,12,0x02,12,0x02,10,0x02,
13,0x02,13,0x02,13,0x02,13,0x02,13,0x02,12,0x02,//十六行
13,0x04,0,0x02,9,0x02,10,0x02,12,0x02,
0x22,0x04,0x23,0x04,0x00,0x04,
0,0x06,9,0x02,10,0x02,12,0x02,
13,0x04,14,0x04,0,0x04,//第二十行
0,0x06,14,0x04,13,0x04,5,0x04,
13,0x04,10,0x04,0,0x04,
0,0x06,10,0x04,12,0x04,10,0x04,
13,0x04,9,0x04,0,0x04, //第二十四行
0,0x06,16,0x02,16,0x02,13,0x02,
13,0x04,14,0x04,0,0x04,
0,0x06,14,0x02,13,0x02,5,0x02,
13,0x04,14,0x04,0,0x04, //第二十八行
0,0x06,14,0x02,13,0x02,12,0x02,
13,0x04,10,0x04,0,0x04,
0,0x06,10,0x02,12,0x02,10,0x02,
13,0x0c,
13,0x02,0,0x04,2,0x02,3,0x02,12,0x02,
5,0x0c,
13,0x02,12,0x04,10,0x02,
12,0x0c,
5,0x08,0,0x04,
0xff,
};
void delayms (int ms)
{
uchar a,b,c;
while(ms--)
{
for(c=1;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
}
void delay(int pai)
{
uchar a,b,c;
while(pai--)
{
for(c=1;c>0;c--)
for(b=28;b>0;b--)
for(a=110;a>0;a--);
}
}
void timer0()interrupt 1
{
if(key_sound==0)
{
TL0=music_l;
TH0=music_h;
beep=~beep;
}
}
void play ()
{
uint qusu ;
uint pai2;
qusu = chuntianli[0];
which=2;
while(chuntianli[which]!=0xff)
{
tone =chuntianli[which];
beat = chuntianli[which+1];
which = which + 2;
tone_l = tone%16; //低四位调
tone_h = tone/16; //高四位 是低音0 中音1 高音2
if(tone != 0)
{
/* if(tone_h==0 && tone_l==4)
{
music_h = chuzhi[1];
music_l = chuzhi[2];
}
else if(tone_h==0 && tone_l==5)
{
music_h = chuzhi[3];
music_l = chuzhi[4];
}
else if(tone_h==0 && tone_l==6)
{
music_h = chuzhi[5];
music_l = chuzhi[6];
}
else if(tone_h==0&&tone_l==7)
{
music_h = chuzhi[7];
music_l = chuzhi[8];
}
else if(tone_h==1&&tone_l==11)
{
music_h = chuzhi[9];
music_l = chuzhi[10];
}
else if(tone_h==1&&tone_l==12)
{
music_h = chuzhi[11];
music_l = chuzhi[12];
}
else if(tone_h==1&&tone_l==13)
{
music_h = chuzhi[13];
music_l = chuzhi[14];
}
else if(tone_h==1&&tone_l==14)
{
music_h = chuzhi[tone+4];
music_l = chuzhi[tone+5];
}
else if(tone_h==1&&tone_l==15)
{
music_h = chuzhi[15];
music_l = chuzhi[16];
}
else if(tone_h==1&&tone_l==0)
{
music_h = chuzhi[17];
music_l = chuzhi[18];
}
else if(tone_h==1&&tone_l==1)
{
music_h = chuzhi[19];
music_l = chuzhi[20];
}
else if(tone_h==2&&tone_l==5)
{
music_h = chuzhi[21];
music_l = chuzhi[22];
}
else if(tone_h==2&&tone_l==6)
{
music_h = chuzhi[23];
music_l = chuzhi[24];
}
else if(tone_h==2&&tone_l==7)
{
music_h = chuzhi[25];
music_l = chuzhi[26];
}
else if(tone_h==2&&tone_l==8)
{
music_h = chuzhi[27];
music_l = chuzhi[28];
}
else if(tone_h==2&&tone_l==9)
{
music_h = chuzhi[29];
music_l = chuzhi[30];
}*/
TR0 = 1; /*给初值赋值后开启定时器0 蜂鸣器开始发声 */
out = 1;
}
else
{
P0 = 0x00;
flash = 0;
}
pai2 = (2400/qusu)*beat;
delay(pai2);
delayms(39);
}
}
void main()
{
P0=0x00;
P2=0xff;
TMOD=0x11;
IE=0x8a;
PT0=1;
while(1)
{
if(key_sound==0)
{
TR0=1;
TR1=1;
play();
}
}
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define allsong 1
sbit beep=P3^0;
sbit key_sound=P3^1;
unsigned char music_l;
unsigned char music_h;
unsigned char tone_h;
unsigned char tone_l;
unsigned char beat;//节拍
unsigned char tone;//音调
unsigned char out = 0;
uint which = 2 ; //简谱表里的第几个音 从第2个开始 第0个用来存曲速
void delayms(int ms);
void delay(int pai);
void play();
uchar code chuzhi[]={
0xfa,0x67,// 低音4 d
0xfb,0x04,// 低音5 e
0xfb,0x90,// 低音6 f
0xfc,0x0c,// 低音70x07g
0xfc,0x44,// 中音1 0x11 (17)
0xfc,0xac,// 2
0xfd,0x09,// 3
0xfd,0x34,// 4
0xfd,0x82,// 5
0xfd,0xc8,// 6
0xfe,0x06,//中音7 0x17 (23)
0xfe,0x22,//高音1 0x211. (33)
0xfe,0x56,// 2 2.
0xfe,0x85,// 3 3.
0xfe,0x9a,// 4 4.
0xfe,0xc1,// 高音5 5.
};
uchar code chuntianli[]={
0x78,//曲速0x78, 表示 120 拍/分
0,0x06,2,0x02,3,0x02,5,0x02,
7,0x02,7,0x02,7,0x02,7,0x02,7,0x02,6,0x02,
5,0x06,2,0x02,3,0x02,5,0x02,
7,0x02,7,0x02,7,0x02,7,0x02,7,0x02,6,0x02,
5,0x06,7,0x02,6,0x02,5,0x02,
5,0x02,5,0x02,5,0x02,5,0x02,5,0x02,3,0x02,//第六行
5,0x06,5,0x02,5,0x02,5,0x02,5,0x02,
6,0x02,6,0x02,6,0x02,6,0x02,6,0x02,5,0x02,//第八行
6,0x04,0,0x02,5,0x02,6,0x02,7,0x02,
9,0x02,9,0x02,9,0x02,9,0x02,9,0x02,7,0x02,
7,0x04,0,0x02,5,0x02,6,0x02,7,0x02,
10,0x02,10,0x02,10,0x02,10,0x02,10,0x02,9,0x02,//第十二行
7,0x04,0,0x02,7,0x02,7,0x02,9,0x02,
10,0x02,10,0x02,9,0x02,10,0x02,10,0x02,12,0x02,
12,0x04,0,0x02,12,0x02,12,0x02,10,0x02,
13,0x02,13,0x02,13,0x02,13,0x02,13,0x02,12,0x02,//十六行
13,0x04,0,0x02,9,0x02,10,0x02,12,0x02,
0x22,0x04,0x23,0x04,0x00,0x04,
0,0x06,9,0x02,10,0x02,12,0x02,
13,0x04,14,0x04,0,0x04,//第二十行
0,0x06,14,0x04,13,0x04,5,0x04,
13,0x04,10,0x04,0,0x04,
0,0x06,10,0x04,12,0x04,10,0x04,
13,0x04,9,0x04,0,0x04, //第二十四行
0,0x06,16,0x02,16,0x02,13,0x02,
13,0x04,14,0x04,0,0x04,
0,0x06,14,0x02,13,0x02,5,0x02,
13,0x04,14,0x04,0,0x04, //第二十八行
0,0x06,14,0x02,13,0x02,12,0x02,
13,0x04,10,0x04,0,0x04,
0,0x06,10,0x02,12,0x02,10,0x02,
13,0x0c,
13,0x02,0,0x04,2,0x02,3,0x02,12,0x02,
5,0x0c,
13,0x02,12,0x04,10,0x02,
12,0x0c,
5,0x08,0,0x04,
0xff,
};
void delayms (int ms)
{
uchar a,b,c;
while(ms--)
{
for(c=1;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
}
void delay(int pai)
{
uchar a,b,c;
while(pai--)
{
for(c=1;c>0;c--)
for(b=28;b>0;b--)
for(a=110;a>0;a--);
}
}
void timer0()interrupt 1
{
if(key_sound==0)
{
TL0=music_l;
TH0=music_h;
beep=~beep;
}
}
void play ()
{
uint qusu ;
uint pai2;
qusu = chuntianli[0];
which=2;
while(chuntianli[which]!=0xff)
{
tone =chuntianli[which];
beat = chuntianli[which+1];
which = which + 2;
tone_l = tone%16; //低四位调
tone_h = tone/16; //高四位 是低音0 中音1 高音2
if(tone != 0)
{
/* if(tone_h==0 && tone_l==4)
{
music_h = chuzhi[1];
music_l = chuzhi[2];
}
else if(tone_h==0 && tone_l==5)
{
music_h = chuzhi[3];
music_l = chuzhi[4];
}
else if(tone_h==0 && tone_l==6)
{
music_h = chuzhi[5];
music_l = chuzhi[6];
}
else if(tone_h==0&&tone_l==7)
{
music_h = chuzhi[7];
music_l = chuzhi[8];
}
else if(tone_h==1&&tone_l==11)
{
music_h = chuzhi[9];
music_l = chuzhi[10];
}
else if(tone_h==1&&tone_l==12)
{
music_h = chuzhi[11];
music_l = chuzhi[12];
}
else if(tone_h==1&&tone_l==13)
{
music_h = chuzhi[13];
music_l = chuzhi[14];
}
else if(tone_h==1&&tone_l==14)
{
music_h = chuzhi[tone+4];
music_l = chuzhi[tone+5];
}
else if(tone_h==1&&tone_l==15)
{
music_h = chuzhi[15];
music_l = chuzhi[16];
}
else if(tone_h==1&&tone_l==0)
{
music_h = chuzhi[17];
music_l = chuzhi[18];
}
else if(tone_h==1&&tone_l==1)
{
music_h = chuzhi[19];
music_l = chuzhi[20];
}
else if(tone_h==2&&tone_l==5)
{
music_h = chuzhi[21];
music_l = chuzhi[22];
}
else if(tone_h==2&&tone_l==6)
{
music_h = chuzhi[23];
music_l = chuzhi[24];
}
else if(tone_h==2&&tone_l==7)
{
music_h = chuzhi[25];
music_l = chuzhi[26];
}
else if(tone_h==2&&tone_l==8)
{
music_h = chuzhi[27];
music_l = chuzhi[28];
}
else if(tone_h==2&&tone_l==9)
{
music_h = chuzhi[29];
music_l = chuzhi[30];
}*/
TR0 = 1; /*给初值赋值后开启定时器0 蜂鸣器开始发声 */
out = 1;
}
else
{
P0 = 0x00;
flash = 0;
}
pai2 = (2400/qusu)*beat;
delay(pai2);
delayms(39);
}
}
void main()
{
P0=0x00;
P2=0xff;
TMOD=0x11;
IE=0x8a;
PT0=1;
while(1)
{
if(key_sound==0)
{
TR0=1;
TR1=1;
play();
}
}