STM32 study notes 2——GPIO lighting

Publisher:数字狂舞Latest update time:2019-03-29 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

After learning how to build a project, you must be eager to use your own development board to show your skills. Don't worry, take your time. When learning C language, the first thing you compile must be the eternal classic code, yes, that is Hello World - simple, clear, and you can see the phenomenon intuitively. There is also a simple, clear, and intuitive program on STM32 - light up. This is our current hello world, let's start learning from it!!!


study

What we need to use to light up the lamp is to control the I/O port we need, so let's take a look at the GPIO port of STM32F first. Each GPIO port in the STM32F0 series microcontroller has: two 32-bit configuration registers (GPIOx_OTYPER and GPIOx_MODER), two 32-bit data registers (GPIOx_IDR and GPIOx_ODR), a 32-bit position/reset register (GPIOx_BSRR), a 16-bit reset register (GPIOx_BRR) and a 32-bit lock register (GPIOx_LCKR). According to the specific hardware characteristics of each I/O port listed in the data sheet, each bit of the GPIO port can be configured by software into: input floating, input pull-up, input pull-down, analog input, open-drain output, push-pull output, push-pull multiplexing and open-drain multiplexing functions. See Table for details. 


Write the picture description here
Write the picture description here


Each I/O port can be freely programmed, however the I/O port registers must be accessed as 32-bit bytes. The GPIOx_BSRR and GPIOx_BRR registers allow independent access to read/write any GPIO register; 


Therefore, we can program the register to generate square waves at the I/O port to control the light on and off. But first we need to find out which I/O port the LED on the development board is on: Find the schematic diagram of the development board and you will find 


Write the picture description here


The LED is connected to the PA5 port and lights up at a high level. (Since the boards are different, you can find out which port your LED is connected to by yourself. If you can't find it, you can connect it externally.) 

Now let's start programming our development board to light up the lights.


Create a project according to yesterday's project, and continue to add gpio.c and rcc.c in the Lib group.

Come to main.c to write the program.

program

1. Include header files


#include "stm32f0xx.h"


2. Delay function


void Delay(volatile unsigned int ncount)

{

    while(ncount--);

}


3. Define the GPIO initialization structure


GPIO_InitTypeDef GPIO_InitStructure;


4. Enter the main function


int main(void)

{

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //enable the clock

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_P;

  GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOA, &GPIO_InitStructure);


  while (1)

  {

     GPIO_SetBits(GPIOA,GPIO_Pin_5);

     Delay(0xfffff);

     GPIO_ResetBits(GPIOA,GPIO_Pin_5);

     Delay(0xfffff);

  }

}


Next, download the program as written yesterday to the development board. When you see the LED light start to flash, it's done. Is everything done? It's still early. We are just beginning. Let's start analyzing why it is written this way. Knowing the result is not only about knowing the result, but also knowing the reason.


analyze

The delay function is fine (but pay attention to the use of volatile. It doesn't matter in KEIL, but if you use GCC for programming, the non-volatile parts will be optimized away, making the delay function meaningless)

So what does the following GPIO_InitTypeDef GPIO_InitStructure; mean?

There is also the following code in GPIO.h


typedef struct

{

  uint32_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.

                                       This parameter can be any value of @ref GPIO_pins_define */


  GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins.

                                       This parameter can be a value of @ref GPIOMode_TypeDef */


  GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.

                                       This parameter can be a value of @ref GPIOSpeed_TypeDef */


  GPIOOType_TypeDef GPIO_OType; /*!< Specifies the operating output type for the selected pins.

                                       This parameter can be a value of @ref GPIOOType_TypeDef */


  GPIOPuPd_TypeDef GPIO_PuPd; /*!< Specifies the operating Pull-up/Pull down for the selected pins.

                                       This parameter can be a value of @ref GPIOPuPd_TypeDef */

}GPIO_InitTypeDef;


The two pieces of code together are what we are looking for. First, GPIO_InitTypeDef is defined, and then GPIO_InitStructure is defined based on this structure. This structure will be used in the subsequent GPIO settings.


What is the first sentence in the main function RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);? I am configuring GPIO, so what is this? 

In STM32, all clocks are off by default after power-on and need to be turned on manually, so this step is to turn on the clock first and control the clock of GPIOPA on AHB in the M0 core. 

Write the picture description here

So to turn on the clock on PA5, you must enable AHB, in the function

void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)

{

  /* Check the parameters */

  assert_param(IS_RCC_AHB_PERIPH(RCC_AHBPeriph));

  assert_param(IS_FUNCTIONAL_STATE(NewState));


  if (NewState != DISABLE)

  {

    RCC->AHBENR |= RCC_AHBPeriph;

  }

  else

  {

    RCC->AHBENR &= ~RCC_AHBPeriph;

  }

}


The variable uint32_t RCC_AHBPeriph is defined in the library as follows #define RCC_AHBPeriph_GPIOA RCC_AHBENR_GPIOAEN #define RCC_AHBENR_GPIOAEN ((uint32_t)0x00020000) The bitwise OR of 0x00020000 and the register is assigned the value that corresponds to GPIOA to enable.


GPIO Settings 

First assign values ​​to the variables in the structure, set the PA5 port, set the port to output, push-pull output (I have only a limited understanding of analog electronics), and set the output speed to 50MHz. 

Then enter the program to turn the LED on and off, for GPIO_SetBits(GPIOA,GPIO_Pin_5); let's look at its original function:

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;

}


It passes two parameters: *GPIOx, GPIO_Pin. In stm32f0xx.h there are


typedef struct

{

  __IO uint32_t MODER;        

  __IO uint16_t OTYPER;       

  uint16_t RESERVED0;       

  __IO uint32_t OSPEEDR;     

  __IO uint32_t PUPDR;       

  __IO uint16_t IDR;         

  uint16_t RESERVED1;      

  __IO uint16_t ODR;          

  uint16_t RESERVED2;         

  __IO uint32_t BSRR;        

  __IO uint32_t LCKR;         

  __IO uint32_t AFR[2];       

  __IO uint16_t BRR;         

  uint16_t RESERVED3;       

}GPIO_TypeDef;


For the definition of __IO as volatile, you can read this article. Here is the link. Thanks to the author. Through this structure, you should also understand why the BSRR register can be operated. 


Write the picture description here

#define GPIO_Pin_5 ((uint16_t)0x0020) 


So this function sets the fifth bit ODR. After the delay, GPIO_ResetBits(GPIOA,GPIO_Pin_5); is similar to the above, so I won't explain it one by one. 

At this point, the lighting procedure is almost done. You may not be able to write what you have done. The writing process is a process of sublimation and improvement. It can solidify your own abilities, think about yourself, and go through what you took for granted before. You can gain a lot... 

PS: After adding the *.c file in the library, if it prompts that the header file cannot be found during compilation, you need to enable the corresponding .h file in stm32f0xx_cof.h (that is, remove the previous comment).

Keywords:STM32 Reference address:STM32 study notes 2——GPIO lighting

Previous article:STM32 KEY control LED
Next article:STM32 buzzer - library function

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号