STM32 (II) GPIO Operation (2) - Controlling the LED light on and off by pressing a button

Publisher:炉火旁的YyeLatest update time:2018-07-21 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

      STM32 is a low-power chip, so its peripherals all have a corresponding clock, and these clocks are turned off when the chip is just powered on, so if you want the peripherals to work, you must turn on the corresponding clock.

      This article introduces how to control the switch of the LEN lamp by pressing buttons based on GPIO.

     (1) Button control


      The figure above shows the circuit of the key. When the key is not pressed, the output signal of KEY2 is low level (the circuit where the key is located is blocked and connected to the ground); when the key is pressed, the output state of KEY2 is high level (the circuit where the key is located is connected and connected to the 3.3V power supply). Therefore, by detecting the level of the pin, it is possible to determine whether the key is pressed.

       When the mechanical contact of the key is disconnected or closed, due to the elastic effect of the contact, the key switch will not be immediately connected or disconnected, and it will produce a ripple signal as shown in the figure below. Software debouncing processing and filtering are required, which is not convenient for input detection. In addition, the hardware can also realize the debouncing function. As shown in the figure above, the hardware debouncing function is to achieve delay through the charge and discharge of C61 to eliminate ripples, thereby simplifying the software processing, so that the software only needs to detect the level of the pin.

      

      As mentioned above, the first step is to turn on the GPIO clock so that the peripheral can work. This article uses the STM32 library function to write the code:



//Start the clock of the button port (port A)

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

//Define a structure to configure the settings of the PA0 pin

GPIO_InitTypeDef GPIO_InitStructure;

//Set the pin of the structure to 0

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

//Set the structure mode to floating input

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

//Set the structure to port A, that is, set the PA0 pin to floating input

GPIO_Init(GPIOA, &GPIO_InitStructure);



      The above code first fills in the GPIO structure, and then writes parameters to the corresponding GPIO register through the function GPIO_Init() to complete the initialization of GPIO.

      Then we need to detect the status of the button. The code is as follows:

// Check if the button is pressed

if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == KEY_ON) {

// Check if the button has been released

      while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == KEY_ON);

//Subsequent code processing after pressing the key         

............................................

}

(II) GPIO output

       Similar to GPIO input, the code for GPIO output is as follows:


//Define a GPIO_InitTypeDef type structure

GPIO_InitTyopeDef GPIO_InitStructure;

// Enable GPIO related peripheral clocks

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

//Select the GPIO pin to be controlled

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

//Set the output mode of the GPIO pin to push-pull output

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

//Set the pin rate to 50MHZ

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHZ;

//Call library function to initialize GPIO

GPIO_Init(GPIOB, &GPIO_InitStructure);


      After using the above code settings, you can control the LED light in the main function.

      Set GPIOB->BSRR to 1 to output a high level to light up the LED, and set GPIOB->BRR to 1 to output a low level to turn off the LED.


Keywords:STM32 Reference address:STM32 (II) GPIO Operation (2) - Controlling the LED light on and off by pressing a button

Previous article:STM32F407 study notes - GPIO_button control LED on and off
Next article:Button lights up LED

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

About the CPU usage of STM32 ~ bare metal without system
1. If you don't run the operating system, the CPU will definitely be 100% used all the time. Even if you are waiting for a delay, the CPU will always execute the empty statement nop, because there is a CPU in the STM32. 2. For stm32, it is always 100%. It just depends on how much free time you have and how much time
[Microcontroller]
STM32 drives WS2812D full-color LED
1 Introduction An intelligent externally controlled LED light source that integrates control circuit and light-emitting circuit. Its appearance is the same as an SMD5050 side-emitting LED lamp bead, and each component is a pixel. The pixel contains an intelligent digital interface data latch signal shaping and amplifi
[Microcontroller]
STM32 drives WS2812D full-color LED
Mapping C library function printf in STM32
1. MDK settings MicroLib in the project's Target 2. Add the following compiled code before the main function: #define COM USART1 //Serial port selection initialization, USART1 is serial port 1, USART2 is serial port 2 #ifdef __GNUC__     #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else   #define PUTCHAR_PR
[Microcontroller]
Mapping C library function printf in STM32
Analysis of the circuit principle of serial one-key download of Zhengdian Atom STM32 Elite Development Board
Use the DTR pin and RTS pin in the serial port chip CH340 to control the level state of the microcontroller reset pin and BOOT0 pin, so as to achieve one-click download. For this one-click download circuit, the key points are that it is enough to understand these two points: 1. You must have the ability to understan
[Microcontroller]
Analysis of the circuit principle of serial one-key download of Zhengdian Atom STM32 Elite Development Board
Network communication UIP transplantation on STM32
1. What is the UIP protocol stack? UIP is a TCP/IP protocol stack suitable for small embedded communication, developed and written by people from the Swedish Institute of Computer Science. It removes the uncommon functions in the complete TCP/IP and simplifies the communication process, but it retains the protocols th
[Microcontroller]
Network communication UIP transplantation on STM32
STM32 study notes - a preliminary understanding of external interrupts
The STM32F103 has 76 interrupts, including 16 core interrupts and 60 maskable interrupts, with 16 levels of programmable interrupt priority. To understand the interrupts of STM32 , we must start with the interrupt priority grouping. To understand the interrupt priority grouping, we must understand what is preemption p
[Microcontroller]
STM32 study notes - a preliminary understanding of external interrupts
STM32 learning clock
STM32 learning----clock collection In STM32, there are five clock sources: HSI, HSE, LSI, LSE, and PLL. ①. HSI is a high-speed internal clock, RC oscillator, with a frequency of 8MHz. ②. HSE is a high-speed external clock that can be connected to a quartz/ceramic resonator or an external clock source with a freq
[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号