51 MCU tutorial: key timer debounce (a program with delay is not a good program

Publisher:科技梦行者Latest update time:2022-01-12 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Key debounce principle

1. First, let’s review the key delay debounce

Since the button is a mechanical structure, it is inevitable to have jitter when pressing it. Generally, the jitter occurs when pressing and releasing it. The jitter time is about 10ms.

insert image description here

Therefore, there is a simple solution to delay the debounce of key jitter:


2. Key debounce code

insert image description here

Method 1: The time of key press loss depends on the time from pressing the key to releasing it, which is at least 10ms. After pressing the key, the CPU will no longer execute other instructions until the key is released, and the application capability is weak.

Code function: Press the button to invert the LED state, press the button and wait until it is released


#include

sbit key=P1^0; //define key as P1.0

sbit led=P2^0; //define LED as P2.0


void delay_ms(unsigned int t) //ms delay

{

unsigned int i,j;

for(i=0; i for(j=0; j<120; j++);

}

void main(void)

{

while(1)

{

if(key==0)   //Read P1.0 pin, if the pin is low, enter if

{

delay_ms(10); //10ms delay to eliminate jitter

if(key==0) //Judge again whether the key is pressed to prevent interference and enhance stability

{

led = !led; //led state changes

while(key==0); //Wait for the key to be released to prevent further execution

}

}

}

}


Method 2: (No while) The time it takes to press a key is about 10ms, no waiting is required, other operations can be performed while the key is pressed, and the application capability is strong


Code function: Press the button to invert the LED state, press the button without waiting for release


#include

sbit key=P1^0; //define key as P1.0

sbit led=P2^0; //define LED as P2.0


void delay_ms(unsigned int t) //ms delay

{

unsigned int i,j;

for(i=0; i for(j=0; j<120; j++);

}


void main(void)

{

int key_up=1; //Key release flag

while(1)

{

if(key==0 && key_up==1) // Check if the key is pressed

{

delay_ms(10); //delay to eliminate jitter

key_up=0; //Prevent loop execution of key control program

if(key==0) //Judge again to exclude the possibility of release or external interference

{

led1=!led1;

}

}

else if(key==1) key_up=1;//No key is pressed and the state is released

}

}


As you can see, the biggest disadvantage of delay debouncing is the delay, which takes at least 10ms to 20ms. For us, 10ms may be very short, but for some high-performance MCUs, it is the time to execute tens of thousands of instructions.


Key timer debounce

1. Key debounce

Key debounce is generally divided into 4 steps:

1. Determine whether the button is pressed

2. Eliminate shaking

3. Determine again whether the button is pressed

4. Wait for the button to be released

2. Timer debounce principle

1. Determine whether the button is pressed.

2. If a key is detected to be pressed, the timer is turned on and the timer interrupt is turned on. The timing time is about 10ms, so that the timer interrupt is entered 10ms after the key is pressed. When the interrupt is entered, the key jitter time has passed.

3. Determine again whether the button is pressed in the timer interrupt

4. Turn off the timer and wait for the button to be released


Code:


#include

sbit key=P1^0; //define key as P1.0

sbit led=P2^0; //define LED as P2.0


void main(void)

{

TMOD|=0X01; //Select timer 0 mode, working mode 1, and only use TR0 to start.

TH0=(65536-10000)/256; //Assign an initial value to the timer, set the timer to 10ms

TL0=(65536-10000)%256;

ET0=1; //Enable timer 0 interrupt

EA=1; //Open the general interrupt

TR0=0; //turn off the timer

while(1)

{

if(key==0)   //Read P1.0 pin, if the pin is low, enter if

{

TR0=1; //Turn on the timer

}

}

}


/*Timer interrupt*/

void Timer0() interrupt 1

{

TH0=(65536-10000)/256; //Assign an initial value to the timer, set the timer to 10ms

TL0=(65536-10000)%256;


TR0=0; //turn off the timer

if(key==0) //Check if the key is pressed again

{

led=!led;

while(key==0);   //Wait for the key to be released

}

}


You can see that the code above still has "while(key==0); //Wait for the key to be released" which will make the program wait


Program upgrade! Final code

Modify the circuit and add external interrupt:

insert image description here

Code:


#include

sbit key=P3^2; //define key to P1.0

sbit led=P2^0; //define LED as P2.0


void main(void)

{

TMOD|=0X01; //Select timer 0 mode, working mode 1, and only use TR0 to start.

TH0=(65536-10000)/256; //Assign an initial value to the timer, set the timer to 10ms

TL0=(65536-10000)%256;

ET0=1; //Enable timer 0 interrupt

TR0=0; //turn off the timer

IT0=1; // Jump edge start mode (falling edge)

EX0=1; //Turn on the interrupt enable of INT0.

EA=1; //Open the general interrupt

while(1)

{


}

}


/*Timer interrupt*/

void Timer0() interrupt 1

{

TH0=(65536-10000)/256; //Assign an initial value to the timer, set the timer to 10ms

TL0=(65536-10000)%256;


TR0=0; //turn off the timer

if(key==0) //Check if the key is pressed again

{

led=!led; //Control the LED status to reverse

}

}

/*External interrupt 0*/

void Int0() interrupt 0 //interrupt function of external interrupt 0

{

TR0=1; //Turn on the timer

}


It can be seen that the entire program has no delay and no waiting, and the key function is realized.


END

Reference address:51 MCU tutorial: key timer debounce (a program with delay is not a good program

Previous article:51 MCU tutorial: Digital tube dynamic display (0~99999999) 74hc138 driver
Next article:51 MCU tutorial: key input, matrix key (key usage) proteus simulation + keil

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号