51单片机演奏
#include "STC15.h"
#include "intrins.h"
code unsigned char fvL[]={0x8c,0x5b,0x15,0x67,0x04,0x90,0x0c,0x44,0xac,0x09,0x34,0x82};
code unsigned char fvH[]={0xf8,0xf9,0xfa,0xfa,0xfb,0xfb,0xfc,0xfc,0xfc,0xfd,0xfd,0xfd};
code unsigned char tone[]={9,8,6,8,8,6,8,8,6,8,6,5,
9,8,6,8,8,6,8,8,10,9,8,8,
5,6,10,10,10,9,10,10,9,10,12,10,10,
10,10,10,10,9,9,9,9,8,9,10,9,
9,8,6,8,8,6,8,8,6,8,6,5,
5,6,10,12,12,10,12,12,10,9,8,8};
code unsigned char beat[]={2,2,2,4,2,2,4,2,2,2,2,4,
2,2,2,4,2,2,4,2,2,2,2,4,
2,2,2,4,2,2,4,2,2,2,2,2,2,
2,2,2,2,2,2,2,4,2,2,4,4,
2,2,2,4,2,2,4,2,2,2,2,4,
2,2,2,4,2,2,4,2,2,2,2,4};
void playtone(unsigned char tone,unsigned char beat);
void main()
{
int i;
TMOD=0x00;
ET0=1;
EA=1;
TR0=1;
for(i=0;i<1000;i++)
{
playtone(tone[i],beat[i]);
}
while(1)
{}
}
void Delay100ms()
{
unsigned char i,j,k;
i=5;
j=52;
k=195;
do
{
do
{
while (--k);
}while (--j);
}while (--i);
}
void Delay20ms()
{unsigned char i, j, k;
i = 1;
j = 234;
k = 113;
do
{
do
{
while (--k);
}while (--j);
}while (--i);
}
void playtone(unsigned char a,unsigned char b)
{
TL0 = fvL[a-1];
TH0 = fvH[a-1];
while(b!=0)
{Delay100ms();Delay20ms();Delay20ms();
b--;}
}
void timer0s() interrupt 1
{ P07 =~P07;}
用fvL和fvH来表示频率的低八位和高八位(用十六进制);
数组tone表示乐谱的音调,数组beat表示节拍;
函数playtone调用tone和beat数组,子函数playtone里的while循环用来控制节拍数(此处用的是每拍140ms);
用中断控制(“interrupt 1”),输出口P07翻转。
这是我之前从博客里找到的音调频率部分对应表,侵删
转载自原文链接, 如需删除请联系管理员。
原文链接:51单片机简单乐曲演奏(青花瓷),转载请注明来源!