STM32 From scratch
Microcontroller From scratch with STM32 : Lesson Zero
First off you have to install STM32CubeIDE and have a STM32 board. The best bet is STM32 Nucleo Boards. For this tutorial, I use NUCLEO-F103RB.
Start STM32CubeIDE:
File => New => STM32 Project
MCU/MPU Selector : STME32F103RBT6 (select the MCU of your board)
Project Name: lsn0 (lesson zero)
Targeted language : C
Targeted Binary Type: Executable
Targeted Project Type: Empty (NOT STME32cube)
Project Explorer => lsn0 > Src > main.c : open the main.c and clear the file and wire the following
int main() {}
Press (Ctrl + B) to build the project. You have to receive a message akin to the below: 14:04:46 Build Finished. 0 errors, 0 warnings. (took 147ms)
To use HAL drivers, we have to do the following:
1) Add Drivers folder containing headers and source files in your project folder in the workspace. Build the project again to see the Drivers folder in your Project Explorer.
2) In the Project Explorer right click lsn0 and then select Properties:
C/C++ General > Paths and Symbols
Includes => Add... => File system...
领英推荐
\lsn0\Drivers\CMSIS\Include
\lsn0\Drivers\CMSIS\Device\ST\STM32F1xx\Include
\lsn0\Drivers\STM32F1xx_HAL_Driver\Inc
Symbols => Add...
USE_HAL_DRIVER
STM32F103xB
Source Location => Add Folder... > Drivers
Click Apply and Close on Properties for lsn0
3) Add "stm32f1xx_hal.conf.h" to lsn0 > Inc
4) Project Explorer > lsn0 > Src > New > Source file > system_stm32f1xx.c
#include <stdint.h>
#include "stm32f1xx_hal.h"
uint32_t SystemCoreClock = 16000000;
void SysTick_Handler(void)
{
HAL_IncTick();
}
Now that your HAL Drivers is ready. Add the following code to the main.c
#include "stm32f1xx_hal.h"
uint8_t bntIn;
int main()
{
HAL_Init();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitTypeDef led={0}, bnt={0};
led.Pin? ?= GPIO_PIN_5;
led.Mode? = GPIO_MODE_OUTPUT_PP;
led.Pull? = GPIO_NOPULL;
led.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA,&led);
bnt.Pin? ?= GPIO_PIN_13;
bnt.Mode? = GPIO_MODE_INPUT;
bnt.Pull? = GPIO_NOPULL;
bnt.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC,&bnt);
while(1)
{
bntInput = HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,bntIn);
}
}
Ctrl + S => Ctrl + B => F11 (Debug) => OK => Switch
In Live Expressions Tab add bntIn as Expression:
Press F8 (Resume) : the LED on your board must turn on and the value of bntIn must became 1 '\001' and once you press the blue push button on your board the LED must turn off and the value of bntIn must became 0'\0'. : Press Ctrl + F2 (Terminate)