51 MCU key detection and signal function simulation

Publisher:as233632621Latest update time:2019-02-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the single-chip microcomputer, there is a problem of identifying buttons. The commonly used independent button circuit is as follows: 

 

Write the picture description here 


Since mechanical contact switches are flexible and will vibrate, they need to be debounced. There are two ways to debounce: hardware debounce and software debounce. 


Hardware debounce circuit: 

 

Write the picture description here 


Software debouncing often uses delayed debouncing: when a key is pressed, when a low level is detected, there is a delay of a period of time, usually 5 to 10ms, and then the port is detected again. If it is still a low level, it means that the key is detected to be pressed, and the same applies when the key is released.


There are generally two methods for detecting independent buttons: interrupt detection and query detection. 


The button is connected to the P3^2 pin to detect the number of times the button is pressed and released.


1. Query detection: You can directly loop the detection in the main program, or you can use a timer to detect once every 20ms. The following is a direct detection in the loop.


Main function:


#include

#include


#define uint unsigned int 


sbit in0 = P3^2; //input signal pin


uint Press_flag = 1; //button pressed flag

uint count = 0 ; //Number of key presses


void delay_ms(uint ms)

{

    uint i , j ;

    for(i = 0 ; i < ms ; i++)

    {

        for(j = 0 ; j < 333 ; j++)

        {

            _nop_() ;

        }

    }

}


void key()

{

       if(in0 == 0) //0 is detected

        {

            delay_ms(10); // Delay debounce

            if(in0 == 0) // detect 0 again

            {

                Press_flag = 0; //Indicates that the button is pressed

            }

        }


        if(Press_flag == 0) //If the key is pressed, detect if the key pops up

        {

            if(in0 == 1)

            {

                delay_ms(10) ;

                if(in0 == 1)

                {

                    count++; //Indicates the number of times the key is pressed and released

                    Press_flag = 1; // reset button pressed flag

                }

            }

        }


}


void main(void)

    while(1)

    {

          key() ;

    }

}


Signal function:



signal void test(unsigned int cc) 

{

    unsigned int i , j ;

    for(i = 0 ; i < cc ; i++)

    {

        for(j = 0 ; j < 2 ; j++)

        {

            port3 &= ~(1<<2) ;

            swatch(0.002) ;

            port3 |= 1<<2 ;

            swatch(0.002);

        }

        port3 &= ~(1<<2) ;

        swatch(0.002) ;


        swatch(0.01) ;


        for(j = 0 ; j < 2 ; j++)

        {

            port3 |= 1<<2 ;

            swatch(0.002) ;

            port3 &= ~(1<<2) ;

            swatch(0.002) ;

        }   


        port3 |= 1<<2 ;

        swatch(0.02) ;


        swatch(0.1) ;

    }

    _break_ = 1 ;

}


Signal function waveform: 


You can see that the value of count is 4 



2. Interruption detection


Main program:


#include

#include


#define uint unsigned int 


sbit in0 = P3^2 ;

uint count = 0 ;    

uint press_flag = 1 ;


void delay_ms(uint ms)

{

    uint i , j ;

    for(i = 0 ; i < ms ; i++)

    {

        for(j = 0 ; j < 333 ; j++)

        {

            _nop_() ;

        }

    }

}



void Init_INT(void)

{

    EA = 1; // Enable general interrupt

    EX0 = 1; //Open external interrupt 0

    IT0 = 1; // Falling edge triggers interrupt

}


void Int0 (void) interrupt 0

{

    EX0 = 0; //Disable external interrupt 0 to prevent frequent interrupt triggering

    delay_ms(10) ;

    if(in0 == 0)

    {

        press_flag = 0 ; //button pressed flag

    }

    if(press_flag == 0 && in0 == 1) // detect button pop-up

    {

        delay_ms(10) ;

        if(in0 == 1)

        {

            count++ ;

            press_flag = 1 ;

        }

    }

    EX0 = 1; // Enable external interrupt

}


void main(void)

{

    Init_INT(); //Interrupt initialization


    while(1)

    {


    }

}

Reference address:51 MCU key detection and signal function simulation

Previous article:Summary of the key points of single-chip C51 programming
Next article:STC89C52 MCU RAM Mode

Recommended ReadingLatest update time:2024-11-16 23:35

Siemens launches HEEDS AI Simulation Predictor and Simcenter Reduced Order Modeling solutions
Siemens launches HEEDS AI Simulation Predictor and Simcenter Reduced Order Modeling solutions • Siemens’ HEEDS AI Simulation Predictor solution helps enterprises leverage the benefits of digital twins to achieve product optimization through AI technology with built-in accuracy awareness • Deliver innovative, high-
[Industrial Control]
Siemens launches HEEDS AI Simulation Predictor and Simcenter Reduced Order Modeling solutions
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号