51 MCU: Button control light on and off and flashing

Publisher:qazwsx007Latest update time:2018-05-09 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Require:


The light is off when the power is on. Press the button once and the light will turn on. Press it again and the light will flash. Press it again and the light will flash faster.

Press again and the light turns off...

The cycle continues.


To do this, program as follows.


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


#include


unsigned char n = 0;


sbit key = P3^4;

sbit led = P0^0;


void delay()

{

    unsigned int  i, j;

    for(i = 0; i < 100; i++)   for(j = 0; j < 500; j++);

}


void readkey(void)

{

    unsigned char  i, j;

    if(!key)  {

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

        for(j = 0; j < 120; j++); //delay debounce

      if(!key)  {

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

        n++;

        n %= 4; //n = 0~3

      }

    }

}


main()

{

    while(1)  {

      switch(n)    {

      case 0: led = 1; readkey(); break; //turn off

      case 1: led = 0; readkey(); break;      //长亮

      case 2: delay(); readkey(); //slow flashing

      case 3: delay(); readkey(); led = ~led; //fast flashing

      }

    }

}


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


This program has been verified in practice and can meet the requirements of the topic.


Reference address:51 MCU: Button control light on and off and flashing

Previous article:51 MCU C language programming: matrix keyboard input, display on digital tube
Next article:51 single chip microcomputer - 8-bit digital tube dynamically displays 12 digits

Recommended ReadingLatest update time:2024-11-16 13:57

MCU - AT89C51 - External interrupt, timer/counter initialization
1. External interrupt # represents 0 or 1 Required: IT# = # // Select the trigger mode, 0 is level trigger mode, 1 is edge trigger mode                EX# = 1 // External interrupt enabled             EA = 1 // General interrupt enabled Optional: P# = # // Set another level of priority 2. Timer/Counter r
[Microcontroller]
MCU - AT89C51 - External interrupt, timer/counter initialization
51 MCU series knowledge 1--internal and external structure of MCU
1. Typical structure of single chip microcomputer 0. ("MCU MCS-51 Principle and Application Development Tutorial" by Chao Yang. Tsinghua University Press) 1. Internal structure of MCU CPU Memory Internal bus I/O interface Parallel interface            Pass-through interface Interrupt system Special f
[Microcontroller]
Design of advertising lamp based on 51 single chip microcomputer
1. Experimental task   : Use the table method to make the port P1 change a single light: move left 2 times, move right 2 times, and flash 2 times (delay time 0.2 seconds). 2. Circuit schematic diagram:                   Figure 4.5.1 3. Hardware connection on the system board : Connect P1.0-P1.7 in the   " MCU system"
[Microcontroller]
Design of advertising lamp based on 51 single chip microcomputer
Detailed Analysis of the Internal Data Memory RAM of MCS-51 MCU
The internal RAM of the 8051 microcontroller has a total of 256 units, which are usually divided into two parts according to their functions: the lower 128 units (unit address 00H ~ 7FH) and the upper 128 units (unit address 80H ~ FFH). The figure shows the configuration diagram of the lower 128 units. Register area
[Microcontroller]
Detailed Analysis of the Internal Data Memory RAM of MCS-51 MCU
Problems that should be paid attention to when developing 51 single-chip microcomputer operating system
(1) The code of the operating system software cannot be too long Because the system hardware resources of the 51 series microcontrollers are relatively scarce, if the code of the operating system is larger than the code of the application, the user's application may even have to consider giving up resources to the o
[Microcontroller]
Design of power supply circuit based on LP2951 single-button switch
Battery power supply It is a common power supply method in products such as normal electronic equipment and intelligent instruments, and the single-key switch circuit is a common power supply form, and its implementation methods are varied. Generally, programmable devices such as triggers, 555 integrated circu
[Power Management]
Design of power supply circuit based on LP2951 single-button switch
How many interrupt sources are there in 8051 microcontroller? There are several interrupt sources in 8051 microcontroller.
  8051 microcontroller   8051 is an 8-bit single-chip microcontroller, a type of MCS-51 single chip, manufactured by Intel in 1981. INTEL has licensed the core technology of MCS51 to many other companies, so many companies are making microcontrollers with 8051 as the core, such as Atmel, Philips, Shenzhen Lianhua and
[Microcontroller]
Direct software architecture for 8-bit MCU systems (C51 series)
1. Preparation of startup documents $ NOMOD51 ; Ax51 macro assembler control command: disable predefined 8051 ; Customize the storage area that needs to be initialized after power-on ; The starting address of the indirect addressing area IDATA is fixed to 0 IDATALEN EQU 8OH; specifies the length
[Microcontroller]
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号