本帖最后由 34af9uc 于 2024-8-19 21:18 编辑
NY8LP10A*长按短按效果
/*****************NY8LP10A**************************/
/*****************按键长按短按效果*******************/
/*****************ID:共同学习stm8*******************/
/*****************20240818*************************/
#include <ny8l.h>
/**************************************************/
#define yi_l PA&=~(1<<2)
#define yi_h PA|=(1<<2)
#define er_l PA&=~(1<<3)
#define er_h PA|=(1<<3)
#define san_l PA&=~(1<<4)
#define san_h PA|=(1<<4)
#define si_l PA&=~(1<<5)
#define si_h PA|=(1<<5)
#define anjian PA&(1<<6)
/**************************************************/
void yanshi(); //延时
void xianshi(); //显示
void anjian_jiance(); //按键检测
unsigned char xian_a1;
/**************************************************/
void main(void)
{
//unsigned char a1;
/**************************************************/
PAIO &=~((1<<2)+(1<<3)+(1<<4)+(1<<5)); //1输入,0输出
PAC |=((1<<2)+(1<<3)+(1<<4)+(1<<5)); //1输出,0开漏
/**************************************************/
PAIO |=(1<<6); //1输入,0输出
PAC |=(1<<6); //1.100K,0.1M
/**************************************************/
xian_a1 =1;
while(1)
{
//CLRWDT();
anjian_jiance(); //按键检测
xianshi(); //显示
yanshi(); //延时
}
}
/**************************************************/
/**************************************************/
void yanshi() //延时
{
unsigned char z1,z2;
for(z1=50;z1>0;z1--)
for(z2=120;z2>0;z2--);
}
/**************************************************/
void xianshi() //显示
{
if(xian_a1&(1<<0)) {yi_h;} else{yi_l;}
if(xian_a1&(1<<1)) {er_h;} else{er_l;}
if(xian_a1&(1<<2)) {san_h;} else{san_l;}
if(xian_a1&(1<<3)) {si_h;} else{si_l;}
}
/**************************************************/
void anjian_jiance() //按键检测
{
static unsigned char anjian_a1;
static unsigned char anjian_a2;
if(anjian) //按下了
{
anjian_a1 =1; //模式1已按
anjian_a2 ++;
if(anjian_a2 >15) //长按
{
anjian_a2 =0;
xian_a1 <<=1;
if(xian_a1&(1<<4))
{
xian_a1 =1;
}
}
}
if(~(anjian)) //没按着
{
if(anjian_a1==1)
{
anjian_a1 =0;
anjian_a2 =0;
xian_a1 <<=1;
if(xian_a1&(1<<4))
{
xian_a1 =1;
}
}
}
}
/**************************************************/
|