#include "reg52.h"
#include "intrins.h"
sbit SPK = P3^4;
sbit LED_L = P3^7;
sbit LED_M = P3^6;
sbit LED_H = P3^5;
sbit KEY = P3^3;
#define KEYDELAY 20
#define KEY_PORT P2
#define LED_PORT P1
bit FT_1ms,FT_250ms;
bit F_PlaySong,F_PlayTone,F_KeyDelay,F_Tone,F_KeyUp,F_Music_Ide;
unsigned char Freq_H,Freq_L,SongLen,PlayIde,i;
unsigned char KeyBuf,KeySure,KeyDelay_Cnt,Timer1_Cnt,KeyDelay_Cnt1;
unsigned char k,Time,PortTemp;
//世上只有妈妈好数据表
code unsigned char MUSIC[]={6,2,6, 5,2,2, 3,2,4, 5,2,4, 1,3,4, 6,2,2, 5,2,2,
6,2,8, 3,2,4, 5,2,2, 6,2,2, 5,2,4, 3,2,4, 1,2,2,
6,1,2, 5,2,2, 3,2,2, 2,2,8, 2,2,6, 3,2,2, 5,2,4,
5,2,2, 6,2,2, 3,2,4, 2,2,4, 1,2,8, 5,2,6, 3,2,2,
2,2,2, 1,2,2, 6,1,2, 1,2,2, 5,1,12, 0,0,0
};
/***********************生日快乐**********************/
code unsigned char MUSIC1[]={
5,1,2, 5,1,2, 6,1,4, 5,1,4, 1,2,4, 7,1,8,
5,1,2, 5,1,2, 6,1,4, 5,1,4, 2,2,4, 1,2,8,
5,1,2, 5,1,2, 5,2,4, 3,2,4, 1,2,4, 7,1,4,
6,1,8, 4,2,2, 4,2,2, 3,2,4, 1,2,4, 2,2,4,
1,2,12, 0,0,0};
// 音阶频率表 高八位
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 TimerInit(void)
{
TMOD = 0x11;
TH1 = (65536 - 1000) / 256;
TL1 = (65536 - 1000) % 256;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
EA = 1;
}
void ScanKey(void)
{
if(F_KeyDelay)
{
KeyDelay_Cnt --;
if(KeyDelay_Cnt == 0)
{
F_KeyDelay = 0;
KeySure = KeyBuf;
F_KeyUp = 1;
}
}
if(KeyBuf != KEY_PORT)
{
KeyBuf = KEY_PORT;
KeyDelay_Cnt = KEYDELAY;
F_KeyDelay = 1;
}
if(!KEY)
{
KeyDelay_Cnt1 --;
if(KeyDelay_Cnt1 == 0)
{
if(F_PlaySong == 0)
{
F_Music_Ide = !F_Music_Ide;
F_PlaySong = 1;
i = 0;
}
}
}
else
{
KeyDelay_Cnt1 = KEYDELAY;
}
if(PlayIde == 1)
{
LED_L = 1;
LED_M = 0;
LED_H = 1;
}
if(PlayIde == 2)
{
LED_L = 1;
LED_M = 1;
LED_H = 0;
}
if(PlayIde == 0)
{
LED_L = 0;
LED_M = 1;
LED_H = 1;
}
}
void main(void)
{
TimerInit();
F_PlayTone = 0;
PortTemp = 0x7f;
for(i = 0;i < 7;)
{
if(FT_250ms)
{
FT_250ms = 0;
i++;
PortTemp = _crol_(PortTemp,1);
LED_PORT= PortTemp;
}
}
for(i = 0;i < 7;)
{
if(FT_250ms)
{
FT_250ms = 0;
i++;
PortTemp = _cror_(PortTemp,1);
LED_PORT= PortTemp;
}
}
LED_PORT = 0xff;
i = 0;
while(1)
{
if(FT_1ms)
{
FT_1ms = 0;
WDT_CONTR = 0x38;
ScanKey();
if(F_PlaySong)
{
if(FT_250ms)
{
FT_250ms = 0;
if(Time > 0)
{
Time --;
}
else
{
PortTemp = 0x01;
F_PlayTone = 1;
if(F_Music_Ide)
{
k = MUSIC + 7 * MUSIC[i + 1] - 1;//去音符振荡频率所需数据
PlayIde = MUSIC[i + 1] - 1;
PortTemp <<= (MUSIC-1);
Time = MUSIC[i+2] - 1; //节拍时长
if(! MUSIC)
{
i = 0;
F_PlaySong = 0;
LED_PORT = 0xff;
F_PlayTone = 0;
SPK = 1;
PlayIde = 1;
Time = 4;
}
}
else
{
k = MUSIC1 + 7 * MUSIC1[i + 1] - 1;//去音符振荡频率所需数据
PlayIde = MUSIC1[i + 1] - 1;
PortTemp <<= (MUSIC1-1);
Time = MUSIC1[i+2] - 1; //节拍时长
if(! MUSIC1)
{
i = 0;
F_PlaySong = 0;
LED_PORT = 0xff;
F_PlayTone = 0;
SPK = 1;
PlayIde = 1;
Time = 4;
}
}
PortTemp = ~PortTemp;
LED_PORT = PortTemp;
Freq_H = FREQH[k];
Freq_L = FREQL[k];
i = i + 3;
}
}
}
else
{
switch(KeySure)
{
case 0x81: F_Tone = 0;F_PlayTone = 1; Timer1_Cnt = 0;k=0; LED_PORT = 0xfe;
break;
case 0x41: F_Tone = 0;F_PlayTone = 1; Timer1_Cnt = 0;k=1; LED_PORT = 0xfd;
break;
case 0x21: F_Tone = 0;F_PlayTone = 1; Timer1_Cnt = 0;k=2; LED_PORT = 0xfb;
break;
case 0x11: F_Tone = 0;F_PlayTone = 1; Timer1_Cnt = 0;k=3; LED_PORT = 0xf7;
break;
case 0x09: F_Tone = 0;F_PlayTone = 1; Timer1_Cnt = 0;k=4; LED_PORT = 0xef;
break;
case 0x05: F_Tone = 0;F_PlayTone = 1; Timer1_Cnt = 0;k=5; LED_PORT = 0xdf;
break;
case 0x03: F_Tone = 0;F_PlayTone = 1; Timer1_Cnt = 0;k=6; LED_PORT = 0xbf;
break;
case 0x00:
if(F_KeyUp)
{
F_KeyUp = 0;
PlayIde++;
if(PlayIde == 3)
{
PlayIde = 0;
}
}
break;
default:
break;
}
Freq_H = FREQH[(PlayIde * 7) + k];
Freq_L = FREQL[(PlayIde * 7) + k];
if(F_Tone)
{
F_Tone = 0;
F_PlayTone = 0;
|