【tool 3】单片机状态过滤库

1.整体方案

该方案主要是用于对单片机io口或者单片机内部的状态进行一些过滤,保证程序拿到稳定的状态。

2.快速使用

(1)执行TOOL_IOFIL_ObjInit函数,进行过滤对象初始化

TOOL_IOFIL_ObjInit(&IoFilterTest,0,2,Gpio_StatusGet);

(2)定时执行TOOL_IOFIL_Poll函数

(3)执行TOOL_IOFIL_ChangeGet函数,检测被过滤对象是否有变化

(4)执行TOOL_IOFIL_CurGet函数,获取过滤后的状态

3.完整快速使用代码

IO_FILTER_T IoFilterTest;

/*
*********************************************************************************************************
*    函 数 名: Gpio_Init
*    功能说明: gpio初始化
*    形    参: 无
*    返 回 值: 无
*********************************************************************************************************
*/
void Gpio_Init(void)
{
    /*定义一个GPIO_InitTypeDef类型的结构体*/
     GPIO_InitTypeDef  GPIO_InitStruct;

    __GPIOG_CLK_ENABLE(); /*开启gpio 时钟*/

    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;             /* 设置输入 */
    GPIO_InitStruct.Pull = GPIO_NOPULL;                 /* 上下拉电阻不使能 */
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;    /* GPIO速度等级 */

    /*选择要控制的GPIO引脚*/                                                                
    GPIO_InitStruct.Pin = GPIO_PIN_0; 
    HAL_GPIO_Init(GPIOG, &GPIO_InitStruct); 
}
/*
*********************************************************************************************************
*    函 数 名: Gpio_StatusGet
*    功能说明: gpio 状态获取
*    形    参: 无
*    返 回 值: 无
*********************************************************************************************************
*/
uint8_t Gpio_StatusGet(void)
{
    return HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_0);
}

/*
*********************************************************************************************************
*    函 数 名: main
*    功能说明: c程序入口
*    形    参: 无
*    返 回 值: 错误代码(无需处理)
*********************************************************************************************************
*/
int main(void)
{    
    bsp_Init();        /* 硬件初始化 */

    bsp_InitTimer();
    /*启用一个软件定时器,定时周期为10ms*/
    bsp_StartAutoTimer(0,10);
    /*gpio 初始化*/
    Gpio_Init();
    /*初始化状态过滤*/
    TOOL_IOFIL_ObjInit(&IoFilterTest,0,2,Gpio_StatusGet);
    /* 进入主程序循环体 */
    while (1)
    {    
        /*检测定时时间是否到达*/
        if(bsp_CheckTimer(0))
        {
            TOOL_IOFIL_Poll();
        }
        /*判断状态是否发生了改变*/
        if(TOOL_IOFIL_ChangeGet(&IoFilterTest))
        {
            printf("状态发生了改变,当前状态为 %d",TOOL_IOFIL_CurGet(&IoFilterTest));
        }
    }
}

4.各个接口说明及代码分析

(1)TOOL_IOFIL_ObjInit

被过滤对象初始化

(2) TOOL_IOFIL_Poll

过滤对象定时轮询

(3)TOOL_IOFIL_ChangeGet

检测被过滤对象是否改变

(4)TOOL_IOFIL_CurGet

被过滤状态获取

5、完整文件

.c文件

/*
*********************************************************************************************************
*
*    模块名称 : 状态过滤模块
*    文件名称 : tool_io_filter.c
*    版    本 : V1.1
*    说    明 : 状态过滤工具
*
*    修改记录 :
*        版本号  日  期        作者     说明
*        V1.0    2024-5-20 StrongerSun  正式发布
*
*    Copyright (C), 2015-2020
*
*********************************************************************************************************
*/

/* Includes ---------------------------------------------------------------------------------------------*/
#include "bsp.h"
#include "tool_io_filter.h"

/* Private macro ----------------------------------------------------------------------------------------*/
#define FILTER_GROUP_MAX 32
/* Private enum -----------------------------------------------------------------------------------------*/
/* Private typedef --------------------------------------------------------------------------------------*/
/* Exported variables -----------------------------------------------------------------------------------*/


/* Private variables ------------------------------------------------------------------------------------*/
static IO_FILTER_T* s_IoFilterStrGroup[FILTER_GROUP_MAX] = {0}; /*Storage structure pointer*/
static uint8_t s_ucIoFilterStrCount = 0; /*Structure pointer count value*/

/* Private function declaration -------------------------------------------------------------------------*/
static void IOFIL_Calc(IO_FILTER_T* _io_filter,uint8_t _io_input);

/* Exported function declaration ------------------------------------------------------------------------*/
/* Exported functions prototypes-------------------------------------------------------------------------*/

 /**
 ***********************************************************************************************************                  
 * @name           BSP_IOFIL_ObjInit
 * @brief          过滤目标初始化
 * @parameter     _io_filter:需要被过滤的目标
 * @parameter    _init_io_value:初始化值
 * @parameter     _filter_count:过滤次数
 * @parameter    _status_get:状态获取
 * @return         null                       
 ***********************************************************************************************************
 **/
void TOOL_IOFIL_ObjInit(IO_FILTER_T* _io_filter,uint8_t _init_io_value,
            uint8_t _filter_count,uint8_t (*_status_get)(void))
{
    if((s_ucIoFilterStrCount < FILTER_GROUP_MAX) &&( _status_get != NULL)) /*Enter an error if the number of registrations exceeds the limit*/
    {
        _io_filter->Count = 0;
        _io_filter->CurIoValue = _init_io_value;
        _io_filter->FilterCount = _filter_count;
        _io_filter->LastIoValue = _init_io_value;
        _io_filter->StatusGet = _status_get;
        if(s_ucIoFilterStrCount < FILTER_GROUP_MAX)
        {
            s_IoFilterStrGroup[s_ucIoFilterStrCount++] = _io_filter;
        }
        else
        {    
            while(1);
        }
    }
    else
    {
        /*Error handing*/
    }
}
 /**
 ***********************************************************************************************************                  
 * @name           BSP_IOFIL_ChangeGet
 * @brief          检测状态是否发生改变
 * @parameter     _io_filter:过滤对象
 * @return         状态是否发生改变
                 false:状态未发生改变
                 true:状态发生了改变
 ***********************************************************************************************************
 **/
bool TOOL_IOFIL_ChangeGet(IO_FILTER_T* _io_filter)
{
    if(_io_filter->ChangeFlag == TRUE)
    {
        _io_filter->ChangeFlag = FALSE;
        return TRUE;
    }
    return FALSE;
}
 /**
 ***********************************************************************************************************                  
 * @name           BSP_IOFIL_CurGet
 * @brief          获取当前状态
 * @parameter     _io_filter:过滤目标
 * @return         当前状态值                       
 ***********************************************************************************************************
 **/
int TOOL_IOFIL_CurGet(IO_FILTER_T* _io_filter)
{
    return _io_filter->CurIoValue;
}
/**
***********************************************************************************************************                  
* @name           TOOL_IOFIL_Poll
* @brief         过滤轮询
* @param        null
* @return       null                      
***********************************************************************************************************
**/
void TOOL_IOFIL_Poll(void)
{
    for(uint8_t i=0;i<s_ucIoFilterStrCount;i++)
    {
        if(s_IoFilterStrGroup[i]->StatusGet != NULL)
        {
            IOFIL_Calc(s_IoFilterStrGroup[i],s_IoFilterStrGroup[i]->StatusGet());
        }
    }

}

 /* Private function prototypes --------------------------------------------------------------------------*/
 /**
 ***********************************************************************************************************                  
 * @name           IOFIL_Calc
 * @brief          过滤计算
 * @parameter     _io_filter:过滤目标
 * @parameter     _io_input:输入状态
 * @return         null                       
 ***********************************************************************************************************
 **/
static void IOFIL_Calc(IO_FILTER_T* _io_filter,uint8_t _io_input)
{
    if(_io_input != _io_filter->CurIoValue) /*If the value is inconsistent with the current value, the filter is entered*/
    {
        if(_io_input == _io_filter->LastIoValue)
        {
            if(++_io_filter->Count >= _io_filter->FilterCount)
            {
                _io_filter->Count = 0;
                _io_filter->CurIoValue = _io_input;
                _io_filter->ChangeFlag = TRUE;
            }
        }
        else
        {
            _io_filter->Count = 0;
        }
    }
    else
    {
        /* Prevent intermittent pulses from reaching the threshold */
        _io_filter->Count = 0;
    }
    _io_filter->LastIoValue = _io_input;
}

/***************************** (END OF FILE) *********************************/

.h 文件

/*
*********************************************************************************************************
*
*    模块名称 : 运行时间记录模块
*    文件名称 : tool_io_filter.h
*    版    本 : V1.0
*    说    明 : 头文件
*
*    Copyright (C), 2012-2013
*
*********************************************************************************************************
*/

/* Define to prevent recursive inclusion ----------------------------------------------------------------*/
#ifndef __TOOL_IO_FILTER_H
#define __TOOL_IO_FILTER_H

/* Includes ---------------------------------------------------------------------------------------------*/
#include "bsp.h"
#include "stdbool.h"

/* Exported macros --------------------------------------------------------------------------------------*/
/* Exported enum ----------------------------------------------------------------------------------------*/
/* Exported types ---------------------------------------------------------------------------------------*/
typedef struct 
{
    uint8_t Count;                   /*Count value */
    uint8_t LastIoValue;            /* Last status*/
    uint8_t FilterCount;            /*Filter value*/
    uint8_t CurIoValue;                /*Current status value */
    bool ChangeFlag;            /*Change the flag*/
    uint8_t (*StatusGet)(void); /*Function registration that gets the status value*/
}IO_FILTER_T;

/* Exported variables -----------------------------------------------------------------------------------*/
/* Exported functions -----------------------------------------------------------------------------------*/
void TOOL_IOFIL_ObjInit(IO_FILTER_T* _io_filter,uint8_t _init_io_value,
                uint8_t _filter_count,uint8_t (*_status_get)(void));
bool TOOL_IOFIL_ChangeGet(IO_FILTER_T* _io_filter);
int TOOL_IOFIL_CurGet(IO_FILTER_T* _io_filter);
void TOOL_IOFIL_Poll(void);


#endif /* __TOOL_IO_FILTER_H */

/***************************** (END OF FILE) *********************************/

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值