yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
STM32F103RCT6 Core board📖
Basic introduction of core board
  • Basic introduction
Experimental documents
  • 1.Turn on RGB light
  • 2.Drive LCD
  • 3.Detect external Flash
  • 4.Read IMU raw data
  • 5. Key control
  • 6. External interruption experiment
  • 7. ADC acquisition
  • 8. PWM Breathing Lamp Experiment
  • 9. Serial communication
Download
  • Source code
  • Environment building tools
  • Hardware data
Download ZIP

Welcome to STM32F103RCT6 Core board repository

5. Key control

Key control

1、Learning objectives

1.Learn the basic working principles of STM32 buttons.

2.Use onboard buttons to control the switch of onboard LED lights.

Introduction to key module:

The key is an electronic switch that can be turned on by gently pressing the switch button during use. When the hand is released, the switch is turned off.

  • Due to the elastic effect of mechanical points, the key switch does not immediately and stably turn on when closed, nor does it suddenly turn off when opened. Therefore, both the closing and opening moments are accompanied by a series of jitters. The length of the jitter time is determined by the mechanical characteristics of the keys, typically between 5ms and 10ms. The length of the stable closing time of the key is determined by the operator's key action, typically from a few tenths of a second to a few seconds. Key shaking can cause keys to be misread multiple times. In order to ensure that the CPU performs only one process for closing a key at a time, it is necessary to perform shake cancellation.
  • There are two methods for key shake elimination, one is hardware shake elimination, and the other is software shake elimination. In order to make the circuit simpler, software dithering is usually used. Our development board also uses software to eliminate jitter. Generally speaking, a simple key elimination method is to first read the status of the key. If the key is pressed, a delay of 10 ms is applied, and then read the status of the key again. If the key is still pressed, it indicates that the key has been pressed. Among them, a delay of 10 ms is software shake removal processing. As for hardware shake removal, you can learn about it from Baidu, which has a very detailed introduction online.

2、Hardware construction

Use the on-board buttons to control the on-board LED lights to turn on and off. From the schematic diagram, it can be seen that the key is PB5, while the LED is PB4 image-2023022700001

3、 Program analysis

Key initialization:

void BSP_init(void)
{
    Key_GPIO_Init();
}

Key_ GPIO_ The Init() function is used to initialize the port and clock of the key. To know whether a key has been pressed, it is necessary to read the IO corresponding to the key Port level status, so we need to configure GPIO to input mode.

Key detection function:

​x
uint8_t Key1_State(uint8_t mode)
{
    static uint16_t key1_state = 0;
​
    if (Key1_is_Press() == KEY1_PRESS)
    {
        if (key1_state < (mode + 1) * 2)
        {
            key1_state++;
        }
    }
    else
    {
        key1_state = 0;
        g_key1_long_press = 0;
    }
    if (key1_state == 2)
    {
        g_key1_long_press = 1;
        return KEY1_PRESS;
    }
    return KEY_RELEASE;
}

Key1_State The function is relatively simple. After the shake is eliminated, it directly determines whether the button is pressed, and pressing it changes the state of the LED lamp. Mode 1: Press once to change the LED status 0: Press repeatedly to change the LED status to achieve a flashing effect

Main function code:

xxxxxxxxxx
int main(void)
{   
    u8 i;
    BSP_init();
    
    
    while(1)
    {
        if(Key1_State(1))
            LED =!LED;  
    }
}
​
​

The main function first initializes the hardware used, and then calls the key scan function within the while loop.

4、Experimental phenomenon

After the program is downloaded, you can control the LED light to turn on and off by pressing the button. Press the button once, the LED light will turn on and off by pressing it again. image-2023022700002

Read IMU raw data
External interruption experiment

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文