單片機(jī)報(bào)警聲
兩個(gè)簡(jiǎn)單的報(bào)警程序,希望能對(duì)大家有所啟發(fā),讓單片機(jī)發(fā)出更美妙的音響
/***********************************************
* 標(biāo)題: 簡(jiǎn)單報(bào)警聲程序
* 日期: 2008-4-30
* yongsheng.gys@163.com
***********************************************/
#include
#include
sbit SPK = P3^4;
unsigned char frq;
void delayms(unsigned char ms)
// 延時(shí)子程序
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
void timer0() interrupt 1 using 1
{
TH0 = 0xfe;
TL0 = frq;
SPK = ~SPK;
}
main()
{
TMOD = 0x01;
frq = 0x00;
TH0 = 0x00;
TL0 = 0xff;
TR0 = 1;
IE = 0x82;
while(1)
{
frq++;
delayms(1); //改變此處可以改變報(bào)警聲的節(jié)奏長(zhǎng)度
}
}
/***********************************************
* 標(biāo)題: 單片機(jī)演奏消防車(chē)的報(bào)警聲音
* 日期: 2008-4-30
* yongsheng.gys@163.com
***********************************************/
#include
#include
sbit SPK = P3^4; //喇叭接到P3.4上
unsigned char frq;
unsigned char flag;
void delayms(unsigned char ms)
// 延時(shí)子程序
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
void timer0() interrupt 1 using 1
{
TH0 = 0xfe;
TL0 = frq;
SPK = ~SPK;
}
void main()
{
TMOD = 0x01;
frq = 0x00;
TH0 = 0x00;
TL0 = 0xff;
TR0 = 1;
IE = 0x82;
flag=0;
while(1)
{
switch(flag)
{ case 0:frq++;break; //flag標(biāo)志置0時(shí),frq遞增
case 1:frq--;break; //flag標(biāo)志置1時(shí),frq遞減
}
if (!(frq^0xff)) flag=1; //當(dāng)frq增加到FFH時(shí),flag置1,準(zhǔn)備frq遞減
if (!(frq^0x00)) flag=0; //當(dāng)frq遞減到00H時(shí),flag置0,準(zhǔn)備frq遞增
delayms(15); //^為邏輯異或改變此值可改變節(jié)奏
}
}