/*****************按键蜂+鸣器**********************/
/*****************qq:774145445*********************/
/*****************快手:共同学习stm8****************/
/*****************IDE:PADAUK_0.91******************/
/********完成功能:按键按几次蜂鸣器响几次***********/
/*****************20210617*************************/
// _______ ______
// -|vdd \_/ gnd|-
// anjian -|pa7 pms150g pa0|- led
// -|pa6 pa4|- fengmingqi
// -|pa5___________pa3|-
#include "extern.h"
/**************************************************/
io_fengmingqi bit pa.4;
io_anjian bit pa.7;
io_led bit pa.0;
/**************************************************/
byte anjian_a1; //按键标志位
byte anjian_b1; //按键次数
byte anjian_c1; //按键退出条件
byte anjian_c2; //按键退出条件
byte fengmingqi_a1; //蜂鸣器标志位
byte fengmingqi_a2; //蜂鸣器次数
/**************************************************/
void fengmingqi(); //蜂鸣器
void anjian(); //按键
void yanshi(); //延时
void fengmingqi_yanshi();//蜂鸣器延时
/**************************************************/
void FPPA0 (void)
{
.ADJUST_IC SYSCLK=IHRC/4 // SYSCLK=IHRC/4
/**************************************************/
$ io_fengmingqi out,low; //输出低
$ io_anjian in,pull; //输入上拉
$ io_led out,high; //输出高
/**************************************************/
anjian_a1 =1; //0不检测,1检测,2计算次数
anjian_b1 =0;
anjian_c1 =0;
anjian_c2 =0;
fengmingqi_a1 =0;
fengmingqi_a2 =0;
while (1)
{
anjian();
fengmingqi();
yanshi();
}
}
/**************************************************/
void fengmingqi() //蜂鸣器
{
word fengmingqi_b1;
if(fengmingqi_a1==1)
{
fengmingqi_a1 =0;
while(fengmingqi_a2--) //次数
{
fengmingqi_b1 =1000;
while(fengmingqi_b1--)
{
fengmingqi_yanshi();
if(io_fengmingqi){io_fengmingqi=0;} else{io_fengmingqi=1;}
}
fengmingqi_b1 =1000;
while(fengmingqi_b1--)
{
fengmingqi_yanshi();
io_fengmingqi =0;
}
}
fengmingqi_a2 =0;
anjian_a1 =1; //开始检测按键
if(io_led){io_led =0;} else{io_led =1;}
}
}
/**************************************************/
void anjian() //按键(标志位版带释放)
{
if(anjian_a1==1) //0不检测,1检测,2计算次数
{
if(io_anjian==0)
{
anjian_a1 =2;
anjian_c1 =200; //2秒退出条件
anjian_c2 =200;
}
}
if(anjian_a1==2)
{
if(anjian_c1 >0) //退出条件1
{
if(io_anjian==0)
{yanshi();}
if(io_anjian==0)
{
yanshi();
while(io_anjian==0)
{
yanshi();
anjian_c1 --;
if(anjian_c1 ==0) //退出
{
anjian_a1 =0;
fengmingqi_a1 =1; //打开蜂鸣器
fengmingqi_a2 =anjian_b1; //蜂鸣器次数
anjian_b1 =0;
}
}
anjian_b1 ++; //按键次数+1
anjian_c1 =200; //续2秒时间
anjian_c2 =200;
}
}
if(anjian_c2 >0) //退出条件2
{
anjian_c2 --;
if(anjian_c2 ==0)
{
anjian_a1 =0;
fengmingqi_a1 =1; //打开蜂鸣器
fengmingqi_a2 =anjian_b1; //蜂鸣器次数
anjian_b1 =0;
}
}
}
}
/**************************************************/
void yanshi() //延时
{
.delay 20000;
}
/**************************************************/
void fengmingqi_yanshi() //蜂鸣器延时
{
.delay 2500;
}
/**************************************************/
/**************************************************/ |