#include "Include.h"
//音调表 //C 262 //D 294 //E 330 //F 349 //G 392 //A 440 //B 494 //C 523 //D 587 //E 659 //F 698 //G 784 //A 880 //B 988 //C 1047 //D 1175 //E 1319 //F 1397 //G 1568 //A 1760 //B 1976
//低音1~7 const unsigned int MusicDataD[7] = {61069,54422,48485,45845,40816,36364,32389}; //中音1~7 const unsigned int MusicData[7] = {30592,27257,24297,22922,20408,18182,16194}; //高音1~7 const unsigned int MusicDataG[7] = {15282,13617,12130,11453,10204,9091,8097};
//音乐数据格式: //0x11~0x17 表示低音1~7 //0x21~0x27 表示中音1~7 //0x31~0x37 表示高音1~7 //0 表示静音 //每个音后面数据表示该音为多少个时间单位:1个16分音符定为1个时间单位
const unsigned char SuperStarData[] = //SuperStar S.H.E. { 0x22,4,0x22,2,0x23,2,0x21,4,0x15,2,0x15,2, 0x22,2,0x21,2,0x22,2,0x23,2,0x21,4,0,2,0x23,2, 0x24,2,0x23,2,0x21,2,0x21,1,0x22,1,0x22,4,0,2,0x23,2, 0x24,2,0x23,2,0x21,2,0x21,2,0x21,2,0x22,1,0x21,1,0x17,4, 0x22,2,0x22,2,0x22,2,0x23,2,0x21,4,0x15,2,0x15,2, 0x22,2,0x21,2,0x22,2,0x23,2,0x21,4,0,2,0x23,2, 0x24,2,0x23,2,0x21,2,0x21,1,0x22,1,0x22,4,0,2,0x23,2, 0x24,2,0x23,2,0x24,2,0x25,2,0x25,4,0x15,2,0x16,2, 0x21,4,0x22,2,0x23,2,0x25,4,0x23,2,0x25,2, 0x26,2,0x27,2,0x26,2,0x25,2,0x23,4,0x22,2,0x23,2, 0x25,2,0x23,2,0,2,0x23,2,0x22,2,0x23,2,0x22,2,0x21,2, 0x15,8,0,4,0x15,2,0x16,2, 0x21,4,0x22,2,0x23,2,0x25,4,0x23,2,0x25,2, 0x26,2,0x27,2,0x26,2,0x25,2,0x23,4,0x22,2,0x23,2, 0x25,2,0x23,2,0,2,0x23,2,0x22,2,0x23,2,0x22,2,0x21,2, //1 0x15,16, 0,32, 0x22,4,0x22,2,0x23,2,0x21,4,0x15,2,0x15,2, 0x22,2,0x21,2,0x22,2,0x23,2,0x21,4,0,2,0x23,2, 0x24,2,0x23,2,0x21,2,0x21,1,0x22,1,0x22,4,0,2,0x23,2, 0x24,2,0x23,2,0x21,2,0x21,2,0x21,2,0x22,1,0x21,1,0x17,4, 0x22,2,0x22,2,0x22,2,0x23,2,0x21,4,0x15,2,0x15,2, 0x22,2,0x21,2,0x22,2,0x23,2,0x21,4,0,2,0x23,2, 0x24,2,0x23,2,0x21,2,0x21,1,0x22,1,0x22,4,0,2,0x23,2, 0x24,2,0x23,2,0x24,2,0x25,2,0x25,4,0x15,2,0x16,2, 0x21,4,0x22,2,0x23,2,0x25,4,0x23,2,0x25,2, 0x26,2,0x27,2,0x26,2,0x25,2,0x23,4,0x22,2,0x23,2, 0x25,2,0x23,2,0,2,0x23,2,0x22,2,0x23,2,0x22,2,0x21,2, 0x15,8,0,4,0x15,2,0x16,2, 0x21,4,0x22,2,0x23,2,0x25,4,0x23,2,0x25,2, 0x26,2,0x27,2,0x26,2,0x25,2,0x23,4,0x22,2,0x23,2, 0x25,2,0x23,2,0,2,0x23,2,0x22,2,0x23,2,0x22,2,0x21,2,
//2 0x15,16, 0,4,0x26,2,0x25,6,0x22,2,0x23,10,0,8, 0,4,0x26,2,0x25,6,0x22,2,0x23,4,0x24,2,0x23,1,0x22,1,0x21,1,0x21,9, 0,4,0x26,2,0x25,6,0x27,2,0x27,6,0,2,0x27,2,0x31,4,0x27,2,0x26,2, 0x26,4,0x22,2,0x23,2,0x21,4,0x15,2,0x15,2, 0x22,2,0x21,2,0x22,2,0x23,2,0x21,4,0,2,0x23,2, 0x24,2,0x23,2,0x21,2,0x22,2,0x22,4,0,2,0x23,2,
0x24,2,0x23,2,0x21,2,0x21,2,0x21,2,0x22,1,0x21,1,0x17,4, 0x22,2,0x22,2,0x22,2,0x23,2,0x21,4,0x15,2,0x15,2, 0x22,2,0x21,2,0x22,2,0x23,2,0x21,4,0,2,0x23,2, 0x24,2,0x23,2,0x21,2,0x22,2,0x22,4,0,2,0x23,2, 0x24,2,0x23,2,0x24,2,0x25,2,0x25,20,0x15,2,0x16,2,
0x21,4,0x22,2,0x23,2,0x25,4,0x23,2,0x25,2, 0x26,2,0x27,2,0x26,2,0x25,2,0x23,4,0x22,2,0x23,2, 0x25,2,0x23,2,0,2,0x23,2,0x22,2,0x23,2,0x22,2,0x21,2, 0x15,8,0,4,0x15,2,0x16,2, 0x21,4,0x22,2,0x23,2,0x25,4,0x23,2,0x25,2, 0x26,2,0x27,2,0x26,2,0x25,2,0x23,4,0x22,2,0x23,2, 0x25,2,0x23,2,0,2,0x23,2,0x22,2,0x23,2,0x22,2,0x21,2, 0x15,12,0,4, };
void SetBellF(unsigned int SetV) { TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseInitStructure.TIM_Period = SetV; TIM_TimeBaseInitStructure.TIM_Prescaler = 0; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = (SetV/2); TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC2Init(TIM2, &TIM_OCInitStructure); }
void Timer2Init(void) { TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseInitStructure.TIM_Period = 10000; TIM_TimeBaseInitStructure.TIM_Prescaler = 0; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 5000; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC2Init(TIM2, &TIM_OCInitStructure); }
void Timer2OutEnable(void) { GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); }
void Timer2OutDisable(void) { GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_Init(GPIOA, &GPIO_InitStructure); }
void Timer2Start(void) { TIM_Cmd(TIM2, ENABLE); }
void Timer2Stop(void) { TIM_Cmd(TIM2, DISABLE); }
//播放歌曲 void PlayMusicFile(unsigned int DataNumber,unsigned char* p,unsigned char UnitTime) { unsigned int i = 0; unsigned int PlayWord;
for(i=0;i<DataNumber;i++) { switch (*p) { case 0x11: PlayWord = MusicDataD[0]; break; case 0x12: PlayWord = MusicDataD[1]; break; case 0x13: PlayWord = MusicDataD[2]; break; case 0x14: PlayWord = MusicDataD[3]; break; case 0x15: PlayWord = MusicDataD[4]; break; case 0x16: PlayWord = MusicDataD[5]; break; case 0x17: PlayWord = MusicDataD[6]; break; case 0x21: PlayWord = MusicData[0]; break; case 0x22: PlayWord = MusicData[1]; break; case 0x23: PlayWord = MusicData[2]; break; case 0x24: PlayWord = MusicData[3]; break; case 0x25: PlayWord = MusicData[4]; break; case 0x26: PlayWord = MusicData[5]; break; case 0x27: PlayWord = MusicData[6]; break; case 0x31: PlayWord = MusicDataG[0]; break; case 0x32: PlayWord = MusicDataG[1]; break; case 0x33: PlayWord = MusicDataG[2]; break; case 0x34: PlayWord = MusicDataG[3]; break; case 0x35: PlayWord = MusicDataG[4]; break; case 0x36: PlayWord = MusicDataG[5]; break; case 0x37: PlayWord = MusicDataG[6]; break; default: PlayWord = 0;//表示无声音 break; } p++; i++; if(PlayWord != 0) { Timer2OutEnable(); SetBellF(PlayWord); } else { Timer2OutDisable(); } DelayNmS(UnitTime*(*p)); p++;
} }
void Timer2Test(void) { unsigned char i = 0;
Timer2Start();
while(1) { while(NO_KEY == GetKey()); PlayMusicFile(sizeof(SuperStarData),(unsigned char*)SuperStarData,100); Timer2OutDisable(); }
//一句简单的歌曲 while(0) { Timer2OutEnable(); SetBellF(MusicData[0]); DelayNmS(400); SetBellF(MusicData[0]); DelayNmS(400); SetBellF(MusicData[4]); DelayNmS(400); SetBellF(MusicData[4]); DelayNmS(400); SetBellF(MusicData[5]); DelayNmS(400); SetBellF(MusicData[5]); DelayNmS(400); SetBellF(MusicData[4]); DelayNmS(400); DelayNmS(400); SetBellF(MusicData[3]); DelayNmS(400); SetBellF(MusicData[3]); DelayNmS(400); SetBellF(MusicData[2]); DelayNmS(400); SetBellF(MusicData[2]); DelayNmS(400); SetBellF(MusicData[1]); DelayNmS(400); SetBellF(MusicData[1]); DelayNmS(400); SetBellF(MusicData[0]); DelayNmS(400); DelayNmS(400); Timer2OutDisable(); DelayNmS(1000); }
//从C播放到B while(1) { SetBellF(MusicData); Timer2OutEnable(); DelayNmS(200); i++; if(i>=7) { i = 0; Timer2OutDisable(); DelayNmS(2000); } } }
|