單片機C語(yǔ)言程序設計:按鍵發(fā)音
/* 名稱(chēng):按鍵發(fā)音
說(shuō)明:按下不同的按鍵會(huì )是 SOUNDER 發(fā)出不同頻率的聲音。本例使用延時(shí)函數實(shí)現不同頻率的聲音
輸出,以后也可使用定時(shí)器
*/
#includereg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit BEEP=P3^7;
sbit K1=P1^4;
sbit K2=P1^5;
sbit K3=P1^6;
sbit K4=P1^7;
//延時(shí)
void DelayMS(uint x)
{
uchar t;
while(x--) for(t=0;t120;t++);
}
//按周期 t 發(fā)音
void Play(uchar t)
{
uchar i;
for(i=0;i100;i++)
{
BEEP=~BEEP;
DelayMS(t);
}
BEEP=0;
}
評論