MCU independent button to prevent continuous pressing

Publisher:张延强Latest update time:2020-03-10 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I recently learned about single-chip microcomputers. When making buttons, I added anti-shake, but accidentally turned it into continuous pressing. If the anti-shake time is extended, it will affect the program execution. Finally, I added a button release detection to solve the problem of accidentally turning it into continuous pressing.


//----------------------Independent buttons-------------------------------------------------------------------

//4-bit independent button test, with anti-shake function and anti-long press function

//--------------------------------------------------------------------------------------------------

 

#include

#include "Keyboard.h"

 

void Key_Init()

{

TRISC=0xF0; //High 4 bits input, key detection, low 4 bits output, sound output

//Beep=0;

}

 

//---------------------------------------------------------------------------------------------------

//Key prompt tone

//n is the number of beeps. When l=0, it beeps about 50ms. When l>0, it beeps about 500ms.

//---------------------------------------------------------------------------------------------------

void Beep_Beep(INT8U n,INT8U l)

{

do

{

Beep=1;

if(l>0)

__delay_ms(500);

else

__delay_ms(50);

Beep=0;

if(l>0)

__delay_ms(500);

else

__delay_ms(50);

}while(1}

 

//---------------------------------------------------------------------------------------------------

//Detect the button to prevent long press. Long press will only display one press.

//---------------------------------------------------------------------------------------------------

char Key()

{

if(Key1==0) //If the key is pressed

{

__delay_ms(10); //delay 10ms

if(Key1==0) //Continue to check whether it is pressed. If it is, it means the button is pressed. If not, it means it is shaking.

{

while(!Key1); //Wait for the button to be released before returning to the button value to prevent continuous pressing due to untimely release

return 1;

}

}

 

if(Key2==0)

{

__delay_ms(10);

if(Key2==0)

{

while(!Key2);

return 2;

}

}

 

if(Key3==0)

{

__delay_ms(10);

if(Key3==0)

{

while(!Key3);

return 3;

}

}

 

if(Key4==0)

{

__delay_ms(10);

if(Key4==0)

{

while(!Key4);

return 4;

}

}

return 0;

}

head File 


//----------------------Independent buttons--------------------------------------------

//4-bit independent button test, with anti-shake function

//--------------------------------------------------------------------------

#define Beep RC3

#define Long_Beep 1

#define Short_Beep 0

 

#define Key1 RC4

#define Key2 RC5

#define Key3 RC6

#define Key4 RC7

 

#define _XTAL_FREQ 4000000UL

#define INT8U unsigned char

 

 

//----------------------------------------------------------------------------

//Function declaration

//----------------------------------------------------------------------------

void Beep_Beep(INT8U n,INT8U l);

void Key_Init();

 

char Key();


Reference address:MCU independent button to prevent continuous pressing

Previous article:PIC16F1933 lights up LCD1602 (assembly)
Next article:Design of DC motor speed regulation with PIC16F874 microcontroller as the central processor

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号