#include<reg51.h>
#define
uchar
unsigned
char
#define
uint
unsigned
int
sbit
SPK=P1^0
sbit
K1=P1^7
//发声函数
void
Alarm(uchar
t)
{
uchari,j
for(i=0i<200i++)
{
SPK=~SPK
for(j=0j<tj++)
//由参数t行成不同的频率
}
}
voidmain()
{
SPK=0
while(1)
{
if(K1==1)
{
Alarm(90)
Alarm(120)
}
}
}
试试这个
int speaker(unsigned int freq,unsigned int delay){ static int flag=0,bit
if(flag==0)
{
flag=1
iopl(3)
}
outb(0xb6,0x43)
outb((freq & 0xff),0x42)
outb((freq >> 8),0x42)
bit=inb(0x61)
outb(3 | bit,0x61)
usleep(10000*delay)
outb(0xfc | bit,0x61)
}
#include <sys/io.h>
#include <unistd.h>
main()
{
int i
for(i = 0 i<1 i++)
{
speaker( 2000, 40 )
sleep(2)
}
speaker( 1000, 40 )
}
希望对你有帮助。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)