The post explains about how to use stm32 pins as input using stmcubeIDE. A simple led on/off tutorial with push button as input is made to explain the coding and working. When a push button is pressed the led turns on and when push button is released the led turns off. The project is pretty straight forward. Before beginning the tutorial i would recommend to take the tutorial on getting started with stm32F103/STM32F303 microcontroller, stumcubeIDE code initialize. This will introduce you how to initialize/configure and align the stmcubeIDE to start working with stm32f103 microcontrollers.
Table of Contents
Requirements:
- STM32F303 – 1
- LED -1
- Push Button -1
- ST-Link Debugger – 1
- FTDI – 1
- Breadboard – 1
- Jumper wires
Light-emitting diode
In the simplest terms, a light-emitting diode (LED) is a semiconductor device that emits light when an electric current is passed through it. Light is produced when the particles that carry the current (known as electrons and holes) combine together within the semiconductor material.
Since light is generated within the solid semiconductor material, LEDs are described as solid-state devices. The term solid-state lighting, which also encompasses organic LEDs (OLEDs), distinguishes this lighting technology from other sources that use heated filaments (incandescent and tungsten halogen lamps) or gas discharge (fluorescent lamps).
Push-button
A push-button (also spelled pushbutton) or simply button is a simple switch mechanism to control some aspect of a machine or a process. Buttons are typically made out of hard material, usually plastic or metal.[1] The surface is usually flat or shaped to accommodate the human finger or hand, so as to be easily depressed or pushed. Buttons are most often biased switches, although many un-biased buttons (due to their physical nature) still require a spring to return to their un-pushed state. Terms for the “pushing” of a button include pressing, depressing, mashing, slapping, hitting, and punching.
Let’s see how to create code using stm32cubeIDE
STEP 1: Open CubeIDE& Create New Project
STEP 1: Choose The Target MCU & Double-Click Its Name
STEP 3: Configure The button Peripheral
Source Code/Program
Method 1
#include"main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#define BUTTON_STATE HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13)
#define LED_HIGH HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET)
#define LED_LOW HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET)
While(1)
{
if(BUTTON_STATE == 1)
{
LED_HIGH;
}
else
{
LED_LOW;
}
}
Method 2
while (1)
{
/* USER CODE END WHILE */
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13) == GPIO_PIN_SET)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
}
else
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
}
}