LED blink with STM32 Nucleo-64 board with Hal Write function | STM32 Programming Tutorial 1

We showed earlier how we can use the hal toggle function to blink a LED with STM32 Nucleo64 board. In this continued STM32 programming tutorial we will use the HAL set and reset write function to blink a LED with the STM32 Nucleo-64 board. 

The basic usage example of these hal write functions are below:

HAL_GPIO_WritePin(): Sets or resets a pin's output state.

As an example to turn on and off a LED connected to the PB1 pin of the Nucleo64 board we write: 

        // Turn the LED ON by writing HIGH to PA5 (onboard LED LD2)

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);

HAL_Delay(200); // Delay 200ms


// Turn the LED OFF by writing LOW to PA5

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);

HAL_Delay(200); // Delay 200ms

The function HAL_GPIO_WritePin() function is used to LOW or HIGH signal to the selected pin. This is like the digitalWrite() Arduino function. Now if you applied a Label like "LED" to the pin PB1 from the STETCube microcontroller interface then you can also use the label instead of the Port pin name like in the above case.You might not always remember or keep trace of what each pin purpose was. 

stm32cube apply pin label
 So in case label was applied to the pin we can use the below code instead of the above.

// Turn the LED ON by writing HIGH to PB1

HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);

HAL_Delay(200); // Delay 200ms


// Turn the LED OFF by writing LOW to PB1

HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);

HAL_Delay(200); // Delay 200ms

However before using this function, the pin must be set to output first and configure the pins properties and the GPIO is properly initialized. The code for this is actually automatically generated in STMCube IDE. These code resides inside the MX_GPIO_Init() function shown below:

static void MX_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct = {0};

/* USER CODE BEGIN MX_GPIO_Init_1 */


/* USER CODE END MX_GPIO_Init_1 */


/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOC_CLK_ENABLE();

__HAL_RCC_GPIOH_CLK_ENABLE();

__HAL_RCC_GPIOA_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();


/*Configure GPIO pin Output Level */

HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);


/*Configure GPIO pin Output Level */

HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);


/*Configure GPIO pin : B1_Pin */

GPIO_InitStruct.Pin = B1_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);


/*Configure GPIO pin : LD2_Pin */

GPIO_InitStruct.Pin = LD2_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);


/*Configure GPIO pin : LED_Pin */

GPIO_InitStruct.Pin = LED_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);


}

In the above code, we can see that the automatically generated C code by STM32Cube has configured the default Port Pins including the pin user labelled LED. It has configured the LED pin which is PB1 pin as an output pin without pullup resistors, low frequency speed and the name being LED_Pin. The complete code for LED blink at pin PB1 in the main.c file is below:

#include "main.h" 

/* Private variables ---------------------------------------------------------*/

UART_HandleTypeDef huart2;


/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART2_UART_Init(void);

 

int main(void)

{

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_USART2_UART_Init();

  while (1)

  {

	 // Turn the LED ON by writing HIGH to PB1

	 HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);

	 HAL_Delay(200); // Delay 200ms

	 // Turn the LED OFF by writing LOW to PB1

	 HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);

	 HAL_Delay(200); // Delay 200ms

  }


}

void SystemClock_Config(void)

{

  RCC_OscInitTypeDef RCC_OscInitStruct = {0};

  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage */

  __HAL_RCC_PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

  /** Initializes the RCC Oscillators according to the specified parameters in the RCC_OscInitTypeDef structure.*/

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

  RCC_OscInitStruct.HSIState = RCC_HSI_ON;

  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

  {

    Error_Handler();

  }


  /** Initializes the CPU, AHB and APB buses clocks */

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;


  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)

  {

    Error_Handler();

  }

}

static void MX_USART2_UART_Init(void)

{

  huart2.Instance = USART2;

  huart2.Init.BaudRate = 115200;

  huart2.Init.WordLength = UART_WORDLENGTH_8B;

  huart2.Init.StopBits = UART_STOPBITS_1;

  huart2.Init.Parity = UART_PARITY_NONE;

  huart2.Init.Mode = UART_MODE_TX_RX;

  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

  huart2.Init.OverSampling = UART_OVERSAMPLING_16;

  if (HAL_UART_Init(&huart2) != HAL_OK)

  {

    Error_Handler();

  }

}

static void MX_GPIO_Init(void)

{

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */

  __HAL_RCC_GPIOC_CLK_ENABLE();

  __HAL_RCC_GPIOH_CLK_ENABLE();

  __HAL_RCC_GPIOA_CLK_ENABLE();

  __HAL_RCC_GPIOB_CLK_ENABLE();


  /*Configure GPIO pin Output Level */

  HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);


  /*Configure GPIO pin Output Level */

  HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);


  /*Configure GPIO pin : B1_Pin */

  GPIO_InitStruct.Pin = B1_Pin;

  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);


  /*Configure GPIO pin : LD2_Pin */

  GPIO_InitStruct.Pin = LD2_Pin;

  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

  HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);


  /*Configure GPIO pin : LED_Pin */

  GPIO_InitStruct.Pin = LED_Pin;

  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

  HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);

}

void Error_Handler(void)

{

  __disable_irq();

  while (1)

  {

  }

  /* USER CODE END Error_Handler_Debug */

}


#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t *file, uint32_t line)

{

}

#endif /* USE_FULL_ASSERT */

Now I like to test and learn microcontroller programming and interfacing with Proteus before I actually build one. Not only that, I think this way you can learn much faster, prototype easier. There are so many circuits and most of the simulation works perfectly in real world after successful simulation. So I have build my own STM32 Nucleo64 Board simulation model for Proteus Simulation. You can download the model for free from the download link below.

Download STM32 Nucleo Simulation Model

The following is the circuit diagram for this STM32 programming tutorial.

LED blink with STM32 Nucleo-64 board

 Now I have already explained in previous LED Blink with STM32 Nucleo64 board how to use the hex file generated by STM32Cube IDE with a video which is again provided below.

 

I hope this guide was useful to you and leave comments if you have questions or whatever.

Post a Comment

Previous Post Next Post