一、简介
在PCBA上有一些过大电流的地方,如果有I/O电平操作,这时就容易受干扰,可能会偶发出现一个毛刺,影响功能,为了设备稳定运行,通常会采用I/O口滤波处理,把偶发的毛刺给过滤掉。
二、I/O电平滤波算法
//数据类型定义
typedef enum IoLevel_e
{
PS_IO_LEVEL_HIGH,
PS_IO_LEVEL_LOW,
PS_IO_LEVEL_NUM
}g_emIoLevel;
g_emIoLevel g_emLedLevel = PS_IO_LEVEL_NUM;
//I/O电平滤波处理
g_emIoLevel PS_GetIoElecLevel(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, uint16_t u16FiltNum)
{
static g_emIoLevel semRetVal = PS_IO_LEVEL_NUM;
static uint16_t s_u16HighLevelCnt = 0;
static uint16_t s_u16LowLevelCnt = 0;
if(HAL_GPIO_ReadPin(GPIOx,GPIO_Pin) == GPIO_PIN_SET)
{
s_u16HighLevelCnt++;
if(s_u16HighLevelCnt > u16FiltNum)
{
s_u16HighLevelCnt = 0;
semRetVal = PS_IO_LEVEL_HIGH;
}
s_u16LowLevelCnt = 0;
}
else
{
s_u16LowLevelCnt++;
if(s_u16LowLevelCnt > u16FiltNum)
{
semRetVal = PS_IO_LEVEL_LOW;
}
s_u16HighLevelCnt = 0;
}
return semRetVal;
}
/***********************函数调用****************************/
g_emLedLevel = PS_GetIoElecLevel(GPIOA, GPIO_PIN_0, 500);
if(g_emLedLevel == PS_IO_LEVEL_HIGH)
{
HAL_GPIO_WritePin(LED4_GPIO_PORT, LED4_PIN, GPIO_PIN_SET);
}
else if(g_emLedLevel == PS_IO_LEVEL_LOW)
{
HAL_GPIO_WritePin(LED4_GPIO_PORT, LED4_PIN, GPIO_PIN_RESET);
}
|