***********************************************************************************************************************************************************
Development board: STM32
cpu :STM32F103
Development environment: keil uVsion4
*************************************************************************************************************************************************************
Preface: After two days, I finally built the development environment and wrote the first entry program of 32. We know that the first thing to do when playing with a single-chip microcomputer development board is to light up the LED light. Because I have played with the arm development board before, I also learned STM32 according to this idea! Because there are a lot of reference materials on the Internet, I feel that the learning process is quite smooth! Learning 32 has library development and register development, because library development is more efficient, so
The rest of the STM32 learning journey will be based on the library development model!
Preliminary preparation:
1. MDK installation (mkd453)
2. J-LINK download and installation (Setup_JLinkARM_V410i.exe)
3. J-LINK cable or serial cable (download program to development board)
1. New construction project
(1) Click project->NEW uVision Project to create a new project file. You can name the file as you like and save the file in the path you set.
(2) The next window is for selecting the company and chip model. I use ST's STM32F103VE, so select STM32F103VE and click OK.
(3) Next, you will be asked whether to start the code into the project file. Here I use the ST library, so click "No" (usually you should click "No").
(4) The project is created successfully, but our project does not have any files. Next, add the required files manually.
(5) Copy the official STM32 official library to the folder under the path we set and create 5 new folders: USER, FWlib, CMSIS, Output, Listing
*FWlib is placed in the inc and src folders under the \Libraries\STM32F10x_StdPeriph_Driver folder
*USER放main.c stm32f10x_conf.h,stm32f10x_it.h和stm32f10x_it.c
*CMSIS puts all folders under \Libraries\CMSIS\CM3,
(6) Return to the project. Under Target, select ADD Group to create STARTCODE, USER, FElib, CMSIS. The code to be placed in it is as shown in the figure:
(7) Set parameters output, Listing, c/c++, Debug, Utilities
( 8 ) Jlink download settings
Second, write main.c
#include "stm32f10x.h"
GPIO_InitTypeDef GPIO_InitStructure;
#define LED2_ON GPIO_SetBits(GPIOD,GPIO_Pin_3)
#define LED2_OFF GPIO_ResetBits(GPIOD,GPIO_Pin_3)
void RCC_Configuration(void);
void LED_Config(void);
void Delay(__IO uint32_t nCount);
void LED_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOD,ENABLE);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
}
int main(void)
{
RCC_Configuration();
LED_Config();
while(1)
{
LED2_ON;
Delay(0xAFFFF);
LED2_OFF;
Delay(0xAFFFF);
}
}
void Delay(__IO uint32_t ncount)
{
for(;ncount != 0;ncount--);
}
void RCC_Configuration(void)
{
SystemInit();
}
3. Main program compilation, connection and debugging
4. Use J-link to download to the development board and run
Click "LOAD"
It was found that the LED light on the development board was flashing successfully. . .
5. Main.c Programming Analysis
First, we want to program to light up the LED, which is divided into several steps:
1. Initialization structure—GPIO_InitTypeDef type
typedef struct
{
uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */
GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIOSpeed_TypeDef */
GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;
2. Initialization library function—GPIO_Init()
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
uint32_t tmpreg = 0x00, pinmask = 0x00;
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
/*---------------------------- GPIO Mode Configuration -----------------------*/
currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)
{
/* Check the parameters */
assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
/* Output mode */
currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
}
......
}
3. Enable the peripheral clock
The clock PCLK2 used by GPIO adopts the default value of 72MHz. To enable the GPIOD pin, we usually call the RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE) function. If you want to enable other gpio pins at the same time, change the function parameter to the corresponding pin.
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
RCC->APB2ENR |= RCC_APB2Periph;
}
else
{
RCC->APB2ENR &= ~RCC_APB2Periph;
}
}
**************************************************************************************************************************************************
Note: Before turning on the peripheral clock, you must first configure the system clock SYSCLK. To configure SYSCLK, you need to set a series of clock sources, frequency multiplication, frequency division and other control parameters. These tasks are completed by the SystemInit() library function. When the chip is reset (including power-on reset), the startup_stm32f10x_hd.s assembly code will start running. This assembly code first calls SystemInit() and then enters the "__main" function in C language to execute. This is an initialization function of the C standard library. After executing this function, it will eventually jump to the "main" function interface in the user file and start running the main program.
***************************************************************************************************************************************************
4. Control I/O output high and low level
(1) Call GPIO_SetBits() function to control the output high level.
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BSRR = GPIO_Pin;
}
(2) Call GPIO_ResetBits() function to control the output low level.
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BRR = GPIO_Pin;
}
5. Fill in the GPIO_InitTypeDef structure
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
Of course, this should refer to the schematic diagram of the stm32 development board:
According to this schematic, we can change the configuration of LED_Config(void), GPIO_SetBits(), and GPIO_ResetBits() functions! Then we can light up the three LEDs.
Previous article:A brief analysis of assert_param function in STM32
Next article:stm32 bootloader update firmware restart IWDG independent watchdog
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- 【Multi-function open source custom macro keyboard】OLED display test
- What model is this silk-screened chip?
- SOPC technology and Nios-II soft-core processor
- Microwave Communications
- I made a development board based on your suggestions.
- During the STM32 hardware simulation debugging process, the system stops at a certain point and does not run further.
- Please help me solve my doubts if you know how to use 6N317 chip
- Morris Chang: China's semiconductor manufacturing lags behind Taiwan by five years
- 【XMC4800 Relax EtherCAT Kit Review】01- Unboxing Photos---Kangaroo Brother
- Wireless charging mouse pads from Rapoo and Jingzao are waiting for you to take them apart - EEWorld invites you to play disassembly (Part 3)