C语言那些事之脉冲丢包率检测

业务实现比较简单,具体看注释。

#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();
	}
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值