这是用单片机实现乐曲的输出程序 运行时有错误 但我已经检查不出错误了 另外一下程序也有不懂的地方,各位高手 帮帮忙 看看 如何改正 如何实现 急啊 谢了
#include
#include
#define uchar unsigned char
#define uint unsigned int
void PORT_Init(void)
{
P2MDOUT=0xFF;
XBR2=0x40;
}
soundoff(void)
{
TR0=0;ET0=0;
}
unit code notefreq[]={21_6_11_6_1_1_3_2_1_1;} //这四句是写简谱的吗?格式对吗?
unit code notename[]={3_3_3_2_2_2_2_2_1_3_2;}
unit code half note freq[]={5_6_3_5_5_3_5_5_3_2_2_1_1;}
unit code half note name[]={2_1_6_1_1_6_1_1_6_1_6_5;}
unit GetFreq (uchar ch,ucharflg) // 这是干什么用的啊 不太明白
{
uchar *pn,i=0;
unit *pf;
if(flg){pn=halfnotename;pf=halfnotefreq;}
else {pn=notename;pf=notefreq;}
while(1)
{
if(pn==0)return 0;
if(ch==pn)return pf;
i++;
}
}
void play(char *str)
{
uchar i=0,ch,halfflg=0;
uchar lasttime;
uint freq;
WDTCN=0xDE;
WDTCN=0xAD;
PORT_Init();
OSCICN=0x14;
while(1) // 这个循环 是乐曲的播放吗? 怎么看不懂啊 ???
{
for (;;i++)
{
ch=str;
if((ch==' ')||(ch=='|')||(ch=='\n')||(ch=='\r')) {i++;continue;}
if(!ch) {soundoff();return;}
if(ch=='#'){halfflg=1;continue;}
if(isdigit(ch)||isalpha(ch))
{
freq=GetFreq(ch,halfflg);
lasttime=16;break;
}
else {halfflg=0;continue;}
}
i++;
ch=str;
while(1)
{
if(!ch)break;
if(isdigit(ch)||isalpha(ch))break;
if(ch='-')lasttime+=8;
if(ch='-')lasttime+=4;
if(ch='_')lasttime/=2;
if(ch='=')lasttime/=4;
i++;
ch=str;
if(freq!=0)sound(freq);
else sound off();
delay(1);
}
}
}
void sound (unit freq)
{
unit timereg;
timereg=65536-(OSKREQ/(24(*freq)));
freqsound H= timereg/256;
freqsound L= timereg&0x00ff;
TR0=1;ET0=1;
}
void delay(uchar time)
{
uchar i;
uint j;
for(i=0;i
for(j=0;j<0x100;j++)
}
void main(void) // 主函数 调用的怎么那么难受啊
{ int c;
TMOD=0x01;
TR0=0;
ET0=0;
EA=1;
for (c=0;ch!=0;c++)
{ch=0;}
while(1)
{ play();}
}
void timer0int() interrupt 1
{
TH0=freqsound H;
TL0=freqsound L;
P5=P5^0x10;
}