03_GPIO输出

发布时间 2024-01-13 17:23:01作者: 爱吃冰激凌的黄某某

GPIO输出

简介

image-20231208104001383

GPIO基本结构

image-20231208105408994

GPIO位结构

image-20231208132902624

GPIO模式

image-20231220153606150

  1. 四种输入模式

    GPIO_Mode_IN_FLOATING 浮空输入模式
    GPIO_Mode_IPU 上拉输入模式
    GPIO_Mode_IPD 下拉输入模式
    GPIO_Mode_AIN 模拟输入模式

  2. 四种输出模式

    GPIO_Mode_Out_OD 开漏输出模式
    GPIO_Mode_Out_PP 推挽输出模式
    GPIO_Mode_AF_OD 复用开漏输出模式
    GPIO_Mode_AF_PP 复用推挽输出模式

浮空/上拉/下拉输入

image-20231220182640377

模拟输入

image-20231221122802856

开漏/推挽输出

image-20231221122926976

复用开漏/推挽输出

image-20231221123148973

LED和蜂鸣器介绍

image-20231221132559298

硬件电路

image-20231221133029076

面包板

image-20231221134311696

点亮PC13LED灯

寄存器操作方式

#include "stm32f10x.h"                  // Device header

int main(void)
{
	RCC->APB2ENR = 0x00000010;
	GPIOC->CRH = 0x00300000;
	GPIOC->ODR = 0x00000000; //灭: GPIOC->ODR = 0x00002000;
	while(1)
	{
		
	}
}

库函数操作方式

#include "stm32f10x.h"                  // Device header

int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	GPIO_SetBits(GPIOC, GPIO_Pin_13); //设置高电平
	GPIO_ResetBits(GPIOC, GPIO_Pin_13); //设置低电平
	while(1)
	{
		
	}
}

控制A0端口使LED闪烁

接线图

image-20240105153352842

代码

#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOA,&GPIO_InitStruct);
//	GPIO_ResetBits(GPIOA,GPIO_Pin_0); //端口置0,点亮
//  GPIO_SetBits(GPIOA,GPIO_Pin_0); //端口置1,熄灭
	while(1)
	{
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET); //端口置0,点亮
		Delay_ms(500);
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET); //端口置1,熄灭
		Delay_ms(500);
	}
}

LED流水灯

接线图

image-20240105153510576

代码

#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
	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(100);
		GPIO_Write(GPIOA,~0x0002); //0000 0000 0000 0010
		Delay_ms(100);
		GPIO_Write(GPIOA,~0x0004); //0000 0000 0000 0100
		Delay_ms(100);
		GPIO_Write(GPIOA,~0x0008); //0000 0000 0000 1000
		Delay_ms(100);
		GPIO_Write(GPIOA,~0x0010); //0000 0000 0001 0000
		Delay_ms(100);                                 
		GPIO_Write(GPIOA,~0x0020); //0000 0000 0010 0000
		Delay_ms(100);                                 
		GPIO_Write(GPIOA,~0x0040); //0000 0000 0100 0000
		Delay_ms(100);                                 
		GPIO_Write(GPIOA,~0x0080); //0000 0000 1000 0000
		Delay_ms(100);
	}
}

蜂鸣器

接线图

image-20240106001738877

代码

#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);
	
	while(1)
	{
		GPIO_ResetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(100);
		GPIO_SetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(100);
		GPIO_ResetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(100);
		GPIO_SetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(700);
	}
}