两个简略的报警程序,期望能对我们有所启示,让单片机宣布更美好的音响
#include
#include
sbit SPK = P3^4;
unsigned char frq;
void delayms(unsigned char ms)
// 延时子程序
{
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); //改动此处能够改动报警声的节奏长度
}
}
#include
#include
sbit SPK = P3^4; //喇叭接到P3.4上
unsigned char frq;
unsigned char flag;
void delayms(unsigned char ms)
// 延时子程序
{
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标志置0时,frq递加
case 1:frq–;break; //flag标志置1时,frq递减
}
if (!(frq^0xff)) flag=1; //当frq增加到FFH时,flag置1,预备frq递减
if (!(frq^0x00)) flag=0; //当frq递减到00H时,flag置0,预备frq递加
delayms(15); //^为逻辑异或改动此值可改动节奏
}
}