蜂鸣器响一下灭一下
#include<reg52.h>
sbit fm=P2^3; //代表P2.3
delay(int xms)
{
int i,j;
for(i=xms;i>=0;i--)
for(j=112;j>=0;j--);
}
void main()
{
while(1)
{
fm=0; //使蜂鸣器响起
delay(500);
fm=1;
delay(500);
}
}
实现蜂鸣器和流水灯同时亮同时响
#include<reg52.h>
sbit fm=P2^3; //代表P2.3
sbit led1=P1^0;
sbit led2=P1^1;
sbit led3=P1^2;
sbit led4=P1^3;
sbit led5=P1^4;
sbit led6=P1^5;
sbit led7=P1^6;
sbit led8=P1^7;delay(int xms)
{
int i,j;
for(i=xms;i>=0;i--)
for(j=112;j>=0;j--);
}
void main()
{
while(1);
{
fm=0; //使蜂鸣器响起
led1=0; //使流水第一盏灯亮起来
delay(500);
fm=1;
led1=1;
delay(500);
fm=0;
led2=0;
delay(500);
fm=1;
led2=1;
delay(500);
fm=0;
led3=0;
delay(500);
fm=1;
led3=1;
delay(500);
fm=0;
led4=0;
delay(500);
fm=1;
led4=1;
delay(500);
fm=0;
led5=0;
delay(500);
fm=1;
led5=1;
delay(500);
fm=0;
led6=0;
delay(500);
fm=1;
led6=1;
delay(500);
fm=0;
led7=0;
delay(500);
fm=1;
led7=1;
delay(500);
fm=0;
led8=0;
delay(500);
fm=1;
led8=1;
delay(500);
}
}