STM32 Novice Growth Record---GPIO Usage

Publisher:MengyunLatest update time:2016-12-28 Source: eefocusKeywords:STM32  GPIO Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 

  1. #include "stm32f10x_lib.h"  

  2. #include "stm32f10x.h"  

  3.   

  4. GPIO_InitTypeDef GPIO_InitStructure; //Define GPIO macro operation structure  

  5.   

  6. void Delay(__IO uint32_t nCount) //__IO macro definition volatile  

  7. {  

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

  9. }  

  10.   

  11. /*Initialize the embedded Flash interface and initialize the PLL to reach the system available frequency*/  

  12. void RCC_Configuration(void)  

  13. {     

  14.   /* Setup the microcontroller system. Initialize the Embedded Flash Interface,   

  15.      initialize the PLL and update the SystemFrequency variable. */  

  16.   SystemInit();  

  17. }  

  18. int main()  

  19. {  

  20.         

  21.      int i;  

  22.      RCC_Configuration(); //Initialize FLASH and its PLL, system clock configuration  

  23.        

  24.    

  25.      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB,ENABLE); //Peripheral clock configuration, turn on the GPIOC clock   

  26.      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9;                          

  27.      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Configure the PC6\7\9 port as a general push-pull output  

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

  29.      GPIO_Init(GPIOC, &GPIO_InitStructure); //Configure GPIOC port  

  30.       

  31.        

  32.    

  33.      while(1)  

  34.      {  

  35.           

  36.          GPIO_SetBits(GPIOC, GPIO_Pin_6); //PC6 port outputs high level  

  37.          GPIO_SetBits(GPIOC, GPIO_Pin_7); //PC7 port outputs a high level GPIO_SetBits(GPIOC, GPIO_Pin_9); //PC9 port outputs a high level GPIO_SetBits(GPIOC, GPIO_Pin_9); //PC9 port outputs a high level  

  38.           

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

  40.          GPIO_ResetBits(GPIOC, GPIO_Pin_6);  

  41.          GPIO_ResetBits(GPIOC, GPIO_Pin_7);  

  42.         // GPIO_ResetBits(GPIOC, GPIO_Pin_9);  

  43.         // Delay(0xAFFFF);  

  44.       

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

  46.      }  

  47. }  

  48.  

The specific code project is in my resources: free of charge  http://download.csdn.net/detail/yx_l128125/4494861

Step 1: declare the GPIO structure:

            GPIO_InitTypeDef GPIO_InitStructure;

 

Step 2 is to assign values ​​to the members of the variable GPIO_InitStructure. If we only set some of the members, we need the following code:

/** * LED1->PC6,LED2->PC7,LED3->PC9 */

 

      GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9;

      GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

      GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;

      GPIO_Init(GPIOC,&GPIO_InitStructure);

 

In fact, one function is omitted here:

GPIO_StructInit, which is used to initialize variables

GPIO_InitStructure, after experimentation, I found that it is also OK without it. You can try it:) Then

Modify the members of this variable. There are three members. On the STM32 development board, the GPIO port is connected to

PC6, PC7, PC9 pins. Therefore, we assign GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9 to the GPIO_Pin member.

Assign GPIO_Speed_50MHz to the GPIO_Speed ​​member.

 The GPIO_Mode member is set to GPIO_Mode_Out_PP, indicating push-pull output mode.

 

Push-pull output -> (the output high level current is as large as the output low level current)

     
        The push-pull circuit is a circuit in which two transistors or MOSFETs with the same parameters exist in the circuit in a push-pull manner, each responsible for the waveform amplification task of the positive and negative half cycles. When the circuit is working, only one of the two symmetrical power switch tubes is turned on at a time, so the conduction loss is small and the efficiency is high. The output can either inject current into the load or extract current from the load.

       The push-pull circuit is suitable for low voltage and high current situations and is widely used in power amplifier circuits and switching power supplies.

The advantages are: simple structure, high utilization rate of the switching transformer core, and when the push-pull circuit is working, only one of the two symmetrical power switch tubes is turned on at a time, so the conduction loss is small.

The disadvantages are: the transformer has a center tap, and the switch tube has a high withstand voltage; due to the existence of the primary leakage inductance of the transformer, a large voltage spike will be generated at the drain-source electrode at the moment the power switch tube is turned off. In addition, the input current ripple is large, so the input filter is larger in size.

       The simplified circuit diagram of the push-pull part of the transistor is shown in the figure

 

Step 3: Call the function GPIO_Init() to initialize the peripheral GPIO. The code is as follows:

        GPIO_Init(GPIOC,&GPIO_InitStructure);

   

Step 4: Enable. Note that in the firmware library, GPIO does not have a GPIO_Cmd function, so this step is omitted.

   

Through the above four steps, we have set up GPIO. There is still one problem that cannot be ignored:

Before setting up the peripheral, we must call a clock function to enable the peripheral clock.

    In the CPU user manual, we know that stm32 has several clocks. Which one should we use now?                                      

What about the clock? Open the P25 page of the Chinese version of the "STM32F10xxx Reference Manual" and take a screenshot as shown below.

We are using the GPIOC port, so APB2 is used.

 

Step 5: After determining the function corresponding to this APB2, we call it:

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

 

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.

 

Note that the clock enable function should be placed first. The clock must be available before subsequent operations can be performed.

Let's first look at the schematic diagram to see how the LED is connected:

 

 From the schematic diagram, we can see that in order to make all three LEDs light up, the corresponding pins must be

Clear to 0. Setting to 1 will turn off the LED. Now open the STM32 firmware library document and find section 10.2, GPIO

Library functions. All functions for GPIO settings are here. Let's see which function of the GPIO library we want to use.

Found: GPIO_SetBits(); and GPIO_ResetBits();

    According to the description, these two functions set a pin to high level and low level respectively.

The GPIO_SetBits function sets the high level.

The GPIO_ResetBits function is a reset operation.

With the information obtained, write the following code: Our goal is to make all LEDs flash regularly and in an infinite loop.

 

We also need to call SystemInit() to initialize the entire system, including setting the clock to

72MHZ. After the above configuration is completed, you can download HEX according to the relevant tutorials of MDK+Jlink.

The file is loaded into the board for debugging.

 

JLINK burning method is in my resource: "Struggle version STM32 development board JTAG download steps" http://download.csdn.net/detail/yx_l128125/4494855

 

Promise! I must publish several articles about system clocks - tick timers, ordinary timers, and 485 communications before this Friday. Due to time constraints, I have not been able to organize them into a blog. I promise to put some pressure on myself! I will not embarrass myself!



Keywords:STM32  GPIO Reference address:STM32 Novice Growth Record---GPIO Usage

Previous article:STM32 Novice Growth Record---General Timer Application
Next article:STM32 Novice Growth Record---1

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号