STM32 learning notes (1): Use of GPIO ports

Publisher:VS821001Latest update time:2015-09-07 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
After a long time of trial and error, I finally managed to light up the LED on the ARM development board. Although it was a simple IO port operation, I had no experience before, so I wasted a lot of time and searched a lot of information. Now I can operate the IO port, which proves that I have taken the first step in learning ARM.

The experimental platform list is as follows:

Development board: STRIVE V3

Core chip: STM32F103VET6

Development environment: RealView MDK-ARM Version: 3.50

PC operating system: Windows 7 Home Basic Edition

Emulator: SEGGER J-Link

Among them, the STM32F103VET6 chip is based on the ARM Cortex-M3 core. For specific technical parameters, please refer to the chip information provided by ST (http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00191185.pdf

). For more information about this chip, please visit

http://www.st.com/cn/mcu/product/164491.jsp

上找到。如果对RealView MDK不熟悉的话,可以参考ARM RealView系列丛书《ARM开发工具RealView MDK使用入门》,李宁编著,北京航空航天大学出版社出版。如果对于STM32不熟悉的话,可以参考ARM RealView MDK系列丛书《基于MDK的STM32处理器开发应用》,李宁编著,北京航空航天大学出版社出版。

When the hardware and software platforms are ready, you can start a new project. For a beginner, it is difficult to create a new project that can run, because you don't know where to start. Therefore, I will describe each step in detail so that even beginners can understand the basic operation of ARM.

Open the MDK development platform and click "Project - New μVision Project" in the menu bar to create a new project. Then select the appropriate chip in the pop-up "Select Device for Target 1" dialog box. Since I use STM32F103VET6, I choose ST - STM32F103VE.

After selecting the chip, a message box will pop up, "Copy STM32 Startup Code to Project Folder and Add File to Project?" asking if you need to load the startup code. Select "Yes" and enter the project.

The so-called startup code is a piece of code executed by the processor when it starts up. Its main tasks are to initialize the processor mode, set the stack, initialize variables, etc. Since the above operations are closely related to the processor architecture and system configuration, they are generally written in assembly. For beginners, it is difficult to design startup code by themselves. The MDK development platform has built-in startup codes for some commonly used chips. Therefore, when creating a new project, it is best to use the default startup code. Of course, chip manufacturers will also write some startup codes themselves and put them on the official website for developers to download.

After entering the project, we can start writing code. First, we need to create a new file and save it in *.c format, so that the development environment can recognize some common keywords and other information in the written code. I will save it directly as main.c. Then right-click Source Group 1 in the Project Workspace on the left side of the screen, select Add Files to Group "Source Group 1", and add the main.c we just saved to Source Group 1, or you can directly double-click Source Group 1 to add files.

Next, you can start writing code. For beginners, the most basic operation should be the operation of the chip IO port. Therefore, when I was learning ARM, the first project I chose to make the three LED lights on the development board light up in sequence. There are a total of 7 groups of general-purpose input and output interfaces (General-Purpose Inputs/Outputs) in STM32F103VET6. Each GPIO pin can be configured by software as an output (push-pull or open-drain), input (with or without pull-up or pull-down) or multiplexed peripheral function port. Most GPIO pins are shared with digital or analog multiplexed peripherals. For specific details, please refer to the Datasheet. In the book "STM32 Processor Development and Application Based on MDK", "7.1 General IO Port" describes in detail the functions, register formats and other related information of each port, so I will not go into details here.

Back to the MDK development platform, now we need to add relevant code to main.c. The code list is as follows:

#include "stm32f10x_lib.h"

 

int main()

{

       int i;

       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOB, ENABLE); //Enable the peripheral clock

       GPIOD->CRL = 0x33333333; //Set port configuration register

       GPIOB->CRL = 0x33333333;

       while(1)

       {             

              GPIOD->ODR = 0xffffffbf; //Set port output register

              for(i=0;i<1000000;i++);                                   //延时

              GPIOD->ODR = 0xffffffff7;

              for(i=0;i<1000000;i++);

              GPIOD->ODR = 0x00000000;

              GPIOB->ODR = 0xffffffff;

              for(i=0;i<1000000;i++);

              GPIOB->ODR = 0x00000000;

       }

}

In the above code, #include "stm32f10x_lib.h" contains the basic header files required for developing stm32f10x series chips. When writing programs, be sure to include this header file. [page]

The RCC_APB2PeriphClockCmd() function is used to set the peripheral clock. The difference between ARM and C51 microcontrollers is that when peripherals are not in use, such as IO ports, ADC, timers, etc., the clocks are disabled to achieve energy saving. Only the peripherals that are needed are turned on. Therefore, when GPIOB and GPIOD are needed, we need to turn on their clocks first. The specific functions used are those in the function library:

void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)

Among them, the first parameter needs to indicate which port's clock to enable, RCC_APB2Periph_GPIOx is to enable the GPIOx clock, and the second parameter needs to indicate whether to enable or disable, ENABLE/DISABLE.

After turning on the peripheral clock, the GPIO configuration registers are set. For specific settings, refer to "7.1 General IO Port" in the book "STM32 Processor Development and Application Based on MDK". The while loop assigns values ​​to the GPIO port output registers. Since the three LED lights of this struggle development board are connected to D3, D6 and B5 respectively, it is enough to set the corresponding bits of the D port and the B port to 1.

After compiling, we found that the compiler reported an error, Undefined symbol RCC_APB2PeriphClockCmd, because the RCC_APB2PeriphClockCmd() function we used was declared in the header file but not defined in the C file. This function is in .. KeilARMRV31LIBSTSTM32F10xstm32f10x_rcc.c. Copy this file to the root directory of the project, and then add it in the Workspace on the left side of the screen.

As for how to download to the ARM development board, different development boards have different methods, and development board manufacturers generally provide relevant documents together with the development board, so I will not go into details here.

In fact, in the MDK library, many macros are defined to avoid us looking up relevant information to set each bit of the register. For example, in this experiment, the on and off of the LED can also be achieved through the following code.

 

#include "stm32f10x_lib.h"

 

int main()

{

        

       int i;

       GPIO_InitTypeDef GPIO_InitStructure; //Define GPIO macro operation structure

 

       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOB,ENABLE); //Peripheral clock configuration, turn on the clocks of GPIOB and GPIOD   

 

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;                               

       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Configure port B5 as general push-pull output

       GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; //Port line flip speed is 50MHz

       GPIO_Init(GPIOB, &GPIO_InitStructure); // Place GPIOB port

      

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_3; //Configure D3 and D6 ports as push-pull output

       GPIO_Init(GPIOD, &GPIO_InitStructure); // Place GPIOD port

 

       while(1)

       {

 

 

              GPIO_SetBits(GPIOB, GPIO_Pin_5); //Port B5 outputs high level

              GPIO_ResetBits(GPIOD, GPIO_Pin_6); //Port D6 outputs low level

              GPIO_ResetBits(GPIOD, GPIO_Pin_3); //Port D3 outputs low level

 

              for(i=0;i<1000000;i++);

 

              GPIO_ResetBits(GPIOB, GPIO_Pin_5);

              GPIO_ResetBits(GPIOD, GPIO_Pin_6);

              GPIO_SetBits(GPIOD, GPIO_Pin_3);

 

              for(i=0;i<1000000;i++);

 

              GPIO_ResetBits(GPIOB, GPIO_Pin_5);

              GPIO_ResetBits(GPIOD, GPIO_Pin_3);

              GPIO_SetBits(GPIOD, GPIO_Pin_6);

 

              for(i=0;i<1000000;i++);

       }

}

 

Since we used the GPIO_InitTypeDef type, we need to find its definition, which is contained in "…KeilARMRV31LIBSTSTM32F10xstm32f10x_gpio.c". Copy the file to the project root directory, and then add it to the project so that the compilation will not report an error.

In most C compilers, all variable declarations are required before executing the statement block. That is to say, if the variables need to be defined, they must be defined at the beginning of entering the main function. If the variables are defined after a certain statement is executed, an error will be reported.

Keywords:STM32 Reference address:STM32 learning notes (1): Use of GPIO ports

Previous article:STM32 learning notes (2): Use of external interrupts
Next article:Regarding the problem of configuring the input of STM32 as pull-up input

Recommended ReadingLatest update time:2024-11-16 15:42

stm32 memory allocation
(1) Stack: It is automatically allocated and released by the compiler to store function parameter values, local variable values, etc. Its operation is similar to A stack in a data structure. (2) Heap: Generally allocated and released by the programmer. If the programmer does not release it, it may be reclaimed
[Microcontroller]
STM32 BOOT mode configuration and function
1. Introduction to three BOOT modes The so-called boot, generally speaking, means that after we download the program and restart the chip, the value of the BOOT pin will be latched at the fourth rising edge of SYSCLK. Users can select the boot mode after reset by setting the state of the BOOT1 and BOOT0 pins. Main F
[Microcontroller]
STM32 BOOT mode configuration and function
How to view the detailed and accurate address of the memory in stm32
Because I have been reading Chinese reference manuals and hal library function development guides before, some content may be different from the addresses used in practice, so how do I see the memory address of a specific chip? 1. Open the project and find the header file stm32f767xx.h, which is around line 1312. Use
[Microcontroller]
The STM32 DC fan can adjust the speed by pressing PWM and the parameters are displayed on the LCD.
I still remember that after I finished learning STM32, I felt like I knew nothing, and basically relied on the knowledge of microcontrollers to get through the final test. Then, I actually always wanted to learn STM32 well, but I felt that learning too much was not a good thing. I always felt that it was not a bad thi
[Microcontroller]
STM32 general timer output PWM function study notes
First of all, if you want to use PWM mode, you have to choose which timer to use to output PWM! Except for the two ordinary timers TIM6 and TIM7, which cannot output PWM, the rest of the timers can output PWM. Each general timer can output 4 channels of PWM, and the advanced timers TIM1 and TIM8 can output 7 channels
[Microcontroller]
STM32 defines constant array to FLASH fixed address
Method: static const uint8_t s_acBmpLogo030 __attribute__((at(0X800F000)))={0x80,0xC0,0xC0,0xC0,0xC0,0x80,xxxxxxx}   After compiling, you can see the assigned address in the .map file 0x0800f000   0x0000005c   Data   RO         4013    .ARM.__AT_0x0800F000  main.o   This is the flash data viewed from the debug window
[Microcontroller]
STM32 defines constant array to FLASH fixed address
The difference between the regular channel and the injection channel of the STM32 ADC
Each ADC module of STM32 can switch to different input channels and perform conversions through the internal analog multiplexer. STM32 has specially added a variety of group conversion modes, which can be set by the program to automatically sample and convert multiple analog channels one by one. There are two ways t
[Microcontroller]
Design of touch screen for battery management system based on STM32
0 Introduction Electric vehicles have always been a focus of attention for their cleanliness and environmental protection. With the intensification of the energy crisis and the continuous rise in oil prices, electric vehicles are becoming more and more popular among users. Electric vehicles are generally po
[Automotive Electronics]
Design of touch screen for battery management system based on STM32
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号