51 MCU Learning Notes——4. Two buttons control the LED light

Publisher:WeaselLatest update time:2022-06-09 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Two buttons to control the LED program:


/*

 * @Author: Wang Yunlong

 * @Date:   2020-02-23 12:48:51

 * @Last Modified by: Wang Yunlong

 * @Last Modified time: 2020-02-23 15:37:24

 */

#include

#define LED P0; //Set LED pin

sbit KEY1 = P3^4; //Set key 1 pin

sbit KEY2 = P3^5; //Set key 2 pin

void LightLed1(); //Lighting mode 1

void LightLed2(); //Lighting mode 2

void delay(unsigned int n); //n ranges from 0 to 65535

unsigned char table1[15] = {0x7E,0xBD,0xE7,0x00,0xFF,0xDB,0xBD,0x7E,0x00,0xFF,0x00,0xFF};

unsigned char table2[] = {0xFE,0xFD,0xE7,0xEF,0xDF,0xBF,0x7E,0xFF,0x00,0xFF,0x7F,0xBF,0xDF,0xDF,,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF,0x01};

void main()//two buttons

{

    while(1)

    {

        if(KEY1==0) //key 1, press detection

        {

            delay(5); //delay 5ms

            if(KEY1==0)

            {

                LightLed1();

            }

            while(!KEY1)//Release detection

        }

        if(KEY2==0) //Key 2

        {

            delay(5);

            if(KEY==0)

            {

                LightLed2();

            }

            while(!KEY2);

        }


    }

}

void delay(unsigned int n) //n ranges from 0 to 65535

{

    unsigned char i;

    unsigned int j;

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

    {

        for(j = 0;j        {

            ;

        }

    }

}

void LightLed1()//Lighting method without identifier, for() loop

{

    unsigned char i;

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

    {

        LED = table1[i];

        delay(1000);   

    }

}

void LightLed2()//Lighting method with identifier, while() loop

{

    unsigned char i = 0;

    while(table2[i]!=0x01)

    {

        LED = table2[i];

        delay(1000);

        i++;

    }

}


Method 1 and method 2 are two different ideas:


Method 1: for() loop: stop by the number of elements in the array

Method 2: while() loop, stopped by identifier in array

Reference address:51 MCU Learning Notes——4. Two buttons control the LED light

Previous article:51 MCU Learning Notes——6. Interrupt method to configure timer
Next article:51 MCU Learning Notes——3. Several different ways of lighting LED (array form) and key detection, delay debounce

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号