#include "timer.h"
unsigned short songfreq[][8]={
//低
{0, 262, 294, 330, 349, 392, 440, 494 },//0
{0, 277, 311, 330, 370, 415, 466, 494 },//1
//中
{0, 523, 578, 659, 698, 784, 880, 988 },//2
{0, 554, 622, 659, 740, 831, 932, 988 },//3
//高
{0, 1046, 1175, 1318, 1397, 1568, 1760, 1976},//4
{0, 1109, 1245, 1318, 1480, 1661, 1865, 1976} //5
};
struct songdata
{
int tone; //音调
int freq; // 音色
int time; // 该音的长短
};
struct songdata xiaopg[] = {
{5,3,40},{5,1,40},{5,2,40},{2,6,40},
{5,3,20},{5,2,20},{5,1,20},{5,2,20},{2,6,80},
{5,3,40},{5,1,40},{5,2,40},{5,2,40},
{5,5,20},{5,3,20},{2,7,40},{5,1,40},{5,1,20},{2,7,20},
{2,6,40},{2,7,20},{5,1,20},{5,2,40},{2,5,40},
{5,6,20},{5,5,20},{5,3,40},{5,3,40},{5,3,20},{5,2,20},
{5,1,40},{5,2,20},{5,3,20},{5,2,20},{5,3,20},{5,2,20},{5,5,20},
{5,5,160}
};
int songindex, songplaytime, songfreq1, songtone;
volatile int playsongflag;
int Playsong(void)
{
if(songplaytime)
{
songplaytime--;
}
else
{
if(songplaytime == 0)
{
if(songindex >= (sizeof(xiaopg)/sizeof(struct songdata)))
{
Stopplaysong();
return 0;
}
}
stop_buzzer();
songplaytime = xiaopg[songindex].time;
songfreq1 = xiaopg[songindex].freq;
songtone = xiaopg[songindex].tone;
if(songfreq)
{
start_buzzer(songfreq[songtone][songfreq1]);
}
++songindex;
} //end else
}
void Startplaysong(void)
{
songplaytime = 0;
songindex = 0;
playsongflag = 1;
}
void Stopplaysong(void)
{
playsongflag = 0;
songplaytime = 0;
songindex = 0;
stop_buzzer();
}