We are going to create a LED Blink Program for stm32f103 microcontroller which comes with stm32 Bluepill development board which is cheap and handy to use. stm32 led blink program will help you to understand the basics of GPIO’s of STM32 platform and help you to understand some basics of using these GPIO as Output.
LED Blinking is considered the Hello world Program for any embedded development project. If you are using stm32 Cortex M series first time then this article is for you. Any Person who want to start any kind of microcontroller programming and want to understand the programming concept of that programming language will had to create first hello world program and in our case or in embedded development case the LED Blinking program.
This program will help us to understand following concepts required as a foundation of this cortex m series microcontrollers.
- Help us to understand how to create and build a project using Cubemx software from ST
- How to use GPIOs of STM32F103 microcontroller?
- How to program a GPIO as an OUTPUT ?
- What different Options are available for us by using any PIN as an Output?
- How to use Delay and how to create a simple infinite loop?
So lets just get started…
another good instructable on this topic could be found here
Creating our project in STM32 CubeMX
- First of all Open Cubemx software and after clicking on “new project” select your microcontroller which in our case is stm32f103c8.
- Now double click on name of microcontroller from right panel.
- After that a new window will open from here change RCC of High Speed Clock or HSC to Crystal/ceramic type
- and change Debug option from SYS menu to Serial Wire
- Now from right panel where the chip is shown click on the pin to which you want to attach LED on, and later want to blink that LED, and click on that pin which in our case is PB9 and select GPIO_output from the menu which will appear in result of clicking.
- Now go to project settings and here select your compiler which is MDK4 in our case and type the name of the project and change the location of that project if you want and click OK.
Thats all. Now You had successfully created your project go to the directory your told cubemx to save project into, and load your project in Keil uV4 or other compiler if you created for some compiler other then keil uVision 4.
Coding in Keil for stm32 LED blink
Now Open the project in Keil uVision 4 and goto Application/User and select main file
And finally these two lines will do rest of the magic
HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_9); //Toggle LED At GPIOB, pin9 HAL_Delay(1000); //Delay 1 Seconds