思路:1.时钟使能(开启GPIOA端口时钟使能) 2.GPIO配置(推挽输出模式,频率,速度等) 3.GPIO输出高电平(输出置位)
STM32F4:
推挽输出,输出低电平灯亮,输出高电平灯灭.
#include <stm32f4xx.h>
void Delay(__IO uint32_t nCount){
while(nCount--);
}
void GPIO_Con(){
GPIO_InitTypeDef GPIO_Struct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
GPIO_Struct.GPIO_Pin=GPIO_Pin_9;
GPIO_Struct.GPIO_Mode=GPIO_Mode_OUT;
GPIO_Struct.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Struct.GPIO_OType=GPIO_OType_PP;
GPIO_Struct.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOF,&GPIO_Struct);
}
int main(void){
GPIO_Con();
while(1){
GPIO_SetBits(GPIOF,GPIO_Pin_9);
Delay(1000);
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
Delay(1000);
}
}
STMF7:
更多的使用HAL库函数。
#include <stdio.h>
#include <stm32f7xx_hal.h>
#include <stm32f7xx_hal_cortex.h>
static int timecounts;
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOI_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOI, GPIO_PIN_1, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
}
int main(){
MX_GPIO_Init();
while(1){
HAL_GPIO_WritePin(GPIOI, GPIO_PIN_1, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOI, GPIO_PIN_1, GPIO_PIN_RESET);
HAL_Delay(500);
}
}
GPIO函数库:
1个初始化函数:void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
2个读取输入电平函数:uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
2个读取输出电平函数:uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
4个设置输出电平函数:void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
针对管脚和针对端口的区别