业务实现比较简单,具体看注释。
#include "pluseCheck.h"
#include "gpio.h"
unsigned long totalCNt; //总的脉冲计数器
unsigned long pluseCNt; //占空比大约为50%的脉冲计数器
/**
*
* 根据脉冲的周期设置调用周期
* 最好是放在定时器中断中
*/
void checkPlusePin(void)
{
static char pinSta=0;
static char isdutyCycleOK=0;
static unsigned long widthTmCnt=0; //高电平时间计数器
static unsigned long negWidthTmCnt=0; //低电平时间计数器
static unsigned long LEDflashTmCnt=0; //LED闪烁时间计数器
static unsigned long LEDflashTm=0; //LED闪烁时间
float dutyCycle;
if(cmt_spi3_gpio3_read()){
if(pinSta == 0){
widthTmCnt = 0;
}
if(widthTmCnt < 0xffffffff)
widthTmCnt++;
pinSta = 1;
}else{
if(pinSta == 1){
dutyCycle = (float)widthTmCnt/(negWidthTmCnt + widthTmCnt);
if( (dutyCycle > 0.45) && (dutyCycle < 0.55) ){
pluseCNt ++;
}else{
}
totalCNt ++;
if(totalCNt == 1000){
if(pluseCNt > 700){
isdutyCycleOK = 1;
LEDflashTm = totalCNt - pluseCNt;
}else{
isdutyCycleOK = 0;
}
pluseCNt = 0;
totalCNt = 0;
}
negWidthTmCnt = 0;
}
if(negWidthTmCnt < 0xffffffff)
negWidthTmCnt ++;
pinSta = 0;
}
//根据丢包率控制LED的开关
if(isdutyCycleOK){
LEDflashTmCnt++;
//根据丢包率的情况控制闪烁的时间
if(LEDflashTmCnt >= LEDflashTm*5){
LEDflashTmCnt = 0;
LED1_Toggle();
}
}else{
LEDflashTmCnt = 0;
LED1_OFF();
}
}