IO.h文件
#ifndef __IO_H
#define __IO_H
#include "stm32f10x.h"
void IO_Init(void);
void SetBite_1_0(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, char a);
#endif
IO.c文件
#include "IO.h"
/*************************************************************************/
/**
* @brief IO初始化配置
* @retval None
*/
void IO_Init()
{
//定义结构体,名字自定义
GPIO_InitTypeDef GPIO_InitStruct_Out_PP; //配置管脚输出
GPIO_InitTypeDef GPIO_InitStruct_IPU; //配置管脚输入
//打开管脚时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //打开A组管脚时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //打开B组管脚时钟
//管脚配置
//管脚位
GPIO_InitStruct_Out_PP.GPIO_Pin = GPIO_Pin_8; //GPIO_Pin_0 ~ GPIO_Pin_15
//管脚模式
GPIO_InitStruct_Out_PP.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出管脚(组)配置
// = GPIO_Mode_IN_FLOATING; //浮空输入管脚(组)配置
// = GPIO_Mode_IPU; //上拉输入管脚(组)配置
// = GPIO_Mode_IPD; //下拉输入管脚(组)配置
// = GPIO_Mode_Out_OD; //开漏输出管脚(组)配置
// = GPIO_Mode_AIN; //模拟输入管脚(组)配置
// = GPIO_Mode_AF_OD; //复用开漏输出管脚(组)配置
// = GPIO_Mode_AF_PP; //复用推挽输出管脚(组)配置
//管脚速度
GPIO_InitStruct_Out_PP.GPIO_Speed = GPIO_Speed_50MHz; //高速50MHz
// = GPIO_Speed_10MHz; //中速10MHz
// = GPIO_Speed_2MHz; //低速2MHz
GPIO_InitStruct_IPU.GPIO_Pin = GPIO_Pin_1; //第1管脚
//上拉输入,这个管脚口是用来输入高低电平的
GPIO_InitStruct_IPU.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStruct_IPU.GPIO_Speed = GPIO_Speed_50MHz;
//初始化管脚
GPIO_Init(GPIOB, &GPIO_InitStruct_Out_PP);
GPIO_Init(GPIOA, &GPIO_InitStruct_IPU);
}
/*************************************************************************/
/**
* @brief 给单个管脚置位0或1
* @param 管脚组:GPIOx: where x can be (A..G) to select the GPIO peripheral.
* @param 单个管脚:GPIO_Pin_x:where x can be (0..16) to select the GPIO peripheral.
* @param 给单个管脚置位0或1
* @retval None
*/
void SetBite_1_0(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, char a)
{
if(a)
GPIO_SetBits(GPIOx, GPIO_Pin); //管脚置高电平函数
else
GPIO_ResetBits(GPIOx, GPIO_Pin); //管脚置低电平函数
}
main.c文件
#include "stm32f10x.h"
#include "IO.h"
int main()
{
//调用初始化函数
IO_Init();
while(1)
{
//GPIO_ReadInputDataBit();这个函数的作用是读取管脚电平状态的,返回1或0
SetBite_1_0(GPIOB, GPIO_Pin_8, GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1)); //如果外部输入低电平,就亮灯
}
}