51 Single Chip Microcomputer (Twenty-five) - Independent button control LED

Publisher:atech123Latest update time:2021-07-14 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the previous article about 51 MCU, we mainly talked about the application of the output function of MCU IO port, such as LED control, digital tube control, etc. In this article, we will explain how to use the MCU IO port to read the state of the key and control the LED according to the state of the key. For the introduction of the key input function of 51 MCU, please refer to the article "51 MCU (Twenty-four) - Working Principle of Independent Key"


In this article, the code of the example is shown below.


#define KEY P2 //Define key input port

#define LED P0 //Define LED output port

 

/************************************************************************/

/* Main function */

/************************************************************************/

void main (void)

{

KEY=0xff; //Set the key input port level to high

for(;;) //main loop

{

LED=KEY;

}

}


The code is very simple. The main code is only one line LED=KEY in the for loop. The purpose of this line of code is to assign the value of KEY to the LED. From the macro definition of the program, we can know that KEY is P2, which is the register of the 8 IO ports connected to the independent button. LED is P0, which is the register of the 8 IO ports connected to the LED. When an independent button is pressed, the level state of the IO port corresponding to P2 is pulled down, and the input is 0. When the button is released, the input is 1. In the for loop, the value of P2 is assigned to P0. When the button is pressed, the output of the IO port that controls the LED is 0, and the corresponding LED lights up. When the button is released, the output of the IO port that controls the LED is 1, and the corresponding LED goes out. Since the assignment code is in the for loop, the state of the LED will be updated in real time.

Reference address:51 Single Chip Microcomputer (Twenty-five) - Independent button control LED

Previous article:51 single chip microcomputer (part 26) - independent button digital tube display
Next article:51 Single Chip Microcomputer (Twenty-four) - Working Principle of Independent Keys

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号