In this Blog, How to configure the built push button on an stm32f411re microcontroller if you have already read the blog, which is about “How to configure inbuilt LED on an stm32f411re”. Here the Link for that blog link
Before diving directly into microcontroller programming, it’s crucial to understand the datasheet and reference manual. These documents play a major role in configuring and utilizing the microcontroller effectively. Here’s how they contribute to the process:
1. Datasheet
- Purpose: The datasheet provides an overview of the STM32F411RE microcontroller’s key features, electrical characteristics, pinout, and package details.
- How It Helps:
- Pinout Information: Identifies the function of each pin, including the push button or user button pins.
- Electrical Characteristics: Provides details on voltage levels, current requirements, and operating conditions.
Package Information: Details the physical dimensions and pin configuration of the microcontroller, which is crucial for designing your circuit board or understanding how to connect peripherals.
2. Reference Manual
Reference Manual for STM32F411RE
- Purpose: The reference manual offers comprehensive details on the microcontroller’s architecture, peripherals, registers, and programming information.
- How It Helps:
- Peripheral Configuration: Provides in-depth descriptions of how to configure and use each peripheral, including GPIOs for the push button.
- Register Descriptions: Details the configuration registers, bit fields, and how to set or clear specific bits to achieve desired functionality.
Functional Descriptions: Explains the operational characteristics and capabilities of different functions within the microcontroller, including timers, communication interfaces, and analog functions
Step 1: Download the Essential Documents
To get started, you’ll need to gather some important documentation:
- STM32F411RE Datasheet
- STM32F411RE Reference Manual
- STM32 Nucleo-64 User Manual
Click the links above to download these documents. They are essential for understanding how to configure and use the push button effectively.
Step 2: Locate the Push Button on the Board
The push button is prominently placed on the STM32 Nucleo-64 board, making it easy to find. To see how it’s connected to the microcontroller:
- Open the STM32 Nucleo-64 User Manual and navigate to the schematic diagram section.
- Find the Push Button in the schematic. It will show how the button is wired to the microcontroller’s GPIO pins.
Step 3: Understand the Configuration
- Refer to the Datasheet:
- The datasheet will provide you with details about the GPIO pins, including the one connected to the push button. Look for electrical characteristics and pin functions.
- Consult the Reference Manual:
- The reference manual will give you detailed information about configuring GPIO pins. It includes register descriptions and how to set or clear bits to read the push button state.
If you have already downloaded the user manual, navigate to page 64 to view the schematic diagram for the push button labeled “USER”.
Step 1
For the first procedure, you need to identify which GPIO port the push button is connected to. For every peripheral, it’s important to determine the appropriate GPIO port. In this case, the push button is connected to GPIO port PC13, corresponding to GPIO PIN 13. Now that we have identified the GPIO pin associated with the push button, we can proceed with configuring it accordingly.
Here you have to configure inbuild LED also, when you press the push button your LED gonna blink. The LED is connected with port A 5th pin.
Step 2
Now that the first task is complete, the next step is to examine the internal architecture of the microcontroller. Open the datasheet and refer to page 15 to view the block diagram. This block diagram will provide a detailed overview of the internal architecture, helping you understand how the GPIO ports and other components are organized within the STM32F411RE microcontroller.
Push button is connected port c 13 pin so “GPIO C”
LED connected with port A 5th pin “GPIO A”.
We already know that the push button is connected to GPIOC. Next, you need to check which bus GPIOC is connected to.
As same as for LED, is connected with which GPIO and bus
Bus Overview:
AHB (Advanced High-performance Bus)
APB (Advanced Peripheral Bus)
The bus system is used to transfer data between different components within the microcontroller.
GPIOC is connected to the AHB1 bus, which operates at 100 Mhz. This completes step 2.
GPIOA is connected to the AHB1 bus, which operates at 100 Mhz.
Step 3
We have determined that GPIOC is connected to the AHB1 bus. The next step is to provide clock access to the AHB1 bus in order to enable GPIOC.
To do this, open the Reference Manual. You might wonder why we frequently refer to the datasheet, reference manual, and user manual. Here’s a simple guide:
Datasheet: For internal block diagrams and overall microcontroller architecture.
Reference Manual: For detailed information on registers and their configurations.
User Manual: For schematic diagrams and board-specific details.
With these resources, you’ll have all the information needed to configure and use the microcontroller effectively.
To enable GPIOC, you first need to provide clock access to the AHB1 bus. For this, you’ll need to find the address to enable specific bits in the AHB1 bus.Refer to the Reference Manual and locate the RCC (Reset and Clock Control) register, specifically the AHB1ENR (AHB1 Enable Register) found on pages 117 and 118. This register will allow you to enable the clock for GPIOC by setting the appropriate bit.
Step 4
In the RCC register, locate the AHB1ENR (AHB1 Enable Register) and enable GPIOC by setting the corresponding bit. For GPIOC, you need to enable the clock for GPIOC by setting the 2nd bit (bit 2) of the AHB1ENR register, as the push button is con
nected to GPIOC.
In the MODER register, since the push button is connected to GPIOC (pin 13), you need to configure MODER13
The push button is intended to read its state (pressed or not), so it should be set as an input. According to the reference manual, you need to set MODER13[1:0] to “00” to configure GPIOC pin 13 as an input.
For LED,
Step 4
Here, implement the function of GPIO C using moder register. Push button going to act as an input. so assign the input bit as(00).
LED is going to perform as output. so assign the bit 01
Step 6
Store the input data from the push button into a register. For this purpose, use the IDR (Input Data Register) in the GPIOC register to read the state of the push button. The IDR register will hold the data representing whether the push button is pressed or not.
Code explaination
#include <stdint.h>
<stdio.h> in standard C, which provides functions like printf and scanf. <stdint.h> ensures that you have precise control over integer sizes and ranges.
uint32_t: Which defines an unsigned 32-bit integer type, which can hold non-negative values ranging from 0 to 4,294,967,295 (2^32 – 1).
*pAHB1ENR: A pointer variable to store the address of the AHB1ENR register.
(uint32_t*): Typecasting the hexadecimal address to a pointer of type uint32_t
0x40023830: The hexadecimal address of the AHB1ENR register, used to enable the clock for GPIO ports.
uint32_t *pGPIOAMODER =(uint32_t*)0x40020000 ;
*pMODER: A pointer variable to store the address of the MODER register.
0x40020000: The hexadecimal address of the MODER register, used to configure the mode of GPIO pins.
uint32_t *pGPIOCMODER =(uint32_t*)0x40020800 ;
*pIDR: A pointer variable to store the address of the IDR (Input Data Register) register.
0x40020010: The hexadecimal address of the IDR register, used to read the state of the GPIO pins.
void delay();
void delay();: Function prototype for delay(), which will create a delay in the program. This function can be called from anywhere in the code to execute a delay.
void delay()
{
for(uint32_t i=0;i<300000;i++);
}
Function definition: Defines the delay() function to introduce a delay.
for(uint32_t i = 0; i < 300000; i++);: A loop that iterates to create a delay. Here, i is the loop variable, and the loop runs 300,000 times to provide the delay.
int main(void)
int main(void): The entry point of the program. Initialization and main logic will be placed here.
*pAHB1ENR |= (1U<<0); enable the specific bit(GPIOA) in AHB1ENR register, so using right shift register enable the bit(GPIOA). here the GPIOA has located in the first bit so no shifting
*pAHB1ENR, pointer address which tends to enable specific bit in the AHB1ENR register
| , is the logical OR operator. This is the operator which can helps to set the bit as 1
<<, Bitwise operator(right shift) move the bits towards right side depends on bit present location.
*pAHB1ENR |= (1U<<2);
*pAHB1ENR |= (1U<<2); enable the specific bit(GPIOC) in AHB1ENR register, so using right shift register enable the bit(GPIOC). here the GPIOC has located in the second bit so two times gonna shift.
*pGPIOAMODER |= (1U<<10);
*pGPIOAMODER |= (1U<<10); Using moder register, assign what the function is going to perform by GPIOA
while(1); infinite time loop gonna execute.
uint8_t value; Declare the variable for to acquire the final data.
if (value==0) , check whether the value is 0