昨晚复习不动了 写了个程序玩玩~
#include <reg52.h>
sbit spk = p0^0;
#define halfnotetime 600
struct note
{
int name;
int time;
};
struct note code text[] = {{5, 1}, {10, 1}, {9, 1}, {8, 1}, {5, 4}, {5, 1}, {10, 1}, {9, 1}, {8, 1}, {6, 4}, {6, 1}, {11, 1}, {10, 1}, {9, 1}, {7,3}, {5, 1}, {12, 1}, {12, 1}, {11, 1}, {9, 1}, {10, 4}, {5, 1}, {10, 1}, {9, 1}, {8, 1}, {5, 4}, {5, 1}, {10, 1}, {9, 1}, {8, 1}, {6, 4}, {6, 1}, {11, 1}, {10, 1}, {9, 1}, {12,1}, {12, 1}, {12, 2}, {13, 1}, {12, 1}, {11, 1}, {9, 1}, {8, 4}, {10, 1}, {10, 1}, {10, 2}, {10, 1}, {10, 1}, {10, 2}, {10, 1}, {12, 1}, {8, 1}, {9, 1}, {10, 4}, {11, 1}, {11, 1}, {11, 2}, {11, 1}, {10, 1}, {10, 2}, {10, 1}, {9, 1}, {9, 1}, {8, 1}, {9, 2}, {12, 2}, {10, 1}, {10, 1}, {10, 2}, {10, 1}, {10, 1}, {10, 2}, {10, 1}, {12, 1}, {8, 1}, {9, 1}, {10, 4}, {11, 1}, {11, 1}, {11, 2}, {11, 1}, {10, 1}, {10, 2}, {12, 1}, {12, 1}, {11, 1}, {9, 1}, {8, 2}, {15, 2}, {0,2}, {-1,0}};
unsigned char code noteh[]={0, 249, 249, 250, 250, 251, 251, 252, 252, 252, 253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 255};
unsigned char code notel[]={0, 34, 225,140, 216, 105, 233, 91, 143, 226, 68, 108, 179, 243, 46, 71, 120, 162, 182, 218, 250, 23};
unsigned char timerh0, timerl0;
void delay(int t)
{
int i;
char j;
for(i=0; i<t; i++)
{
for(j=0; j<110; j++);
}
}
void osc() interrupt 1
{
spk = ~spk;
th0 = timerh0;
tl0 = timerl0;
}
void play(struct note text[])
{
int i=0;
while(text.name != -1)
{
if(text.name == 0)
{
delay(halfnotetime*text.time);
}
else
{
timerh0 = noteh[text.name];
timerl0 = notel[text.name];
tr0 = 1;
delay(halfnotetime*0.9*text.time);
tr0 = 0;
delay(halfnotetime*0.1*text.time);
}
i++;
}
}
void main()
{
tmod = 0x01;
ea = 1;
et0 = 1;
while(1)
{
play(text);
}
}