STM32 GPIO - Flowing Light Routine Learning Summary

Publisher:skyshoucangLatest update time:2018-09-13 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

By controlling LED lights, you can become familiar with STM32's memory structure, address mapping, clock tree, library files, library utilization methods, and development project steps, and establish STM32 development ideas.


This is the first project I have built. Since it involves a wide range of areas and the knowledge points are scattered, I will make a summary here.

1) To control the LED light, you need to use the GPIO peripheral.

GPIO (General-Purpose I/O): I/O pins can be set by software to various functions, such as input or output. Controlling the LED light is achieved by controlling the level of the I/O pins of the STM32 chip.

2) Understand the functions of the GPIO peripheral and how to use it.

GPIO pins are divided into different groups: GPIOA, GPIOB, ..., GPIOG. Each group of ports is divided into 16 different pins from 0 to 15. For different chip models, the number of port groups and pins is different.

Function View Reference Manual

3) Get the address mapping of GPIO and know the bus APB2 on which it is mounted.

4) Understand the encapsulation of registers in the ST official library.

5) Understand the clock tree and check the clock source of GPIOC, which is PCLK2.

6) Include the used header files stm32f10x_gpio.h and stm32f10x_rcc.h in the stm32f10x_conf.h file.

7) Add led.c and led.h user files based on the project template.

8) Write the driver initialization function LED_GPIO_Config().

9) Turn on the peripheral GPIOC clock and analyze whether the default clock frequency of Sysclk=71MHz configured by the SystemInit() function meets the project requirements.

10) Define and fill the initialization structure GPIO_InitStructure according to the control requirements, and write appropriate parameters to the corresponding structure members.

11) Call the initialization function GPIO_Init() to initialize GPIOC.

12) Write the corresponding led.h header file.

13) Write the main application.

14) Debug the program and complete.


The project I built:




/*led.h*/

#ifndef __LED_H

#define __LED_H

 

#include "stm32f10x.h"

 

#define ON  0

#define OFF 1

 

#define LED1(a) if(a) \

                 GPIO_SetBits(GPIOD,GPIO_Pin_3);\

else   \

GPIO_ResetBits(GPIOD,GPIO_Pin_3)

 

#define LED2(a) if(a) \

                 GPIO_SetBits(GPIOD,GPIO_Pin_6);\

else   \

GPIO_ResetBits(GPIOD,GPIO_Pin_6)

 

#define LED3(a) if(a) \

                 GPIO_SetBits(GPIOB,GPIO_Pin_5);\

else   \

GPIO_ResetBits(GPIOB,GPIO_Pin_5)

void LED_GPIO_Config(void);

 

#endif /*__LED_H*/


/*led.c*/

#include "led.h"

 

/*Configure the I/O port used by the LED*/

 

void LED_GPIO_Config(void)

{

/*Define a GPIO_InitTypeDef type structure*/

   GPIO_InitTypeDef GPIO_InitStructure;

/*Turn on the peripheral clock of GPIOC*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);

/*Select the GPIO pin to control*/

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_6 | GPIO_Pin_5;

/*Set pin mode to general push-pull output*/

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

/*Set the pin rate to 50MHz*/

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

/*Call library function to initialize GPIOB, GPIOD*/

GPIO_Init(GPIOB,&GPIO_InitStructure);

GPIO_Init(GPIOD,&GPIO_InitStructure);

/*Turn off all LED lights*/

GPIO_SetBits(GPIOB,GPIO_Pin_5); //Pins 3, 6, and 5 output high level and turn off the three LED lights

GPIO_SetBits(GPIOD,GPIO_Pin_3|GPIO_Pin_6);

                                                      

}


/*main.c*/

#include "stm32f10x.h"

#include "led.h"

 

void Delay(__IO u32 nCount);

 

int main (void)

{

    

SystemInit() ; //Set the system clock SYSCLK

 

LED_GPIO_Config();

   while(1)

{

      LED1(ON); //light up

 Delay(0x0FFFEF);

 LED1(OFF);

 LED2(ON);  

 Delay(0x0FFFEF);

 LED2(OFF);

 LED3(ON);  

 Delay(0x0FFFEF);

 LED3(OFF);

  }


The above code runs in the STM32 class. The following is a recommended example for comparison to find out your own shortcomings.

/*example*/

#include"stm32f10x.h"

 

#define ON           1

#define OFF          0

 

#define DELAY_TIME   0x3FFFFF

 

enum 

{

        LED1 = 0,

LED2,

LED3,

MAX_LED,

};

 

typedef struct led_gpio_s

{

int num; /* LED number*/

GPIO_TypeDef *group; /* Which group is the GPIO used by the LED: GPIOB or GPIOD */

        uint16_t pin; /* Which pin in the GPIO group is used by the LED: GPIO_Pin_x */

} led_gpio_t;

 

 

led_gpio_t        leds_gpio[MAX_LED] =

{

{LED1, GPIOB, GPIO_Pin_5}, /* GPB5 for LED1 */

{LED2, GPIOD, GPIO_Pin_6}, /* GPD6 for LED2 */

{LED3, GPIOD, GPIO_Pin_3}, /* LED3 use GPD3 */

};

 

 

void init_led_gpio(void)

{

int                i;

GPIO_InitTypeDef   GPIO_InitStructure;

 

/* Enable the clock of PB and PD group GPIO*/

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD , ENABLE);

/*Set PB5(LED1), PD6(LED2), PD3(LED3) to GPIO output push-to-disable mode, port line flip speed is 50MHz */

for(i=0; i

{

/*Set PB5 (LED1) to GPIO output push-to-disable mode, port line flip speed is 50MHz */

GPIO_InitStructure.GPIO_Pin = leds_gpio[i].pin;     

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(leds_gpio[i].group, &GPIO_InitStructure);

  }

}

 

void turn_led(int which, int cmd)

{

         if(which<0 || which> MAX_LED )

return;

 

if(OFF == cmd)

GPIO_ResetBits(leds_gpio[which].group, leds_gpio[which].pin);

else

GPIO_SetBits(leds_gpio[which].group, leds_gpio[which].pin);

}

 

void Delay(__IO uint32_t nCount)

{

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

}

 

int main(void)

{

/* Initialize system clock */

        SystemInit();

/* Initialize the GPIO pins of each LED*/

init_led_gpio();

 

  while(1)

  {

 /* Turn on LED1 and turn off LED2 and LED3*/

turn_led(LED1, ON);

 turn_led(LED2, OFF);

turn_led(LED3, OFF);

 Delay(DELAY_TIME);

 

 /* Turn on LED2 and turn off LED1 and LED3*/

turn_led(LED2, ON);

 turn_led(LED1, OFF);

turn_led(LED3, OFF);

 Delay(DELAY_TIME);

 

 /* Turn on LED3 and turn off LED1 and LED2*/

turn_led(LED3, ON);

 turn_led(LED2, OFF);

turn_led(LED1, OFF);

 Delay(DELAY_TIME);

  }

}


Keywords:STM32 Reference address:STM32 GPIO - Flowing Light Routine Learning Summary

Previous article:STM32F103C8T6-LED lighting program
Next article:STM32F407 serial port usage collection (USART1, USART2, USART3, USART6)

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

stm32 software emulation I2C
I. Overview   Many people know that there are BUGs in the hardware I2C of stm32. Now we implement I2C by software simulation timing.   Using software to simulate I2C is mainly to facilitate program transplantation. You only need to change the corresponding IO port. 2. Software Simulation Implementation 1 Start sig
[Microcontroller]
GPIO configuration of STM32F051R8T6
GPIO Input Mode void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure;   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;; GPIO_InitStructure.GPIO_Sp
[Microcontroller]
The principle and function of crystal oscillator in STM32
A crystal oscillator can be electrically equivalent to a two-terminal network consisting of a capacitor and a resistor in parallel and a capacitor in series. In electrical engineering, this network has two resonant points, with the lower frequency being the series resonance and the higher frequency being the parallel
[Microcontroller]
The principle and function of crystal oscillator in STM32
Application Comparison between STM32 and LPC1700
NXP products are widely used in cars. I have been in the automotive industry since I graduated. Freescale products are the most used in cars, while Infineon products are adopted at the car factory level, which is the so-called pre-installed standard. Except for ST's TDA7540, the biggest bugs in car radios are still
[Microcontroller]
STM32 touch screen
1. Introduction to touch screen controller 1. Overview of TSC2046 2. TSC2046 pin description 3. TSC2046 application circuit 4. Common wiring methods YU=Y- YD=Y+ XL=X- XR=X+ 2. TSC2046 Programming Notes      The PENIRQ pin of TSC2046 outputs a low level when the touch screen is pressed, and a high level wh
[Microcontroller]
STM32 touch screen
(V) STM32 engineering code HardFault exception error checking and debugging method
1. There are many reasons for exceptions, such as directly using pointers to unallocated space, stack overflow, and other illegal operations that will cause the program to enter the HardFault exception state. The following describes how to find the exceptions in the program. Next, in the keil_MDK project, compile th
[Microcontroller]
(V) STM32 engineering code HardFault exception error checking and debugging method
Comparison of M0 M3 interrupt offset in stm32 during IAP usage under keil environment
M3 interrupt offset: After adding the IAP program, the program running flow is as shown in the figure: By default, main flash memory (0x0800 0000) is selected boot space, which is mapped to 0x0000 0000. This is equivalent to starting the program from the physical address 0x0800 0000. 00 If an interrupt request occ
[Microcontroller]
Design of wireless terminal based on STM32 microcontroller and EM310
  0Introduction   In the monitoring and control and data acquisition system (SCADA) system, the real-time and accuracy of the collected data have a great impact on the system. A suitable communication network can effectively improve the benefits of the SCADA system. The application of the GPRS wireless terminal desi
[Microcontroller]
Design of wireless terminal based on STM32 microcontroller and EM310
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号