C语言实现的PLC软定时器 上升沿,下降沿判断算法及功能

一、序言 使用单片机开发开发过程控制中,经常会用到定时,变量上升沿动作,下降沿动作,在业余做了个单独工具处理模块

二、C代码组成如下

共有.h文件和.c文件组成。

2.1 F00200_utils.h代码构成

 

#ifndef __F00200_UTILS_H__
#define __F00200_UTILS_H__

#include “stm32f10x.h”

#define PLS_SUM  100
#define PLF_SUM  100

//定时 触点  经过值 设定值 
#define STIMER_EMPTY  0    
#define STIMER_VALID  1

//ms 
#define STIMER_BASETIME 10  

typedef int32_t  (*StimerProcFunc)(int xArg);

typedef struct 
{
     StimerProcFunc     ProcFunc;
    
    // 传入参数  
     uint32_t     uiArgParam;      
    
    //时间间隔 
     uint32_t      uiTimerSet ;       
    
    // 时间计数  
     uint32_t    uiTimerCnt;      
    
    //定时器状态
     uint8_t       ucTimerStatus;  
    
    //超时标志 
     uint8_t     ucTimeOutFlag;     
    
     // 循环周期,-1为无限循环  
     int32_t    iCycle;     
} Stimer ;

#define MAX_TIMER 15

#endif

#define   AxStartStimer        F00200_AxStartStimer
#define   AxStimerProc         F00200_AxStimerProc 
#define   AxStimerTaskManage   F00200_AxStimerTaskManage

int32_t   AxStartStimer(uint8_t xId, StimerProcFunc  xProcFunc, uint32_t xTimeSet, 
                     uint32_t xArgParam, int32_t  xCycle);
void AxStimerProc(void);
void AxStimerTaskManage(void);

void AxCommonUtilCheck(void);

2.2 F00200_utils.c

#include “F00200_utils.h”
#include “D00201_mem_block.h” 

typedef void (*DataChgeFunc)(void);

uint8_t ucPlsValue[PLS_SUM ];
uint8_t ucPlfValue[PLF_SUM];

//上升沿
uint8_t AxPoseEdgeFuc(uint8_t xOldValue,uint8_t xId)
{
    uint8_t cPls[100];
    cPls[xId]=xOldValue&(xOldValue^ucPlsValue[xId]);
    ucPlsValue[xId]=xOldValue;
    return  cPls[xId];

}
//
uint8_t AxNegeEdgeFuc(uint8_t xOldValue,uint8_t xId)
{
    uint8_t cPlf[100];
    
    cPlf[xId]=~xOldValue&(~xOldValue^ucPlfValue[xId]);
    ucPlfValue[xId]=~xOldValue;
    return  cPlf[xId];

}

static Stimer  tStimerBuf[MAX_TIMER]; 

int32_t AxStartStimer(uint8_t xId, StimerProcFunc  xProcFunc, uint32_t xTimeSet, 
                     uint32_t xArgParam, int32_t  xCycle)
{
    if (xId >= MAX_TIMER) 
    {
        return -1;
    }
    if (STIMER_EMPTY == tStimerBuf[xId].ucTimerStatus) 
    {
        tStimerBuf[xId].ucTimerStatus = STIMER_VALID;
        tStimerBuf[xId].uiTimerCnt = 0;
        tStimerBuf[xId].ucTimeOutFlag = 0;
        tStimerBuf[xId].uiTimerSet = xTimeSet;
        tStimerBuf[xId].ProcFunc = xProcFunc;
        tStimerBuf[xId].uiArgParam = xArgParam;
        tStimerBuf[xId]. iCycle = xCycle;
        return 1;
    }
    return -1;
}

void AxStopStimer(uint8_t xId)
{
    if (xId >= MAX_TIMER) return;
    
    if (STIMER_VALID == tStimerBuf[xId].ucTimerStatus) 
    {
        tStimerBuf[xId].ucTimerStatus = STIMER_EMPTY;
        tStimerBuf[xId].uiTimerCnt = 0;
        tStimerBuf[xId]. iCycle = 0;
        tStimerBuf[xId].ucTimeOutFlag = 0;
        tStimerBuf[xId].uiTimerSet = 0;
    }
}

void AxStimerProc(void)
{
    int i;
    for (i = 0; i     {
        if (STIMER_VALID == tStimerBuf[i].ucTimerStatus) 
        {
            if (tStimerBuf[i].uiTimerCnt             {
                tStimerBuf[i].uiTimerCnt++;
            }
            else
            {
                tStimerBuf[i].ucTimeOutFlag = 1;
            }
        }
    }
}
void AxStimerTaskManage(void)
{
    uint32_t i;
    
    StimerProcFunc cFunc;
    
    for (i = 0; i     {
        if (STIMER_VALID == tStimerBuf[i].ucTimerStatus) 
        {
            if (tStimerBuf[i].ucTimeOutFlag) 
            {
                cFunc = tStimerBuf[i].ProcFunc;
                if ( cFunc != 0) 
                {
                    (*cFunc)(tStimerBuf[i].uiArgParam);
                }
                
                if (0 == tStimerBuf[i].iCycle) 
                {
                    AxStopStimer(i);
                }
                if (tStimerBuf[i].iCycle > 0)
                {
                    tStimerBuf[i].iCycle–;
                }
             }
        }
    } 
}

uint8_t ucLedSts =0;
int32_t  AxLedManage(int xArg)
{
    if(ucLedSts ==0)
    {
        tCxMemBlock.mDO.mQ[1]=0xFF;
        ucLedSts =1;
    }
    else
    {
        tCxMemBlock.mDO.mQ[1]=0x0;
        ucLedSts =0;
    }
     
    
}

uint8_t ManageSts =0;
void AxCommonUtilCheck(void)
{
   tCxMemBlock.mDO.mQ[0]=AxPoseEdgeFuc(tCxMemBlock.mDI.mI[0]&0x01,0) ;
   tCxMemBlock.mDO.mQ[0]|= AxNegeEdgeFuc( tCxMemBlock.mDI.mI[0]&0x01,1)    switch(ManageSts)
   {
       case 0:
           AxStartStimer(0, AxLedManage, 20000, 0, 12);
            
           ManageSts =1;
           break;
       case 1:
           break;
       default:
           break;
   }
   AxStimerTaskManage();
}
 

 

 

 

 

 

文章知识点与官方知识档案匹配,可进一步学习相关知识算法技能树首页概览35208 人正在系统学习中

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年1月6日
下一篇 2020年1月6日

相关推荐