电路图:
程序完成功用:
完成四个按键宣布不同的腔调,代码如下:
#include
#define uchar unsigned char
#define uint unsigned int
sbit BEEP = P3^7; //将P3.7接口赋予BEEP(接喇叭)
sbit K1 = P1^4; //将P1.4接口赋予K1
sbit K2 = P1^5; //将P1.5接口赋予K2
sbit K3 = P1^6; //将P1.6接口赋予k3
sbit K4 = P1^7; //将p1.7接口赋予K4
//延时
void DelayMS(uint x)
{
while(x–)
{
for(t=0;t<120;t++);
}
}
void Play(uchar t)
{
for(i=0;i<100;i++)
{
BEEP = ~BEEP; //半个周期取反一次
DelayMS(t); //延时
}
BEEP = 0;
}
void main()
{
while(1)
{
if(K1==0) Play(1);
if(K2==0) Play(2);
if(K3==0) Play(3);
if(K4==0) Play(4);
}
}