GPIO input-external power detection

foreword

(1) This series is based on STM32 project notes, covering the use of various STM32 peripherals, from shallow to deep.

(2) The MCU used by the editor is STM32F105RCT6. The project notes are based on the actual project of the editor, but the content in the blog is suitable for the students who develop various MCUs to learn and use.

learning target

  1. Analysis of the principle of external power detection circuit.
  2. The development of external power detection program code, master the configuration of GPIO port input mode.
  3. Experimental results: LED1 is on when the external power is connected, and LED1 is off when the external power is disconnected.

Schematic diagram of external power detection hardware

Principle analysis

The state of the external power is detected through the PB1 port. Then analyze the state of PB1 without external power input and with external power input.

(1) External power disconnected

PB1 is grounded through the pull-down resistor R55, and PB1 inputs low level.

(2) External power input port connected to 5V power supply

R55/(R54+R55) = VR55(point A) / 5V (VR54+R55)

The voltage value of PB1 is: VR55 =5*200 /(100+200) = 3.33V, high level.

Program implementation – external power status detection

Initialization of external power detection GPIO

Port definition: The port for external power detection is PB1
// AC Check Pin
#define CHECK_ACSTATE_PORT       GPIOB
#define CHECK_ACSTATE_PIN           GPIO_Pin_1

Initialization of PB1 Initialization of GPIO:

void hal_GpioConfig_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE); 	
	
	GPIO_InitStructure.GPIO_Pin = CHECK_ACSTATE_PIN;     
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   //浮空输入
	GPIO_Init(CHECK_ACSTATE_PORT, &GPIO_InitStructure);	
	
        AcState = (en_AcLinkSta)hal_GPIO_GetACState();  /// 
} 

Selection of pull-up, pull-down, and floating modes:

Select floating input mode

typedef enum
{ GPIO_Mode_AIN = 0x0,//模拟输入
  GPIO_Mode_IN_FLOATING = 0x04,//浮空输入
  GPIO_Mode_IPD = 0x28,//上拉输入
  GPIO_Mode_IPU = 0x48,//下拉输入
  GPIO_Mode_Out_OD = 0x14,//通用开漏输出
  GPIO_Mode_Out_PP = 0x10,//通用推挽输出
  GPIO_Mode_AF_OD = 0x1C,//复用开漏输出
  GPIO_Mode_AF_PP = 0x18//复用推挽输出
}GPIOMode_TypeDef;

If the GPIO mode selection: pull-up input, PB1 is also high level when the external power is off.

When the GPIO mode selects pull-up input, Rdo does not exist. At this time, there are only three other resistors in the circuit. Regardless of whether the external power is connected or disconnected, PB1 is pulled high by the pull-up resistor and is at a high level.

If the GPIO mode is selected: pull-down input, the level of PB1 will become smaller when the external power is connected.

When the GPIO mode selects pull-down input, Rup does not exist, and there are only three other resistors in the circuit at this time. After R55 and RDO are connected in parallel, the overall resistance becomes smaller, and R54 remains unchanged. According to the voltage division of the series circuit, the voltage of PB1 will become smaller.

Commonly used GPIO input library functions

GPIO common input functions
//读取单个输入端口的数据
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

//读取单个输入端口的数据
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);

//读取单个输入端口的数据
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

//读取单个输入端口的数据
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);

Define external power status enumeration type variable

typedef enum
{
	 STA_AC_BREAK = 0,
	  STA_AC_LINK,
}en_AcLinkSta;

External power detection function

// 获取PB1端口状态(高低电平)
static unsigned char hal_GPIO_GetACState(void)
{
	return (GPIO_ReadInputDataBit(CHECK_ACSTATE_PORT,  CHECK_ACSTATE_PIN ));		
}
void delay_1msTest(void)
{
	unsigned int i=0;
	i = 72000;
	while(i--);   
}

外电检测函数任务
返回值   外电的状态
en_AcLinkSta hal_Gpio_AcStateCheck(void)
{
	en_AcLinkSta state;
	static unsigned char times = 0;	//静态延时计数
	state = (en_AcLinkSta)hal_GPIO_GetACState();//记录第一次检测到的外电状态
	delay_1msTest();
	if(state == AcState)
	{如果本次获取的状态和上次一样,则清零延时计数
		times	= 0;
	}
	else if(state != AcState)
  {///如果状态有变化
		times	++; //计数增加
		if(times > 20)
		{如果不一样的状态计数超过20次  则更新 AcState 状态。
			times = 0;
			AcState = state;
		}
	}
	return AcState;///
}
///

hal_GPIO.h code

#ifndef ____HAL_GPIO_H_
#define ____HAL_GPIO_H_
 
// AC Check Pin
#define CHECK_ACSTATE_PORT       GPIOB
#define CHECK_ACSTATE_PIN        GPIO_Pin_1

typedef enum
{
		STA_AC_BREAK = 0,
	  STA_AC_LINK,
}en_AcLinkSta;

void hal_GpioConfig_init(void);
en_AcLinkSta hal_Gpio_AcStateCheck(void);

#endif
//


hal_GPIO.c code

#include "stm32F10x.h"
#include "hal_GPIO.H"

static unsigned char hal_GPIO_GetACState(void);

static en_AcLinkSta AcState;// 外电的状态静态全局变量

/GPIO 初始化函数
void hal_GpioConfig_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE); 	
	
	GPIO_InitStructure.GPIO_Pin = CHECK_ACSTATE_PIN;     
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   //浮空输入
	GPIO_Init(CHECK_ACSTATE_PORT, &GPIO_InitStructure);	
	
  AcState = (en_AcLinkSta)hal_GPIO_GetACState();  ///上电获取外电的状态   
}

// 获取PB1端口状态(高低电平)
static unsigned char hal_GPIO_GetACState(void)
{
	return (GPIO_ReadInputDataBit(CHECK_ACSTATE_PORT, GPIO_Pin_1));		
}
void delay_1msTest(void)
{
	unsigned int i=0;
	i = 7200;
	while(i--);   
}


en_AcLinkSta hal_Gpio_AcStateCheck(void)
{
	en_AcLinkSta state;
	static unsigned char times = 0;	//静态延时计数
	
	state = (en_AcLinkSta)hal_GPIO_GetACState();
	
	delay_1msTest();
	if(state == AcState)
	{如果本次获取的状态和上次一样,则清零延时计数
		times	= 0;
	}
	else if(state != AcState)
  {///如果状态有变化
		times	++; //计数增加
		if(times > 20)
		{如果不—样的状态计数超过20次 则更新 Acstate 状态。
			times = 0;
			AcState = state;
		}
	}
	return AcState;///
}
///

Modify the main function

#include "stm32f10x.h"
#include "hal_timer.h"
#include "hal_led.h"
#include "hal_gpio.h"

int main(void)
{
	hal_LedInit();
	hal_GpioConfig_init();
//	hal_timerInit();
  while (1)
  {		
		if(hal_Gpio_AcStateCheck() == STA_AC_BREAK)
		{
			GPIO_ResetBits(LED7_PORT,LED7_PIN);
		}
		else
		{	
			GPIO_SetBits(LED7_PORT,LED7_PIN);			
		}
  }
}

Guess you like

Origin blog.csdn.net/weixin_62261692/article/details/132499152