51 MCU Basics of IoT 03 - Independent Buttons

Publisher:转眼人老Latest update time:2022-05-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Preface

In the previous section, we completed the 51 single-chip microcomputer to control LED lights and achieved the effect of flowing lights. In this section, we will learn to operate independent buttons and use two buttons to turn on and off all LED lights respectively. Buttons are very common as input devices in the system development process. Generally speaking, buttons can set system parameters and control the system operation status.


1. Basic knowledge

1. Button Introduction

The switch used for common buttons is a mechanical elastic switch. When the mechanical contacts are opened and closed, the voltage signal will change. The common button switch is shown in the figure below, where the A-end pins are on the same side and are disconnected by default, and the B-end pins are not on the same side and are turned on by default. When the button is pressed, the A-end is turned on and the B-end is disconnected.

This type of switch usually does not turn on immediately when pressed. The moment of opening and closing is accompanied by a series of jitters, generally 5ms to 10ms. The specific jitter time is determined by the mechanical characteristics of the button. The jitter process is shown in the following figure:

In order to solve this problem, two methods, hardware debouncing and software debouncing, can usually be used. The principle of hardware debouncing is to use capacitors to smooth the wave, and then after Schmidt inverter shaping, a pulse wave without burrs is obtained. In most actual projects, in order to save costs, software filtering is usually used. Software filtering generally uses a delay method. When the key is first detected to be pressed, the software delays for about 10ms, and then checks the key status again. If it is still pressed, it is considered that the key is pressed.


2. Key detection principle

The schematic diagram of a simple independent button connection circuit is shown below

In the independent button circuit, one side of the button is connected to GND, and the other side is connected to the IO port of the microcontroller. When the button is pressed, 1 and 2 on both sides of the button are turned on, and the P2.5 pin of the microcontroller is directly connected to GND, and the level is low. Therefore, you only need to let the microcontroller continuously detect whether the I/O port of the P2.5 pin is at a low level. Once the program detects that the I/O port pin becomes a low level, it means that the button is pressed.


3. Development board button hardware circuit

The schematic diagram of the independent buttons of the development board used in the experiment is as follows. One end of the pins of the four buttons K1, K2, K3, and K4 are grounded, and the other ends are connected to different I/O ports of the microcontroller. Whether the button is pressed can be detected based on the electrical levels of different I/O ports.

2. Examples

1. Create a project

Copy the project from the previous section and change the name to 02-key

2. Program modification

#include


sbit key1 = P2^4;

sbit key2 = P2^5;


unsigned char val = 0;


void delay_ms(int ms)

{

        int i = 0, j = 0;

        for (i = ms; i > 0; i--)

        {

                for (j = 110; j > 0; j--);

        }

}


int main(void)

{

        while(1)

        {

                if (0 == key1 || 0 == key2)

                {

                        delay_ms(10);

                        if (0 == key1)

                        {

                                P1 = 0x00;

                        }


                        if (0 == key2)

                        {

                                 P1 = 0xFF;

                        }

                }

                delay_ms(5);

        }

}


The program detects the status of KEY1 and KEY2. When KEY1 is pressed, the eight LED lights LED3-LED10 are turned on. When KEY2 is pressed, LED3-LED10 are turned off.


3. Operation

After compiling the code, download the generated project.hex file to the board according to the first section. When you press KEY1, the effect is as shown below:

When you click KEY2, the light goes out. Since it’s quite simple, I won’t post the picture.

Reference address:51 MCU Basics of IoT 03 - Independent Buttons

Previous article:51 MCU Basics of IoT 04-Buzzer
Next article:51 MCU Playing with the Basics of the Internet of Things 02-Light up the LED lights to achieve a marquee effect

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号