两个简单的报警程序,希望能对大家有所启发,让单片机发出更美妙的音响
#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); //^为逻辑异或改变此值可改变节奏
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)