STM32 IO口输入高低电平

本文介绍STM32微控制器的GPIO配置方法,包括管脚初始化、输入输出配置及读写操作。通过实例展示了如何设置管脚为输出模式,并根据输入管脚的状态改变输出管脚的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)); //如果外部输入低电平,就亮灯
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值