STM32 IO端口工作模式
GPIO_Mode_AIN 模拟输入
GPIO_Mode_IN_FLOATING 输入浮空
GPIO_Mode_IPD 输入下拉
GPIO_Mode_IPU 输入上拉
GPIO_Mode_Out_OD 开漏输出
GPIO_Mode_Out_PP 推挽输出
GPIO_Mode_AF_OD 开漏复用功能
GPIO_Mode_AF_PP 推挽式复用功能
推挽输出高低电平均有驱动能力
开漏输出高电平相当于高阻态,没有驱动能力,低电平有驱动能力
案例
LED闪烁代码
#include "stm32f10x.h"
#include "delay.h"
int main(void)
{
delay_init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
while(1)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
delay_ms(500);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
delay_ms(500);
}
}
LED流水灯代码
#include "stm32f10x.h"
#include "delay.h"
int main(void)
{
delay_init();
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); //~0000 0000 0000 0001
delay_ms(500);
GPIO_Write(GPIOA,~0x0002);//~0000 0000 0000 0010
delay_ms(500);
GPIO_Write(GPIOA,~0x0004);//~0000 0000 0000 0100
delay_ms(500);
GPIO_Write(GPIOA,~0x0008);//~0000 0000 0000 1000
delay_ms(500);
GPIO_Write(GPIOA,~0x0010);//~0000 0000 0001 0000
delay_ms(500);
GPIO_Write(GPIOA,~0x0020);//~0000 0000 0010 0001
delay_ms(500);
GPIO_Write(GPIOA,~0x0040);//~0000 0000 0100 0001
delay_ms(500);
GPIO_Write(GPIOA,~0x0080);//~0000 0000 1000 0001
delay_ms(500);
}
}
蜂鸣器
#include "stm32f10x.h"
#include "delay.h"
int main(void)
{
delay_init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
while(1)
{
GPIO_SetBits(GPIOB,GPIO_Pin_8);
delay_ms(500);
GPIO_ResetBits(GPIOB,GPIO_Pin_8);
delay_ms(500);
}
}
啊