STM32_GPIO configuration and library function explanation - LED marquee

Publisher:Joyful888LifeLatest update time:2016-07-30 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
gpio general-purpose input/output general-purpose input/output port

List of GPIO register abbreviations

March 1, 2013 - wj86843248 - Don't touch me

 

Each bit of the GPIO port can be configured into multiple modes by software.

 

STM32_GPIO configuration and library function explanation - LED Marquee - wj86843248 - Don't touch me

 

 

 

 

During and immediately after reset, the multiplexing function is not enabled and the I/O ports are configured in floating input mode.

 

 

The LED hardware connection is shown in the figure below: A high level lights up the LED.

 

March 1, 2013 - wj86843248 - Don't touch me

 

 

 

To successfully light up an LED, the program requires the following steps: (required)

Step 1: Configure the system clock. See STM32F103x RCC register configuration

 

In addition, the GPIO peripheral clock needs to be turned on.

 


/* Enable GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

Step 2: Configure the interrupt vector table. Decide whether to download the program to RAM or FLASH. I will talk about this later.

 

 


void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM //VECT_TAB_RAM is not defined in the program, so download the program to Flash
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}

Step 3: Configure the GPIO mode. Input mode or output mode. The focus of this chapter.


void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/* Configure PC.06, PC.07, PC.08 and PC.09 as Output push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Spe ed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}

In fact, using GPIO is very simple. You only need to fill in the member variables of the following structure:


typedef struct
{
u16 GPIO_Pin; //Which pin
GPIOSpeed_TypeDef GPIO_Speed; //If it is output mode, you also need to set the speed
GPIOMode_TypeDef GPIO_Mode; //Pin type
}GPIO_InitTypeDef;

hint


void GPIO_SetBits(GPIO_TypeDef* GPIOx, u16 GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));

GPIOx->BSRR = GPIO_Pin;
}

The GPIO_BSRR register is involved, as shown belowMarch 1, 2013 - wj86843248 - Don't touch me

 

 

March 1, 2013 - wj86843248 - Don't touch me
 
This involves GPIOx_ODR, as shown belowMarch 1, 2013 - wj86843248 - Don't touch me
March 1, 2013 - wj86843248 - Don't touch me
v GPIO_ResetBits writes 0 to the specified Pin of the specified Port:

void GPIO_ResetBits(GPIO_TypeDef* GPIOx, u16 GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));

GPIOx->BRR = GPIO_Pin;
}

The GPIO_BRR register is involved, as shown below
March 1, 2013 - wj86843248 - Don't touch me
March 1, 2013 - wj86843248 - Don't touch me
 
After the above 4 steps, you can successfully drive the LED.
 
The LED marquee program is given below:

/* Includes ----------------------------------------------- ------------------*/
#include "stm32f10x_lib.h"

/* Private function prototypes ------------------ ----------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void Delay(vu32 nCount);

/****************************************** *************************************
* Function Name: main
* Description: Main program.
* Input : None
* Return : None
********************************************** *************************************/
int main(void)
{
#ifdef DEBUG
debug();
# endif

/* Configure the system clocks */
RCC_Configuration();

/* NVIC Configuration */
NVIC_Configuration();

/* Configure the GPIO ports */
GPIO_Configuration();

/* Infinite loop */
while (1)
{
GPIO_SetBits(GPIOC,GPIO_Pin_6);//Turn on LED1
Delay(1000000);
Delay(1000000);//Turn on for a while so that people can see the exact change of the LED
GPIO_ResetBits(GPIOC,GPIO_Pin_6);//Turn off LED1

GPIO_SetBits(GPIOC,GPIO_Pin_7);//Turn on LED2
Delay(1000000);
Delay(1000000);
GPIO_ResetBits(GPIOC,GPIO_Pin_7);//Turn off LED2

GPIO_SetBits(GPIOC,GPIO_Pin_8);//Turn on LED3
Delay(1000000);
Delay(1000000);
GPIO_ResetBits(GPIOC,GPIO_Pin_8); //Turn off LED3

GPIO_SetBits(GPIOC,GPIO_Pin_9);//Light up LED4
Delay(1000000);
Delay(1000000);
GPIO_ResetBits(GPIOC,GPIO_Pin_9);//Turn off LED4
}
}

/************** *************************************************** ***************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Return : None
************* *************************************************** ****************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;

/* RCC system reset(for debug purpose) */
RCC_DeInit();

/* Enable HSE */
RCC_HSEConfig( RCC_HSE_ON);

/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();

if (HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);

/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_ Div1);

/ * PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);

/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);

/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

/* Enable PLL */
RCC_PLLCmd(ENABLE);

/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {}

/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08) {}
}

/* Enable GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
}

/********************************************** ******************************************
* Function Name: NVIC_Configuration
* Description: Configures Vector Table base location.
* Input : None
* Return : None
**************************************** ***************************************/
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/ * Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}

/****************************************************** ****************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Return : None
************************************************ *******************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/* Configure PC.06, PC. 07, PC.08 and PC.09 as Output push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}

/********************************************** ******************************************
* Function Name: Delay
* Description: Inserts a delay time .
* Input : nCount: specifies the delay time length.
* Return : None
********************************** *********************************************/
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}

#ifdef DEBUG
/****************************** ***************************************************
* Function Name: assert_failed
* Description: Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input: - file: pointer to the source file name
* - line: assert_param error line source number
* Return: None
** *************************************************** ***************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

/* Infinite loop */
while (1)
{
}
}
#endif

 

How to debug: Set a breakpoint at while (1).


After executing the GPIO_Configuration function, observe the GPIO_CRL and GPIO_CRH registers and you can see:

March 1, 2013 - wj86843248 - Don't touch me

The mode configuration of each pin is determined by 4 bits in GPIO_CRL or GPIO_CRH. For example, the PC6 pin is determined by the MODE6[1:0] and CNF6[1:0] bits in GPIO_CRL, and the others are determined by analogy.

The GPIO_CRL register is involved, as shown below

March 1, 2013 - wj86843248 - Don't touch me
March 1, 2013 - wj86843248 - Don't touch me
 
 
Because MODE6[1:0]=11, looking at the above table, it can be concluded that PC6 is in output mode and the maximum speed is 50MHZ. Since CNF6[1:0]=00 and in output mode, the output driver is used in general push-pull output mode.

 

After executing GPIO_SetBits(GPIOC,GPIO_Pin_6); //Turn on LED1, you can see: GPIO_ODR's ODR6=1

After executing GPIO_ResetBits(GPIOC,GPIO_Pin_6); //Turn off LED1, you can see: GPIO_ODR's ODR6=0

The same goes for other pins.


Keywords:STM32 Reference address:STM32_GPIO configuration and library function explanation - LED marquee

Previous article:stm32_exti (including NVIC) configuration and library function explanation
Next article:Further discussion on IAR 4_42A project configuration

Recommended ReadingLatest update time:2024-11-16 16:37

Use of stm32 watchdog
Why use a watchdog The matter is very simple. A data collection product I made before was abnormal for some reason, fell into an infinite loop and then "frozen". After analyzing it many times, I couldn't find the reason, but every time I restarted it, I could collect data normally. Later I found a solution: watchdog
[Microcontroller]
Use of stm32 watchdog
STM32 interrupt data callback function reception processing
1. Define an empty pointer function. The parameter of the pointer function is uint8_t type ch typedef void (* usart_recv_callback)(uint8_t ch); 2. Declare this type usart_recv_callback  usart1_recv_cb; 3. When configuring the serial port, one parameter is the serial port interrupt receive callback void Usa
[Microcontroller]
STM32 controls the LCD driver driven by ILI9341 (continued)
I encountered this problem while doing the project. I think the article is well written. I would like to share it with my colleagues who have doubts about the use of FSMC! LCD has the following control lines: CS: Chip Select, low level valid RS: Register Select WR: Write signal, low level valid RD: Read signal, low l
[Microcontroller]
Analysis of the circuit principle of serial one-key download of Zhengdian Atom STM32 Elite Development Board
Use the DTR pin and RTS pin in the serial port chip CH340 to control the level state of the microcontroller reset pin and BOOT0 pin, so as to achieve one-click download. For this one-click download circuit, the key points are that it is enough to understand these two points: 1. You must have the ability to understan
[Microcontroller]
Analysis of the circuit principle of serial one-key download of Zhengdian Atom STM32 Elite Development Board
Research on stm32 DMA initialization options
DMA is easy to use and simple. Today, when I was doing "continuous" and "scanning" sampling of multi-channel ADC, I had a deeper understanding of DMA. Today I will share with you: #define ADC1_DR_Address ((uint32_t)0x4001244C)unsigned short Buff ; ......DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBase
[Microcontroller]
STM32 SRAM and FLASH--Overview
1 STM32 has built-in SRAM and FLASH. FLASH is used to store programs, and SRAM is used to store intermediate variables during program operation. Usually, the sizes of SRAM and FLASH of different models of STM32 are different. For the STM32F103VET6 at hand, according to the data sheet,   FLASH memory is also called f
[Microcontroller]
STM32 SRAM and FLASH--Overview
STM32 study notes - a preliminary understanding of external interrupts
The STM32F103 has 76 interrupts, including 16 core interrupts and 60 maskable interrupts, with 16 levels of programmable interrupt priority. To understand the interrupts of STM32 , we must start with the interrupt priority grouping. To understand the interrupt priority grouping, we must understand what is preemption p
[Microcontroller]
STM32 study notes - a preliminary understanding of external interrupts
STM32 serial port function_library function USART_SendData problem and solution
Personal Records: The reason why I didn't succeed in the serial port experiment yesterday was that the previous one was always overwritten by the next one when calling USART_SendData continuously. I used to think that there should be no problem with ST's official library, so I didn't think about it in this regard. N
[Microcontroller]
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号