音乐门铃c语言程序

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/***************************************************************************
音乐门铃 is from DOORBell4. Four musics are played sequently.A button could
be used to control the play procedure(not added yet).
mcu@
2008.5.1
***************************************************************************/
/*******************************head files*********************************/
#include

/********************definition for symbolic constants or pins*************/
sbit BUZZ=P1^2;
sbit LED=P1^0;

/************************declaration for function prototype****************/
void SystemInit();

void sound(unsigned int frequency);
void nosound();
void PlayMusic(unsigned char* MusicDataPtr);

void Delay(unsigned int ms);
void Delay1ms();

/************************declaration for constant array********************/
unsigned int code SoundFreqTable[]=
{
0, 131, 147, 165, 175, 196, 220, 247,0,0,
0, 262, 294, 330, 349, 392, 440, 494,0,0,
0, 523, 587, 659, 698, 784, 880, 988,0,0,
0,1047,1175,1319,1397,1568,1760,1976,0,0,
0,2094,2350
};

unsigned char code PHONE_RING[]=
{
27,1,27,1,27,1,27,1,27,1,27,1,27,1,27,1,27,1,27,1,27,1,27,1,27,1,
0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,
27,1,27,1,27,1,27,1,27,1,27,1,27,1,27,1,27,1,27,1,27,1,27,1,27,1,
0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,
0
};

unsigned char code SOUND1_7[]=
{
11,10,12,10,13,10,14,10,15,10,16,10,17,10,21,10,0xff,1,0xff,1,0xff,1,0xff,1,
21,10,17,10,16,10,15,10,14,10,13,10,12,10,11,10,0xff,1,0xff,1,0xff,1,0xff,1,
21,10,22,10,23,10,24,10,25,10,26,10,27,10,31,10,0xff,1,0xff,1,0xff,1,0xff,1,
31,10,27,10,26,10,25,10,24,10,23,10,22,10,21,10,0xff,1,0xff,1,0xff,1,0xff,1,
0
};

unsigned char code DI_DI_DI[]=
{
27,1,0xee,1,27,1,0xee,1,27,1,0xee,1,27,1,0xee,1,0xff,1,0xff,1,
27,1,0xee,1,27,1,0xee,1,27,1,0xee,1,27,1,0xee,1,0xff,1,0xff,1,
0
};

unsigned char code DaChangJin[]=
{
22,15,23,15,23,15,23,30,22,5,21,10,0xff,1,
16,15,21,15,21,15,21,30,0xff,1,0xff,1,
0
};

unsigned char code TWO_TIGER[]=
{
21,10,22,10,23,10,21,10,21,10,22,10,23,10,21,10,23,10,24,10,25,20,23,10,24,10,25,20,
0
};

unsigned char code Sakura[]=
{
16,10,17,10,21,10,17,10,16,10,17,5,16,5,14,20,13,10,11,5,12,5,13,10,14,10,13,10,13,5,11,5,7,20,
0
};

unsigned char code DOOR_BELL[]=
{
27,20,25,20,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,
27,20,25,20,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,0xff,1,
0
};
/************************declaration for global variable*******************/
unsigned int FreqTiming,TimerCount=0;
unsigned char TH0Buff,TL0Buff;
/************************* interrupt service routines**********************/
void T0ISR(void) interrupt 1
{
TH0=TH0Buff;
TL0=TL0Buff;
BUZZ=~BUZZ;
}

/******************************main function*******************************/
void main()
{

Sys

temInit();
sound(1500);
Delay(1000);
nosound();
Delay(3000);
while(1)
{
PlayMusic(SOUND1_7);
PlayMusic(DI_DI_DI);
PlayMusic(DaChangJin);
PlayMusic(PHONE_RING);
PlayMusic(TWO_TIGER);
PlayMusic(Sakura);
PlayMusic(DOOR_BELL);
}
}

/********************************sub-functions*****************************/
void SystemInit()
{
TMOD=0x01;
ET0=1; //enable T0 interrupt
EA=1;
}

void sound(unsigned int frequency)
{
FreqTiming=460800/frequency; //e.g for 1Khz,FreqTiming=20,

TH0Buff=(65536-FreqTiming)>>8;
TL0Buff=(65536-FreqTiming)&0xff;

TH0=TH0Buff;
TL0=TL0Buff;

TR0=1; //Buzzer will be toggled each 500us
}

void nosound()
{
TR0=0;
BUZZ=1; //buzzer power down
}

void PlayMusic(unsigned char *MusicDataPtr)
{
LED=0;
while(1)
{
switch(*MusicDataPtr)
{
case 0: nosound(); LED=1;return;
case 0xff: nosound(); Delay(200); MusicDataPtr++; break;
case 0xee: nosound(); MusicDataPtr++; break;
default: sound(SoundFreqTable[*MusicDataPtr]);MusicDataPtr++;break;
}
Delay(20*(*MusicDataPtr));
MusicDataPtr++;
nosound();
Delay(24);
}
}

void Delay(unsigned int ms)
{
do
{
Delay1ms();
} while(--ms);
}

void Delay1ms()
{
unsigned char i;

for(i=151;i;i--)
;
}
/********************************end of program****************************/

相关文档
最新文档