Microcontroller learning record - independent button

Publisher:数字冒险Latest update time:2019-05-10 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Hardware Hookup 

 Write the picture description here 

From the circuit connection diagram, we can see that one end of the 8 independent buttons is grounded and the other end is connected to the socket JP5. 

On the development board, JP5 is connected to the P1 port through an external cable. The microcontroller reads the status of the P1 port (0 or 1) to determine which button is pressed.


software design


Writing a key scanning function

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

* Function name: duli_key_scan

* Function: Identify which key is pressed

* Input: None

* Output: key value 1~8

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

unsigned char duli_key_scan(void)

{

    static unsigned char key_num;

    if (KEY != 0xff)

    { //Indicates that a key is pressed

        delay10ms(); //delay to eliminate jitter

        if ((KEY != 0xff))

        { //A key is pressed

            switch (KEY)

            {

                case 0xfe :

                    key_num = 1;

                    break;

                case 0xfd :

                    key_num = 2;

                    break;

                case 0xfb :

                    key_num = 3;

                    break;

                case 0xf7 :

                    key_num = 4;

                    break;

                case 0xef :

                    key_num = 5;

                    break;

                case 0xdf :

                    key_num = 6;

                    break;

                case 0xbf :

                    key_num = 7;

                    break;

                case 0x7f :

                    key_num = 8;

                    break;  

            }

        }

    }


    return key_num;


}


According to the key value returned by the key scanning function, write the LED display program to light up different LEDs.


LED display program

void led(unsigned char num)

{

    //Code value when 8 LEDs are lit individually

    unsigned char led[9] = {0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};


    LED = led[num];


}


Burn to the development board


Note: There are two key modes (the only difference between the two codes is the keyword static) 

The first type: press the light on, release the light off (no static) 

The second type: the light turns on when the button is pressed, and remains unchanged when the button is released until the next press, when the light turns on. (With static).


Extensions 

Buttons move the LED light left and right (button 1 moves right, button 2 moves left) 

Press the button to select the pattern display (each button is like a mode switch, press it to display different modes)


Reference address:Microcontroller learning record - independent button

Previous article:Microcontroller learning record - buzzer
Next article:Microcontroller learning record - playing with LED

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号