STM32 Nucleo MCU starting with LED blinking [Gateway to Embedded system]
Setting the STM32's peripheral GPIOs to blink the output LEDs is a simple operation in itself, but it means that the series of steps from the initial setup of MCU to the build and transfer to MCU have been successfully completed.
The most elementary program for MCU is probably the LED-blinking, which arbitrarily turns on and off LEDs connected to the outputs.
Although the operation itself is simple, controlling the output LEDs means that the series of steps from the initial setup of MCU used, program build, and transfer to MCU have been successfully completed.
This is a small step in terms of overall development, but for those who operate MCUs, especially beginners, it means that they have broken through a major barrier at the beginning. This application is a gateway to success in embedded technology.
LED-blinking circuit
Setting specification:
Input:PC13 Floating input Pushbutton SW
Output:PA5 Push-pull output+500Ω+LED
Program Description
Elementary LED-blinking
The application gpio.c is a program that turns on the output LED only when the pushbutton SW is pressed as input. Although simple, this contains the minimum elements to operate MCU.
In using GPIO in an application, the essential things are (1) to read the pin status of the specified GPIO input port and (2) to set 1 or 0 to the specified output port.
① Reads the pin status of the specified GPIO port
Use GPIO_ReadInputDataBit() to read the state of a specified input pin.
Function Prototype:
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
設定例:if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13)==SET){}//PC13の読み込み
The first argument of the function specifies the GPIO port to be set.
The second argument of the function specifies the pin to be used.
② Set output settings to the pins of the specified GPIO port
Use GPIO_SetBits() to set the output state to the specified GPIO port pins (1).
Function Prototype:
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
Example of Setting: GPIO_SetBits(GPIOA, GPIO_Pin_5);//Set PA5 pin as "High"
The function arguments are the same as for GPIO_ReadInputDataBit().
Reset settings to the pin outputs of the specified GPIO port
Use GPIO_ResetBits() to reset (0) the output state to the specified GPIO port pins, which is an inversion of GPIO_SetBits().
In gpio.c, when the pushbutton switch is pressed, input PC13 is SET(1) and output PA5 is 1 and the LED is lit.