LED Blinking using STM32

After working a lot with 8-bit microcontrollers and learning all the protocols and functions they offer, now I thought to switch to 32-bit microcontrollers. I decided to go with arm 32-bit processors, because of their popularity in the market. I choose the arm cortex-m3 processor series for my new hobby/learning projects. The reason behind choosing the arm cortex-m3 series is cortex-m3 processors are specially made for connected embedded applications, and the microcontrollers built with this series are used in many mid-levels of embedded projects/applications/products. After so much googling i finally decided to move forward with stm32 microcontrollers. Stm32 is a family of 32-bit microcontrollers offered by STMicroelectronics. Stm32 microcontrollers are built around Cortex-M7, Cortex-M4F, Cortex-M3, Cortex-M0+, and Cortex-M0 processors.

After decided to go with stm32, i started to took initial tutorials on how to get started with stm32. I found much information on getting started but non of it is well organized. So i decided to make a series of tutorials on getting started with stm32 microcontrollers. In this whole series, i will discuss all the protocols/functions of stm32 microcontrollers offered. I will present a working example of an easy protocol/interface/function with source code and circuit diagram.

 ​There are many IDE’s(Integrated development environments) that supports the stm32 series and you can use any one of them to program your stm32 microcontroller. Some Ide’s are Coocox, Keil, mBed, Attolic, microC for Arm. I decide to go with stm32CubeIDE. STRIDE gives you in-depth knowledge of the microcontroller and its interface. If you don’t want to go in depths and want a piece of cake then go with MikroElectronica microC for Arm. It’s very easy to work with microC ide, it has plenty of example and libraries you only need to call the functions and it’s all done. Stm32CubeMx is a microcontroller peripheral configuration. By using stm32cube you don’t need to write configuration code for your stm32 microcontroller. Its a visual platform where you can make the microcontroller pins input, output, enable pull-ups and pull-down can define the operating frequency of the microcontroller visually and a lot more. After the visual configuration, you can generate code for the configuration you made. I prefer to work with stmCubeIDE because it is provided by officially STMicroelectronics and its good to work with the stuff provided by the owner. 

Note: Stm32CubeMx is not an Ide its a configuration manager. You make your stm32 microcontroller configuration in it and then generate code for the configuration you have done to be used with any other ide. You can directly generate the STMIDEide project with stmCubeIDE by selecting the option from stmCubeIDE to translate the configuration into STMIDE ide project

STM32F303

STM32F103C8T6 is a 32-bit microcontroller manufactured by STMicroelectronics having ARM Cortex-M3 Architecture. The part number indicates the below specifications :

  • STM : Manufactures name STMicroelectronics
  • 32 : 32-bit ARM architecture
  • F103 : Indicate the architecture ARM Cortex M3
  • C : 48 pins
  • 8 : 64 KB Flash Memory
  • T : package type LQFP
  • 6 : Operating Temperature – 40°C to 85°C

Specifications

  • Digital Pins : 32 pins
  • Analog Pins : 10 pins
  • PWM Pins : 15 pins
  • ADC Resolution : 12 bit
  • PWM Resolution : 16 bit
  • 2x I2C , 2x SPI , 1x CAN , 2x USART , 1x USB .
  • Internal Flash : 64 KB
  • SRAM : 20 KB
  • Max. Clock Frequency : 72 MHz
  • Operating Voltage : 2.0 to 3.6 V
  • Debugging : Serial, JTAG

Programming

How to Generating Code with STM32CubeIDE

Generating the code kindly find the below attachment

Click here to know how to generate code using STMIDE

open main.c file:

Now, Scroll Down until you find “While(1)”:

This is where you will write your Code.

Now, go into while loop and enter following line of code:

Edit main.c to

Method 1 HAL_GPIO_TogglePin()

HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

/**
  * @brief  Toggle the specified GPIO pin.
  * @param  GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family
  * @param  GPIO_Pin specifies the pin to be toggled.
  * @retval None
  */
  HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14);
	  HAL_Delay(1000);

Method 2

Blink the led using HAL_GPIO_WritePin()

HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)

/**
  * @brief  Set or clear the selected data port bit.
  *
  * @note   This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
  *         accesses. In this way, there is no risk of an IRQ occurring between
  *         the read and the modify access.
  *
  * @param  GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family
  * @param  GPIO_Pin specifies the port bit to be written.
  *         This parameter can be one of GPIO_PIN_x where x can be (0..15).
  * @param  PinState specifies the value to be written to the selected bit.
  *         This parameter can be one of the GPIO_PinState enum values:
  *            @arg GPIO_PIN_RESET: to clear the port pin
  *            @arg GPIO_PIN_SET: to set the port pin
  * @retval None
	  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_SET);
	  HAL_Delay(1000);
	  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
	  HAL_Delay(1000);

Method 3

 Create you own led blink macro name

#define led_HIGH_PB15 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_SET)
#define led_LOW_PB15 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET)

Inside of while(1)

  while (1)
  {
    /* USER CODE END WHILE */
 	  /////////////////////// create you own led function   //////////////

	  led_HIGH_PB15;
	  HAL_Delay(1000);
	  led_LOW_PB15;
	  HAL_Delay(1000);

    /* USER CODE BEGIN 3 */
  }

Debug the project

Click on the Debug toolbar icon  to start the debug session (or in the menu, select Run > Debug).
Click on Resume icon  to continue the execution.
Now watch the green LED (LD2) toggling on the Nucleo-L476RG board.

Now you are able to

  • create a new project using STM32CubeIDE.
  • configure a project in STM32CubeIDE and generate the initialization code.
  • update the project code in STM32CubeIDE using HAL functions.
  • execute a project in debug mode.
  • make a LED blink.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: