一、GPIO简介
![](https://img-blog.csdnimg.cn/4157878f024e4b94983df30f396e5012.png#pic_center)
单片机型号:STM32F103C8T6
通用输入输出口有:GPIOA-GPIOG
二、GPIO基本结构
![在这里插入图片描述](https://img-blog.csdnimg.cn/26cce6f3a009421aa5c87fa5d196bb48.png#pic_center)
GPIOA有十六个端口,分别为PA0--PA15,以此类推
想要使用GPIO,需要开启APB2的RCC外设时钟(RCC相当于使能),以及进行GPIO的初始化
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
三、GPIO模式
![在这里插入图片描述](https://img-blog.csdnimg.cn/7b94ce14c14644b2b6a9370c1258d410.png#pic_center)
GPIO初始化,需要端口模式、端口号、端口速度,端口模式有八种模式,四输入四输出
配置好之后就可以正常使用了
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
四、LED闪烁
int main()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
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);
while(1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
Delay_ms(500);
GPIO_SetBits(GPIOA,GPIO_Pin_0);
Delay_ms(500);
}
}
五、LED流水灯
int main()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
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);
while(1)
{
GPIO_Write(GPIOA,~0x0001);
Delay_ms(500);
GPIO_Write(GPIOA,~0x0002);
Delay_ms(500);
GPIO_Write(GPIOA,~0x0004);
Delay_ms(500);
GPIO_Write(GPIOA,~0x0008);
Delay_ms(500);
GPIO_Write(GPIOA,~0x0010);
Delay_ms(500);
GPIO_Write(GPIOA,~0x0020);
Delay_ms(500);
GPIO_Write(GPIOA,~0x0040);
Delay_ms(500);
GPIO_Write(GPIOA,~0x0080);
Delay_ms(500);
}
}
六、总结
首先要开启RCC外设时钟进行使能,使GPIO工作,再进行GPIO口的初始化配置,即可正常使用