This article was first published on my blog huanghaozi. Cn, original link
Recently in the study of STM32, watching a teaching video of peach electronics feel good, I bought the development board back to struggle. However, WHEN I first saw the code in the video, I had a lot of doubts, because the video did not write the code at the beginning, but explained the code that had been written.
They have done a lot of packaging to write good code abstract, such as sys. J h, leds. H is not some official firmware database, there are a lot of macro definition, I think that increased the difficulty of learning, because the macro is your own definition, convenient for my own use, for other people also need a certain amount of learning and memory, understanding of the nature of the code is not very intuitive.
So I watched some videos from other vendors like Spot Atom and Wildfire, and they were all basically the same. I also read various so-called “minimalist” codes on the Internet that use registers. Finally, I read the “STM32 firmware library user manual” and realized the simplest code of LED lights by myself.
Construction of the Project
For the construction of the project everyone is basically the same, the firmware Library zip package in the Library folder is the ontology. Its file directory tree is listed below:
├─CMSIS │ ├─CM3 │ ├─//CoreSupport -- CMSIS│ ├ ─ ├ ─ garbage │ ├ ─//STM32F10x -- CMSIS│ ├─ ├─ ├─ ├─//arm -- STARTUP│ │ ├ ─ gcc_ride7 │ │ ├ ─ iar │ │ └ ─ TrueSTUDIO │ └ ─ Documentation └ ─//STM32F10x_StdPeriph_Driver -- LIB├ ─ inc └ ─ the SRCCopy the code
The directory marked // contains **. H,. C,. S suffix C/ assembly source files, generally need to be copied out separately **, to establish a project template for easy use.
There are three folders to copy separately: CMSIS, LIB, STARTUP, and actually creating a folder for our own code — User, ST provides a project template for this, not in the Library, It is in the Project–>STM32F10x_StdPeriph_Template folder of the firmware library package. C, stm32f10x_it. C, stm32f10X_it. H, stm32f10x_conf.h.
Then we will create a new project in keil5 and import these folders, as shown below:
Compiler Settings
After importing the file, you need to do some compilation Settings. Click the Magic wand button on the toolbar and set as shown below:
STM32F10X_MD,USE_STDPERIPH_DRIVER Include Paths:.\CMSIS; .\LIB\inc; .\USER
Output Settings, as shown below:
Write the code
The preparatory work
First, you need to clear main.c, add the following simplest code, compile and build again to see if there is any error, if there is any error, you need to check the previous step:
#include "stm32f10x.h"
int main(a)
{
while(1) {}}Copy the code
If there are no errors, you can start programming according to the STM32 firmware library User manual.
The LED lamp circuit on my development board is relatively simple, as shown below:
LED1 is connected to PB0, LED2 is connected to PB1, and PB is actually GPIO port (standard IO port).
Initialize pins
It is mentioned in the user manual of the firmware library that the operation pins must be initialized first, and the specific code writing method is mentioned in Section 2.3 of the manual:
Suppose the peripheral interface is called PPP, first declare a PPP_InitTypeDef structure named PPP_InitStructure, then set the parameters of the structure (different peripherals have different parameters), and finally use PPP_Init function to initialize the peripheral. Also, keep in mind that the clock needs to be initialized before the peripherals are initialized, using the RCC_AHBPeriphClockCmd, RCC_APB2PeriphClockCmd, RCC_APB1PeriphClockCmd functions.
My LED lights are connected to the GPIO port, so I naturally need to refer to some functions for initializing GPIO. I went straight to the GPIO_Init function in the directory of the user manual, which has a very detailed description of its parameters for the reader to look up.
From this, we can write the initialization code:
void LED_Init(a)
{
// The GPIOB clock is initialized
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// Set the GPIO initialization parameters
GPIO_InitTypeDef LED_InitStructure;
LED_InitStructure.GPIO_Pin = GPIO_Pin_0; / / 0 feet
LED_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // The maximum speed is 50MHz
LED_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Push pull output mode
// Perform initialization
GPIO_Init(GPIOB, &LED_InitStructure); / / GPIOB group of pins
}
Copy the code
Light up the LED
Before writing the code to light up the LED, we need to execute the initialization code we wrote above in the main function.
// Add before the while loop
LED_Init();
Copy the code
The next step is to light the LED. According to my circuit, it is necessary to output a high level to the pin to light the LED. Looking at the manual again, I found that the GPIO_SetBits function does this very well.
Add the following code to the main function:
// Add to the while loop
GPIO_SetBits(GPIOB, GPIO_Pin_0);
Copy the code
Build, burn and run, see the effect? ! The general code is as follows, isn’t it very simple:
#include "stm32f10x.h"
void LED_Init(a)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef LED_InitStructure;
LED_InitStructure.GPIO_Pin = GPIO_Pin_0;
LED_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
LED_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &LED_InitStructure);
}
int main(a)
{
LED_Init();
while(1) { GPIO_SetBits(GPIOB, GPIO_Pin_0); }}Copy the code
This article was automatically published by ArtiPub