STM32 Getting Started: GPIO

Publisher:JoyfulHarmonyLatest update time:2015-10-14 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
    I have been using STM32 for a while. I felt it was difficult to get started and I didn't know where to start. Now I have learned a little bit and I would like to share it with you.

    First of all, what is GPIO? The answer to this question is that I don’t know either! At least I don’t need to know it for now. I just need to know that it is actually the IO port of the 51 microcontroller. The difference is that the IO port of the 51 microcontroller does not require configuration mode, while the IO port of the STM32 (oh, no, it should be said to be the GPIO port, ╮(╯▽╰)╭, it’s really confusing) needs to configure the mode and clock (well, another clock pops up, which is troublesome enough). Here I want to explain that if you want to use the GPIO of the STM32, you have to do two steps. The first is to configure the mode and speed, and the second is to configure its clock and enable it (it seems a bit far-fetched to say two steps). I guess everyone is confused now. Okay, don’t worry, I will explain it to you bit by bit;

    The first step is to know what the eight modes of GPIO are and how to use them. The eight modes of GPIO are as follows:

    (1) Floating input: In_Floating

    (2) With pull-up input: IPU (In Push_Up)

    (3) With pull-down input: IPD (In Push_Down)

    (4) Analog input: AIN (Anolog In)

    (5) Open drain output: OUT_PD (OD stands for open drain, OC stands for open collector)

    (6) Push-pull output: OUT_PP (PP stands for push-pull, Push_Pull)

    (7) Alternate-function push-pull output: AF_PP (AF stands for alternate function, Alternate-Function)

    (8) Multiplexed function open-drain output: AF_OD

    Well, just these eight modes, my hands are sore from typing...

    It is worth studying when to use which of the eight modes, but you don’t have to study it hard now. You will gradually understand it during use, so I won’t say more here.

    The next step is to configure the GPIO flip speed, which can be 10M, 2M and 50M. You will understand the specific configuration method by pasting the complete code at the end.

    Now that we have talked about GPIO configuration, let's talk about clock configuration. Clock configuration is actually a bunch of codes that beginners cannot understand, but after reading it more and slowly understanding it, you will understand what he is talking about. When writing a program, you can directly transplant it from the project he gave, and then modify the relevant configuration. I will post a code first, and then explain it.

STM32 study notes GPIO
The picture above is the code for configuring the clock. I will explain it line by line:

SystemInit(); As the name suggests, this is system initialization. I don’t know what is initialized, and I don’t need to know it at present. Just write this sentence;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA

                       |RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC

                       |RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE

                                            |RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO

                       |RCC_APB2Periph_SPI1, ENABLE );

These sentences are a bit long and it makes me dizzy just reading them, but we are not afraid, let's read them one by one, the name of the function RCC_APB2PeriphClockCmd() is a bit strange, it is called enabling or disabling the APB2 peripheral clock (this name is confusing enough), don't worry about it, just copy it directly, and then look at the brackets, RCC_APB2Periph_USART1, this means that the clock of USART1 is the same as that of APB2, and the following ones have the same meaning, connected together with the or symbol, and then add an ENABLE, this enables the pins you want to use, everyone should pay attention, the above code is just something I found at random, in actual use, what can be enabled depends on what you need, and it is best not to enable what you don't need, so as not to cause unnecessary interference and trouble.

    Okay, now that we have discussed the two questions at the beginning, let's talk about how to make GPIO output high and low levels. In fact, this question is very simple. Many materials start talking about registers when they mention this question. When they talk about registers, they involve the internal principles. Well, finally, beginners lose their composure, their brains become a pile of paste, and then their confidence is frustrated, and then their learning efficiency is reduced. In fact, I personally think that it is unnecessary. Since there are library functions, we can use the library functions first, and when we are familiar with the library functions, we can slowly understand the register operations in the library functions. Look at the following two statements:

GPIO_SetBits(GPIOD, GPIO_Pin_9);

GPIO_ResetBits(GPIOD, GPIO_Pin_9);

The first sentence means to set the GPIOD9 port (we assume that the mode of this port is output mode), and the second sentence means to clear the GPIOD9 port (same assumption). Haha, how about it, simple, this is the basic IO port operation, in this way, you can successfully light up or turn off the specified LED, and a routine is attached below, you can study it slowly.

#include "stm32f10x.h"

 

//-------------------------------------------GPIO initialization-----------------------------------------//

void GPIO_Configuration(void)

{

       GPIO_InitTypeDef GPIO_InitStructure;

      

        

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;

      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

      GPIO_Init(GPIOD, &GPIO_InitStructure);

 

 

      

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

      GPIO_Init(GPIOB, &GPIO_InitStructure);

 

}

 

//--------------------------------------Configure the system clock and enable the clocks of various peripherals---------------------------------//

void RCC_Configuration(void)

{

       SystemInit(); 

       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB

                           | RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD

                           | RCC_APB2Periph_GPIOE, ENABLE );

}

 

 

//-----------------------------------Configure all peripherals------------------------------//

void Init_All_Periph(void)

{

       RCC_Configuration();  

       GPIO_Configuration();

}

//----------------------------------------Delay function--------------------------------------------//

void Delay(vu32 nCount)

{

  for(; nCount != 0; nCount--);

}

 

//------------------------------------------Main function--------------------------------------------//

int main(void)

       Init_All_Periph();

      while(1)

      {

          

           GPIO_SetBits(GPIOD, GPIO_Pin_8);

          

           GPIO_ResetBits(GPIOD, GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11);

          

           Delay(0xEFFFF);

 

          

           GPIO_SetBits(GPIOD, GPIO_Pin_9);

          

           GPIO_ResetBits(GPIOD, GPIO_Pin_8 | GPIO_Pin_10 | GPIO_Pin_11);

          

           Delay(0xEFFFF);

 

          

           GPIO_SetBits(GPIOD, GPIO_Pin_10);

          

           GPIO_ResetBits(GPIOD, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_11);

          

           Delay(0xEFFFF);

 

          

           GPIO_SetBits(GPIOD, GPIO_Pin_11);

          

           GPIO_ResetBits(GPIOD, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10);

          

           Delay(0xEFFFF);

           GPIO_ResetBits(GPIOD, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10);

      }

}

Reference address:STM32 Getting Started: GPIO

Previous article:stm32 IAR environment printf function call
Next article:GNU ARM Assembly - (I) Introduction

Latest Microcontroller Articles
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号