STM32 From scratch

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:

No alt text provided for this image

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)

要查看或添加评论,请登录

Mohammad Daneshgar的更多文章

  • Qt Multithreading and Concurrency

    Qt Multithreading and Concurrency

    There are multiple ways to utilize threads in Qt. Here I show 5 ways: 1) First method is very similar to STD C++.

  • STM32 Bare-Metal

    STM32 Bare-Metal

    Microcontroller From scratch with STM32 : Lesson One In lesson zero we did a simple example using HAL drivers. Here we…

  • Throws and Self defense

    Throws and Self defense

    I've been practicing martial arts for more than two decades. I still train two times a day, one-hour session dedicated…

  • 17 Things To Do After After Installing Ubuntu 20.04

    17 Things To Do After After Installing Ubuntu 20.04

    Create Progs (working directory for programming) and Books $ mkdir Progs Books 2. Install vim Text Editor $ sudo apt…

    4 条评论
  • VHDL & C++

    VHDL & C++

    This prelude is neither about VHDL nor about C++ indeed it's about "to add some C++ codes to VHDL codes" or to put it…

    1 条评论
  • ???? ?? ?????? ??? ? ??? ??? ?? ?? ?? ?????

    ???? ?? ?????? ??? ? ??? ??? ?? ?? ?? ?????

    ?? ??? ??? ???? ??? ??? ? ??? ???????? ?? ???? ?? ????? ????? ?? ?? ???? ???. ?? ????? ?? ????? ?? ???? ???? ???? ?????…

  • Linux & EDA

    Linux & EDA

    Using Linux as your quotidian OS isn't just for computer savvies, coders or hackers, Linux is also the best bet of…

    1 条评论
  • From 5.6-liter V8 Sedan to 2.0-liter I4 crossover

    From 5.6-liter V8 Sedan to 2.0-liter I4 crossover

    I'm #survivalist and the catastrophe I'm preparing for is 2.0-liter I4 SUVs and illiterate celebrities being professors…

  • Smoldering Passion

    Smoldering Passion

    At one time, this was an adage to live by: follow your passion. At present, they say it’s “Awful”, “Crappy”, “Dump”…

社区洞察

其他会员也浏览了