初始化GPIO的常用格式:
1.引用结构体
2.使能GPIO
3.设置引脚的引脚、模式、速度
4.初始化指定的GPIO
5.推挽输出要设置GPIO的高低电平
GPIO_InitTypeDef GPIO_InitStructure;//引用两个结构体
//GPIO是挂载在APB2总线的外设,使用使用RCC_APB2PeriphClockCmd()来实现。
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|
RCC_APB2Periph_GPIOD, ENABLE); //使能 PA,PD 端口时钟
//设置引脚、模式、速度
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_2; //PA.8、PD.2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度 50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure);//根据设定参数配置 GPIO
GPIO_SetBits(GPIOA, GPIO_Pin_8);//输出1
//GPIO_ResetBits (GPIOA, GPIO_Pin_8);//输出0
GPIO_Init(GPIOD, &GPIO_InitStructure);//根据设定参数配置 GPIO
GPIO_SetBits(GPIOD, GPIO_Pin_8);//输出1
相关文章









