GPIO简介
•GPIO(General Purpose Input Output)通用输入输出口
•可配置为8种输入输出模式
•引脚电平:0V~3.3V,部分引脚可容忍5V
•输出模式下可控制端口输出高低电平,用以驱动LED、控制蜂鸣器、模拟通信协议输出时序等
•输入模式下可读取端口的高低电平或电压,用于读取按键输入、外接模块电平信号输入、ADC电压采集、模拟通信协议接收数据等
GPIO结构
GPIO位结构
GPIO模式
•通过配置GPIO的端口配置寄存器,端口可以配置成以下8种模式
浮空/上拉/下拉 输入
并不是TTL肖特基触发器而是施密特触发器
模拟输入
施密特触发器关闭直接走最上面的一条路
开漏/推挽输出
如果P-MOS无效,为开漏输出
如果P-MOS,N-MOS都有效,为推挽输出
复用开漏/推挽输出
流水灯
1)程序
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main (void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE );
//APB2Periph外设时钟控制GPIOA使能
GPIO_InitTypeDef GPIO_InitStruct;
//定义一个结构体
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
//设置输出模式,推挽输出
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_All ;
//设置引脚
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
//设置输出速度
GPIO_Init(GPIOA,&GPIO_InitStruct);
//用结构体的参数来初始化GPI口
while(1)
{
GPIO_Write(GPIOA,~0x0001);
Delay_ms(1000);
GPIO_Write(GPIOA,~0x0002);
Delay_ms(1000);
GPIO_Write(GPIOA,~0x0004);
Delay_ms(1000);
GPIO_Write(GPIOA,~0x0008);
Delay_ms(1000);
GPIO_Write(GPIOA,~0x0010);
Delay_ms(1000);
GPIO_Write(GPIOA,~0x0020);
Delay_ms(1000);
GPIO_Write(GPIOA,~0x0040);
Delay_ms(1000);
GPIO_Write(GPIOA,~0x0080);
Delay_ms(1000);
}
}
2)Proteus仿真
提前配置
蜂鸣器
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main (void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE );
//APB2Periph外设时钟控制GPIOB使能
GPIO_InitTypeDef GPIO_InitStruct;
//定义一个结构体
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
//设置输出模式,推挽输出
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12 ;
//设置引脚
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
//设置输出速度
GPIO_Init(GPIOB,&GPIO_InitStruct);
//用结构体的参数来初始化GPI口
while(1)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
//输出低电平
Delay_ms(100);
GPIO_SetBits(GPIOB,GPIO_Pin_12);
//输出高电平
Delay_ms(100);
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
Delay_ms(100);
GPIO_SetBits(GPIOB,GPIO_Pin_12);
//GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET);
Delay_ms(700);
}
}