网上找的,自己看看吧
#include
sbit SPK=P1^2;
unsigned char Timer0_H,Timer0_L,Time;
code unsigned char MUSIC[]={ 0,0,6,1, 5,6,1,1, 5,6,4,3, 2,2,2,5, 5,2,4,3, 1,2,4,4, 6,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 DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
/*------------------------------------------------
节拍延时函数
各调1/4节拍时间:
调4/4 125ms
调2/4 250ms
调3/4 187ms
------------------------------------------------*/
void delay(unsigned char t)
{
unsigned char i;
for(i=0;i
TR0=0;
}
void TIM0_ISR() interrupt 1
{
TR0=0;
SPK=!SPK;
TH0=Timer0_H;
TL0=Timer0_L;
TR0=1;
}
void Song()
{
TH0=Timer0_H;//赋值定时器时间,决定频率
TL0=Timer0_L;
TR0=1; //打开定时器
delay(Time); //延时所需要的节拍
}
void main(void)
{
unsigned char k,i;
TMOD|=0x01;
EA=1;
ET0=1;
while(1)
{
i=0;
while(i<100)
{
k=MUSIC[i]+7*MUSIC[i+1]-1;
Timer0_H=FREQH[k];
Timer0_L=FREQL[k];
Time=MUSIC[i+2];
i=i+3;
Song();
}
}
}
这里有单片机音乐演奏的制作实例
http://hi.baidu.com/dzkfw/blog/item/54a6d051dbd4167c853524b1.html
这个可以做
什么时候要?
分有点少