蜂鸣器程序举例
http://www.ke-sen.com
蜂鸣器程序举例一:
#define beep_nor() —–
#define beep_on() —–
#define beep_off() —–
typedef struct _BEEP
{
UINT8 MuteState; //静音状态 : 开、关
UINT8 BeepState; //蜂鸣器状态: 开、关
UINT8 BeepCount; //蜂鸣器响的次数
UINT8 BeepOnTime; //蜂鸣器响的时间
UINT8 BeepOffTime; //蜂鸣器不响的时间
UINT8 BeepWaitTime; //蜂鸣器响和不响的定时时间
UINT8 BeepRefresh; //是否需要刷新蜂鸣器显示图标
}BEEP_STRUCT;
//—————————–
//n-响的次数,btime-响的时间,gtime-不响的时间
//—————————–
void beep(BEEP_STRUCT *beep, UINT8 n,UINT8 btime,UINT8 gtime)
{
if((beep->MuteState ==0)&&(n>0)) //非静音
{
beep->BeepState = 1; //响
beep->BeepCount = n;
beep->BeepOnTime = btime;
beep->BeepOffTime = gtime;
beep->BeepWaitTime = 3*beep->BeepOnTime;
}
}
//—————————–
//在100ms的定时器处理中调用
//—————————–
void beep_deal(BEEP_STRUCT *beep)
{
if(beep->BeepState != 0){ //响
if(beep->BeepCount >0){
if(beep->BeepWaitTime>0){
beep->BeepWaitTime–;
}
else{
beep->BeepWaitTime = 3*beep->BeepOffTime;
beep->BeepState = 0; //不响
beep->BeepCount–; //响的次数减1
}
}
else{
beep->BeepState = 0;
}
}
else{
if(beep->BeepCount>0){
if(beep->BeepWaitTime>0){
beep->BeepWaitTime–;
}
else{
beep->BeepWaitTime = 3*beep->BeepOnTime;
beep->BeepState = 1;
}
}
}
if(beep->BeepState !=0){
beep_on();
}
else{
beep_off();
}
}
蜂鸣器程序举例二:
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
//#define delay
//#difine beep
#include
//sbit P1_2;
unsigned char count;
void delay(unsigned int x)
{
unsigned int i,j;
for(i=x;i>0;i–)
{
for(j=250;j>0;j–)
P1_7=~P1_7;//喂狗
}
}
void beep0(void)
{
P1_2 = 0;
delay(100); //短鸣
P1_2 = 1;
delay(500); //停一会
}
void beep1(void)
{
P1_2 = 0;
delay(100); //短鸣
P1_2 = 1;
delay(500); //停一会
P1_2 = 0;
delay(300); //长鸣
P1_2 = 1;
delay(500); //停一会
}
void main(void)
{uint n,t,m;
while(1)
{
P1_7=~P1_7;
for(n=0;n<8;)
{ t=n%8;
m=t%2;
P1_2=!P1_2;
if(m==1)
{ switch (m)
{
case 1:beep1();
break;
default:break;
}
}
else
switch(t)
{case 0:beep0();
break;
case 2:beep0();
break;
case 4:beep1();
break;
case 6:beep0();
break;
default:break;
}
n++;
}
}
} |