INDIAN INSTITUTE OF
TECHNOLOGY
PATNA
EC3101: MICROCONTROLLER AND
EMBEDDED SYSTEM LAB
Experiment Name: STM32 Nucleo-Board Blinking LED
Submitted by:
Name: Saksham Srivastava
Roll No: 2301EC56
Group No: 03(FRIDAY GROUP)
AIM: To write and implement an embedded C program to blink the user LED on the STM32F103RB
Nucleo board using the HAL library and code generated from the .ioc file.
APPARATUS:
Hardware: STM32F103RB NucleoDevelopment Board, Micro-USB cable.
Software: STM32CubeIDE.
THEORY:
The STM32CubeIDE provides a graphical tool (.ioc file editor) to configure the
microcontroller's peripherals and automatically generate initialization code using
the HAL (Hardware Abstraction Layer) library. This approach simplifies setup,
allowing the developer to focus on the application logic.
STM32 HAL Library Core Functions:
The HAL provides a set of high-level, user-friendly APIs to interact with the
microcontroller's peripherals. For this experiment, we rely on the auto-generated
initialization and focus on three key functions for our logic:
1. HAL_Init():
This is the first function that must be called in any HAL-based application.
It performs essential system-level initializations. Its primary responsibilities
include:
o Initializing the Flash prefetch buffer and instruction cache.
o Setting up the SysTick timer to be the time base for the HAL. The SysTick
is a 24-bit timer that provides a periodic interrupt, which is crucial for
functions like HAL_Delay().
o Setting the default interrupt priority grouping.
Essentially, HAL_Init() prepares the microcontroller and the HAL drivers
for use. It is automatically called at the beginning of the main() function in
a CubeIDE-generated project.
2. HAL_GPIO_WritePin(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin,GPIO_PinState):
This function is used to set or reset a specific GPIO pin. It is the fundamental function for
controlling digital outputs like LEDs, relays, etc.
o GPIOx: Specifies the GPIO port to be used (e.g., GPIOA, GPIOB). For the
user LED, this is GPIOA.
o GPIO_Pin: Specifies the pin number to be controlled (e.g., GPIO_PIN_5).
o PinState: This is an enumeration that determines the pin's state. It can be
GPIO_PIN_SET to drive the pin HIGH (3.3V) or GPIO_PIN_RESET to
drive the pin LOW (0V).
3. HAL_Delay(uint32_tDelay):
This function provides a blocking delay for a specified number of
milliseconds. It relies on the SysTick timer configured by HAL_Init(). When
called, it polls a counter variable that is incremented by the SysTick interrupt
service routine. The function does not return until the desired time has elapsed.
It is a simple way to pause the program execution, for example, to control the
blinking speed of an LED.
Blinking Logic:
The blinking effect is achieved by repeatedly setting and resetting the LED's GPIO
pin within an infinite while(1) loop. The HAL_Delay() function is used to create a
visible pause between each state change.
The sequence is as follows:
1. Set PA5 HIGH using HAL_GPIO_WritePin() to turn the LED ON.
2. Wait for a specific duration using HAL_Delay().
3. Set PA5 LOW using HAL_GPIO_WritePin() to turn the LED OFF.
4. Wait for the same duration.
5. Repeat.
CIRCUIT: PA5: GPIO_OUTPUT
CODE:
int main(void)
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
HAL_Delay(500);
/* USER CODE BEGIN 3 */
/* USER CODE END 3 */
RESULT:
The embedded C program using the HAL library was successfully compiled and flashed onto
the STM32F103RB Nucleo board. The user LED (LD2) was observed to be blinking
continuously with a 500ms interval, confirming the correct use of the HAL_GPIO_WritePin
and HAL_Delay functions within the CubeIDE-generated framework.