STM32 GPIO input programming example read button status

I. Overview 1. Introduction to the button

The button is a kind of mechanical device. The two ends of the button correspond to two breakpoints of a certain circuit. We can control the voltage and other parameters of the circuit by pressing the button. The application that we use the button usually has a control relay, a keyboard, Reset, etc. As the application expands, buttons have become an integral part of the board.

2, button category introduction

There are four main types of buttons: normally open with reset, normally open without reset, normally closed with reset, normally closed without reset. (This experiment uses the normally open reset button)

The buttons have the following four working modes:

Normally open with reset: The initial default state is open circuit. When pressed, the button will make the circuit connect. After the force is over, it will automatically return to the open state. Normally open with no reset: The initial default state is open circuit, and each time the button is pressed, the open and close state is changed. Normally closed with reset: The initial default state is connected. When the force is pressed, the button makes the circuit open, and it automatically returns to the connected state after the force is over. Normally closed without reset: The initial default state is connected, and each time the button is pressed, the open and close state is changed. 3, button debounce

When using the manual button, the mechanical jitter may cause incorrect recognition of the button. Generally, the button is manually pressed and then released, and the time for contacting the two metal films of the button is about 50 ms, and the time for the button to be released to stabilize is 5-10 ms. Therefore, if the delay is detected for about 20 ms after the first detection of the button being pressed, it is confirmed whether or not the button is actually pressed, thereby eliminating the erroneous recognition caused by the button shake. In this experiment, the detection is performed after a certain delay, thereby effectively avoiding the misjudgment caused by the button shake.

Second, the hardware circuit

In the iCore3 dual-core development board, a separate button is used to connect to the PH15 of the STM32F407, and the current limiting resistor is 1K.

Third, the experimental principle

One end of the button is connected to the GPIO (PH15) of the STM32, and the other end is grounded, and the PH15 is connected to a pull-up resistor of 1K current. PH15 is set to the input mode during initialization. When the button is bounced, PH15 is high (3.3V) due to the pull-up resistor; when the button is pressed, PH15 is directly connected to GND by the button, which is low. . Therefore, it is judged that the level of PH15 changes, and the state of the button can be obtained.

Fourth, the source code

1, the main function

/*

* Name : main

* Description : ---

* Author : ysloveivy.

*

* History

* --------------------

* Rev : 0.00

* Date : 11/21/2015

*

* create.

* --------------------

*/

Int main(void)

{

Int i;

Static int work_status = 0; //Three-color LED light working status

Static int key_status = 1; //button to release the label

Led.initialize(); //Three-color LED light port initialization

Key.initialize();

While(1){

If(KEY_INPUT)key_status = 1;

If(key_status == 1){

If(!KEY_INPUT){

For(i = 0;i < 10000;i++); //button anti-jitter

If(!KEY_INPUT){

Key_status = 0;

Work_status += 1;

If(work_status > 2)work_status = 0;

/ / Operation LED

Switch(work_status){

Case 0:

LED_RED_ON;

LED_GREEN_OFF;

LED_BLUE_OFF;

Break;

Case 1:

LED_RED_OFF;

LED_GREEN_ON;

LED_BLUE_OFF;

Break;

Case 2:

LED_RED_OFF;

LED_GREEN_OFF;

LED_BLUE_ON;

Break;

Default:

Break;

}

}

}

}

}

}

2, GPIO initialization

LED GPIO initialization configuration is consistent with the previous one, the following is the GPIO initialization program for the button

/*

* Name : initialize

* Description : ---

* Author : ysloveivy.

*

* History

* --------------------

* Rev : 0.00

* Date : 11/21/2015

*

* create.

* --------------------

*/

Static int initialize(void)

{

GPIO_InitTypeDef GPIO_uInitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);

/ / Set the IO port of the connection button

GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_15;

/ / Set the port to input mode

GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_IN;

/ / Set the speed to the third level

GPIO_uInitStructure.GPIO_Speed ​​= GPIO_Speed_100MHz;

/ / Set the input port does not change

GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

/ / Use the set parameters to the structure

GPIO_Init(GPIOH, &GPIO_uInitStructure);

Return 0;

}

3, small knowledge

voidRCC_AHB1PeriphClockCmd

(uint32_tRCC_AHB1Periph, FunctionalStateNewState)

This function is a peripheral enable function. The first parameter is the peripheral that needs to be enabled. The second parameter is enable or disable. Many readers may see that this function does not know how to start. I don't know how to fill in the parameters. Don't worry, let's take a look at his definition:

View the definition method: Double-click the function name to select it, right click and click GO TODefinition of ... to view it.

The RCC_AHB1PeriphClockCmd function is defined as follows:

Void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)

{

/* Check the parameters */

Assert_param(IS_RCC_AHB1_CLOCK_PERIPH(RCC_AHB1Periph));

Assert_param(IS_FUNCTIONAL_STATE(NewState));

If (NewState != DISABLE)

{

RCC->AHB1ENR |= RCC_AHB1Periph;

}

Else

{

RCC->AHB1ENR &= ~RCC_AHB1Periph;

}

}

Pay attention to the string that I am marked with, according to the method just defined, it will tell us that the parameters of the function can be filled. See IS_FUNCTIONAL_STATE:

#define IS_FUNCTIONAL_STATE(STATE)

(((STATE) == DISABLE) || ((STATE) == ENABLE))

We can see that the second parameter can be filled in DISABLE and ENABLE.

Summary: The above use of the peripheral's enable function for everyone to introduce the function of the method, this method can often be used in practical applications, when we read, modify the program, what are not clear functions, you can Look at this way.

Five, experimental phenomena

Each time the button is pressed, the ARM·LED color is changed (red, green, and blue alternately).

Projector Cable

Thick VGA to VGA monitor cable connects PC or laptop to projector, SVGA monitor and other display system with 15 pin VGA port
50 ft extra long VGA cable extends your operating distance and makes devices' movement conveniently (15 meters)
Heavy duty VGA display cord is well built with Al-foil shielded layer and dual ferrite beads to protect against EMI and RFI interference for high quality video signal transmission
Nickel plated connectors (male to male interface) and copper conductors enhance this sturdy cable performance (resolution up to Full HD 1080p - 1920 x 1080)
Black computer monitor VGA cable for desktop with thumbscrews to make plugging and unplugging a breeze while ensure easy secure connections.

Projector Cable,Projector Cord,Projector Wire,Projector To Laptop Cable

UCOAX , https://www.jsucoax.com

This entry was posted in on