×
嵌入式 > 嵌入式开发 > 详情

STM32学习笔记之GPIO

发布时间:2020-08-25 发布时间:
|
GPIO功能描述

每个GPI/O端口有两个32位配置寄存器(GPIOx_CRL,GPIOx_CRH),两个32位数据寄存器(GPIOx_IDR和GPIOx_ODR),一个32位置位/复位寄存器(GPIOx_BSRR),一个16位复位寄存器(GPIOx_BRR)和一个32位锁定寄存器(GPIOx_LCKR)。根据数据手册中列出的每个I/O端口的特定硬件特征, GPIO端口的每个位可以由软件分别配置成多种模式。 ─ 输入浮空 ─ 输入上拉 ─ 输入下拉 ─ 模拟输入 ─ 开漏输出 ─ 推挽式输出 ─ 推挽式复用功能 ─ 开漏复用功能每个I/O端口位可以自由编程,然而I/0端口寄存器必须按32位字被访问(不允许半字或字节访问)。GPIOx_BSRR和GPIOx_BRR寄存器允许对任何GPIO寄存器的读/更改的独立访问;这样,在读和更改访问之间产生IRQ时不会发生危险。


void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

#ifdef USE_STM3210C_EVAL

GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);


GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
#elif defined USE_STM3210B_EVAL

GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
#endif


GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = USARTz_RxPin;
GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = USARTz_TxPin;
GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);


RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO_Init(GPIOB, &GPIO_InitStructure);

}

#ifndef __LED_H
#define __LED_H

#include "stm32f10x.h"

#define LED1On()GPIOA->BSRR = GPIO_Pin_4
#define LED1Off()GPIOA->BRR = GPIO_Pin_4
#define LED2On()GPIOA->BSRR = GPIO_Pin_5
#define LED2Off()GPIOA->BRR = GPIO_Pin_5
#define LED3On()GPIOA->BSRR = GPIO_Pin_6
#define LED3Off()GPIOA->BRR = GPIO_Pin_6
#define LED4On()GPIOA->BSRR = GPIO_Pin_7
#define LED4Off()GPIOA->BRR = GPIO_Pin_7

#endif

PS:在使用GPIO前必须进行配置,注意复用功能,使能GPIO时钟等。



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
Recogni:将高端AI芯片推向自动驾驶边缘