我想要一段单片机控制音乐喷泉的C语言程序,很急!~谁能帮帮我,谢谢~

谢谢谢谢~

第1个回答  2012-04-13
#include<reg51.h>
#include "SoundPlay.h"
uchar code table[]="sound_signal:";
uchar code table1[]="sound_pace:";
uchar code table2[]="0123456789 ";
uchar data count=0;
sbit motor=P2^1;
sbit rs=P2^2;
sbit e=P2^3;
void init_com()
{
TMOD=0x01;
TH0=0xff;
TL0=0xff;
EA=1;
ET0=1;
}

void timer0(void) interrupt 1 using 3
{
counter=counter+1;//节拍次数计数
TH0=0xd8; //定义单位节拍的延时大小 10ms定时
TL0=0xef;
}
void delay(uchar n)
{
uchar i;
while(n--)
for(i=0;i<125;i++); //延时1毫秒

}

void sound_delay(uchar n)
{
uchar i;
while(n--)
{
for(i=0;i<2;i++);
}
}

/*******LCD显示*******/
void zhiling(uchar zl) //写指令
{
rs=0;
e=0;
P0=zl;
delay(5);
e=1;
delay(5);
e=0;
}
void shuju(uchar sj) //写数据
{
rs=1;
e=0;
P0=sj;
delay(5);
e=1;
delay(5);
e=0;
}
void lcdinit() //初始化
{
delay(15);
zhiling(0x01);
zhiling(0x38);
delay(5);
zhiling(0x38);
delay(5);
zhiling(0x38);
delay(5);
zhiling(0x0c);
delay(5);
zhiling(0x06);
delay(5);
zhiling(0x01);
delay(5);
}
//*****************液晶初始化*********
void main()
{
uint i,a;
uchar sound_signal;//定义音符大小
uchar sound_pace;//定义节拍大小
init_com();
lcdinit();
zhiling(0x80);
for (i=0;i<13;i++)
shuju(table[i]);
zhiling(0x80+0x40);
for (i=0;i<11;i++)
shuju(table1[i]);
//array[i]=0x00 代表歌曲演唱完毕
//array[i]=0xff 代表是休止符
while(1)
{
i=0;

/***************************************///////////
while(array[i]!=0x00)
{
//如果是休止符,延时100ms,并终止本次循环,进入下一个循环
if(array[i]==0xff)
{
TR0=0;
i++;
delay(100);
continue;
}
//从表中取得 音符大小
sound_signal=array[i];
i=i+1;
//从表中取得 节拍大小
sound_pace=array[i];
a= sound_pace;
P1=a|(a>>4);
//彩灯的根据节拍闪烁
zhiling(0x80+13);
if(sound_signal/100==0)
shuju(table2[10]);
else shuju(table2[ sound_signal/100]);
shuju(table2[sound_signal%100/10]);
shuju(table2[sound_signal%10]);
zhiling(0x80+0x40+11);
if(sound_signal/100==0)
shuju(table2[10]);
else shuju(table2[sound_pace/100]);
shuju(table2[sound_pace%100/10]);
shuju(table2[sound_pace%10]);
motor=0;
delay(40);
TR0=1;
//当节拍数未达到时候,继续循环,产生该音调的声音
while(counter<=sound_pace)
{
motor=1;
sound=~sound;
sound_delay(sound_signal);
}

i++;
counter=0;//节拍计数器置0,进入下一个音调
}
/*************************************************/
delay(10); //歌曲演唱完毕后,延时一段时间
}
}
//*******soundplay.h**********歌曲
#ifndef __SOUNDPLAY_H_REVISION_FIRST__
#define __SOUNDPLAY_H_REVISION_FIRST__
#define uchar unsigned char
#define uint unsigned int
sbit sound=P2^0;
uint counter=0;
uchar code array[]=
{
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
};
第2个回答  2008-11-03
单片机的程序是要根据它的电路来写的,你起码也要把你的原理图给出来,才能写程序。本回答被网友采纳
第3个回答  2008-11-03
这么提问让人一头雾水-_-!
相似回答