P2.5管脚控制蜂鸣器
#include "reg51.h"
typedef unsigned int u16;
typedef unsigned char u8;
sbit BEEP=P2^5;
void delay(time){
while(time--);
}
void main(){
u16 i=2000; //脉冲2000次
while(1){
while(i--){
BEEP=!BEEP; //产生一定频率的脉冲信号
delay(100); //改变数值,音调会变
}
i=0; //清零
BEEP=0; //关闭蜂鸣器
}
}
#include "reg51.h"
typedef unsigned int u16;
void delay(u16 time){
while(time--);
}
sbit BEEP=P2^5;
void main(){
u16 i=80000;
while(1){
while(i>0){
BEEP=!BEEP;
delay(80);
}
if(i==0){
BEEP=0;
}
}
}