Use STM32GPIO to read buttons and implement button operations

Publisher:数字之舞Latest update time:2018-07-07 Source: eefocusKeywords:STM32  GPIO Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Development board button settings

    In addition to the reset button, four buttons are designed on the "STM32-PZ6806L" development board, marked as "UP", "DOWN", "LEFT" and "RIGHT". The circuit is as follows:

According to the circuit connection, one end of the K_UP button is connected to GPIOA_0, and the other end is connected to 3.3V through a 1KΩ resistor. Therefore, when configuring GPIOA_0, the working mode should be set to "pull-down input". When the button is released, it is a low level, and when the button is pressed, it is a high level; one end of the three buttons K_LEFT, K_DOWN and K_RIGHT are connected to GPIOE_2, GPIOE_3 and GPIOE_4 respectively, and the other end is grounded. Therefore, GPIOE_2, GPIOE_3 and GPIOE_4 should be configured as "pull-up input". When the button is released, it is a high level, and when the button is pressed, it is a low level.

2. Basic configuration of the project

This project is designed based on the music player project. When these four keys are pressed, the buzzer will emit different sounds. For the implementation of the music player project, see: Use STM32 to control the passive buzzer to play music (STM32_07)

1. Copy the pMusic project folder and rename the folder to "pKey";

2. Create a new "Key" folder under the "pKey/User" folder;

3. Use "Keil uVision5" to open the project "pMusic" in the "pKey" folder (the project name has not changed);

4. Create "key.h" and "key.c" files and save them in the "pKey/User/Key" folder;

5. Add the "key.c" file to the "User" group of the project;

6. Configure the project and add the ".\User\Key" path to the "Include Paths" in the "C/C++" tab so that the system can find it when other programs include the "key.h" header file.

3. Programming to realize button functions

1. "key.h" header file program

In the header file, define the key GPIO port macro, key pin macro, and macro for reading pin values, and define the declarations of the two functions Key_Init and ReadKey. The contents are as follows:


#ifndef __KEY__H

#define __KEY__H

#include "system.h"

#include "stm32f10x_gpio.h"

#define KEY_UP_PORT                         GPIOA

#define KEY_OTHER_PORT        GPIOE

#define KEY_UP                                               GPIO_Pin_0

#define KEY_LEFT                                 GPIO_Pin_2

#define KEY_DOWN                              GPIO_Pin_3

#define KEY_RIGHT                              GPIO_Pin_4

//Use library function to read keystrokes

#define K_UP                                           GPIO_ReadInputDataBit(KEY_UP_PORT, KEY_UP)

#define K_LEFT                                       GPIO_ReadInputDataBit(KEY_OTHER_PORT, KEY_LEFT)

#define K_DOWN                                      GPIO_ReadInputDataBit(KEY_OTHER_PORT, KEY_DOWN)

#define K_RIGHT                                      GPIO_ReadInputDataBit(KEY_OTHER_PORT, KEY_RIGHT)

void Key_Init(void);

u8 ReadKey(u8 mode);

#endif

2. "key.c" program file program

The "Key_Init" function enables GPIOA and GPIOE, configures GPIOA_0 as a pull-down input mode, and configures GPIOE_2, GPIOE_3, and GPIOE_4 as pull-up input modes. The "ReadKey" function implements key scanning, and selects single scan (mode=0, a key press from being pressed to being released) and continuous scan (mode=1, a key press from being pressed to being released can be considered as multiple key presses) through the parameter "mode".

#include "key.h"

#include "stm32f10x_Rcc.h"

#include "SysTick.h"

void Key_Init()

{

         GPIO_InitTypeDef GPIO_mode;

         // Enable GPIOA and GPIOE clocks

         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOE, ENABLE );    

         GPIO_mode.GPIO_Mode = GPIO_Mode_IPD;

         GPIO_mode.GPIO_Pin = KEY_UP;

         GPIO_mode.GPIO_Speed = GPIO_Speed_50MHz;

         GPIO_Init(KEY_UP_PORT,&GPIO_mode);

        

         GPIO_mode.GPIO_Mode = GPIO_Mode_IPU;

         GPIO_mode.GPIO_Pin = KEY_DOWN|KEY_LEFT|KEY_RIGHT;

         GPIO_mode.GPIO_Speed = GPIO_Speed_50MHz;

         GPIO_Init(KEY_OTHER_PORT,&GPIO_mode);

}

/*

mode=0--single scan

mode=1--continuous scanning

*/

u8 ReadKey(u8 mode)

{

         static u8 key = 1;

         if(key==1&&(K_UP==1||K_DOWN==0||K_LEFT==0||K_RIGHT==0))

         {

                   delay_ms(10);

                   key = 0;

                   if(K_UP==1)

                   {

                            return 1;

                   }

                   else if(K_DOWN==0)

                   {

                            return 2;

                   }

                   else if(K_LEFT==0)

                   {

                            return 3;

                   }

                   else if(K_RIGHT==0)

                   {

                            return 4;

                   }

         }

         else if(K_UP==0&&K_DOWN==1&&K_LEFT==1&&K_RIGHT==1)//按键松开

         {

                   key = 1;

         }

         if(mode==1)

         {

                   key = 1;

         }

         return 0;

}

3. "main.c" program

In the main function, first initialize SysTick, buzzer IO port, and Key button IO port, then read the buttons repeatedly, and call the Sound function in "beep.c" according to the button value to make the sound. The program is as follows:

#include "beep.h"

#include "SysTick.h"

#include "key.h"

int main()

{

         u8 key, i;

         u16 tone[] = {0,262,294,330,349};

         SysTick_Init(72);

         BEEP_Init();

         Key_Init();

         while(1)

         {

                   key = ReadKey(0);

                   if(key!=0){

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

                                     Sound(tone[key]);

                   }

         }

}

4. Download the hex file to the development board, and press "UP", "DOWN", "LEFT" and "RIGHT" to hear the buzzer emit the sounds of Do, Re, Mi and Fa.


Keywords:STM32  GPIO Reference address:Use STM32GPIO to read buttons and implement button operations

Previous article:Explanation of problems caused by not understanding the STM32 GPIO mode
Next article:About 8 working modes of GPIO in STM32

Recommended ReadingLatest update time:2024-11-16 11:25

stm32 io port configuration and use
1.io model There are 8 types of configurations for stm32 GPIO: (1) GPIO_Mode_AIN analog input  (2) GPIO_Mode_IN_FLOATING floating input (3) GPIO_Mode_IPD pull-down input  (4) GPIO_Mode_IPU pull-up input  (5) GPIO_Mode_Out_OD open-drain output (6) GPIO_Mode_Out_PP push-pull output (7) GPIO_Mode_AF_OD multiplexe
[Microcontroller]
stm32 io port configuration and use
STM32 Timer Application Notes
As we all know, the timer functions of STM32 are very large and complex, and their applications are also very common. Currently, the STM32 family has 10 product lines, all of which have multiple built-in timer peripherals. Although the timers of each STM32 series may differ slightly in number and characteristics, the
[Microcontroller]
STM32 Timer Application Notes
How to confirm whether the STM32 clock configuration is correct
Configure STM32F103 clock (HSI) to 48M void SystemClock_Config(void) {     RCC_DeInit();     RCC_HSICmd(ENABLE);     while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);     RCC_HCLKConfig(RCC_SYSCLK_Div1);          RCC_PCLK1Config(RCC_HCLK_Div2);     RCC_PCLK2Config(RCC_HCLK_Div1);       RCC_PLLConfig(RCC_PLLS
[Microcontroller]
STM32 uses serial port interrupts to send and receive data
The chip used here is STM32F103, which uses interrupts to receive and send serial port data. Here we use the method of directly operating the registers. Using the library function is similar to this, except that we just call the corresponding library function instead. Configure serial ports and multiplexed IO ports v
[Microcontroller]
The role of the type modifier volatile in STM32 code
Today I am still working on the STM32 DAC Function generator. After all, the functions of the microcontroller are limited, and it is a bit difficult to make a function generator. However, the STM32 built-in DAC can achieve a 100KHz 40-sample sine signal, which is indeed quite powerful. The DAC uses DMA + timer to trigg
[Microcontroller]
The role of the type modifier volatile in STM32 code
STM32 vector table - serial port 1 interrupt
DCD instruction: used to allocate a continuous word storage unit (32 bits) and initialize the value of the expression to the word storage unit, similar to defining and initializing an array in C. For example: DCD 0 means: allocate a word storage unit and initialize the unit to 0. EXPORT directive: a pseudo-instructi
[Microcontroller]
Migration between different series of STM32 projects
1. The determination of chip capacity is often overlooked. Small capacity products refer to STM32F101xx, STM32F102xx and STM32F103xx microcontrollers with flash memory capacities between 16K and 32K bytes . Medium capacity products refer to STM32F101xx, STM32F102xx and STM32F103xx microcontrollers with flash memory c
[Microcontroller]
stm32 gpio configuration method
The STM32 input and output pins have the following 8 possible configurations: (4 inputs + 2 outputs + 2 multiplexed outputs) ① Floating input_IN_FLOATING ② With pull-up input_IPU    ③ With pull-down input_IPD                      ④ Analog input_AIN ⑤ Open drain output_OUT_OD          ⑥ Push-pull output_OUT_PP
[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号