STM32 高级定时TIM 死区时间计算--C语言实现

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

int DTG[8]={0,0,0,0,0,0,0,0};
///是十进制转二进制
int dectobin(int n)
{
    int sum = 0;
    int y, x = 1; // y表示余数,x为叠加的系数
    while (n != 0)
    {
        y = n % 2;
        sum += x * y;
        x *= 10;
        n /= 2;
    }
	return sum;
}
int main()
{
	double TDTS;
	double Tdtg1,Tdtg2,Tdtg3,Tdtg4;
	char str[100];
	double time;
	unsigned int frequency;
	bzero(str,sizeof(str));
	printf("1s = 1000ms \t 1ms = 1000us \t 1us = 1000ns\n");
	printf("输入定时器频率(MHZ)\n");
	scanf("%d",&frequency);
	TDTS = (1*1000.0*1000.0*1000.0)/(frequency*1000000.0);
	printf("TDTS: %0.2lf  ns\n",TDTS);
    sprintf(str,"%0.2lf",TDTS);    
	Tdtg1 = atof(str);
	printf("Tdtg1:%0.2lf\n",Tdtg1);
	
	printf("----------------第一种-------------\n");
	double DT1;///持续时间
	printf("tTdtg: %lf  ns\n",Tdtg1);
	printf("DTG[7:5]= 0xx => DT=DTG[6:0] × Tdtg1,Tdtg1 = TDTS\n");
	printf("Tdtg1: %0.2lf  ns\n",Tdtg1);
	DT1 = (pow(2,7)-1)*Tdtg1;
	printf("0--(2^(7)-1)*Tdtg1 :\n DT1 = 0ns ~~~~~%0.2lf ns\n",DT1);
	
	printf("----------------第二种-------------\n");
	double DT2_0,DT2_1;///持续时间
	Tdtg2 = atof(str)*2;
	printf("Tdtg2 = TDTS*2: %0.2lf ns\n",Tdtg2);
	printf("DTG[7:5]=10x => DT=(64+DTG[5:0])Tdtg2 , Tdtg2 = 2×TDTS\n");
	DT2_0 = 64*Tdtg2;
	DT2_1 = (64+(pow(2,6)-1))*Tdtg2;
	printf("(64 + 0 ~ 64 + 2 ^ 6 - 1)*2* Tdtg2:\n DT2 =%0.2lf ns~~~%0.2lf ns\n",DT2_0,DT2_1);
	
	printf("----------------第三种-------------\n");
	double DT3_0,DT3_1;///持续时间
	Tdtg3 = atof(str)*8;
	printf("Tdtg3 = TDTS*8: %0.2lf ns\n",Tdtg3);
	DT3_0 = 32*Tdtg3;
	DT3_1 = (32+(pow(2,5)-1))*Tdtg3;
	printf("DTG[7:5]=110 => DT=(32+DTG[4:0]) × Tdtg3,Tdtg3 = 8 × TDTS\n");
	printf("(32 + 0 ~ 32 + 2 ^ 5 - 1)*2* Tdtg3:\n DT3 =%0.2lf ns~~~%0.2lf ns\n",DT3_0,DT3_1);
	
	printf("----------------第四种-------------\n");
	double DT4_0,DT4_1;///持续时间
	Tdtg4 = atof(str)*16;
	printf("Tdtg4 = TDTS*16: %0.2lf ns\n",Tdtg4);
	DT4_0 = 32*Tdtg4;
	DT4_1 = (32+(pow(2,5)-1))*Tdtg4;
	printf("DTG[7:5]=110 =16> DT=(32+DTG[4:0]) × Tdtg4,Tdtg4 = 8 × TDTS\n");
	printf("(32+0~32+ 2^4 - 1)*2* Tdtg4: \n DT4 =%0.2lf ns~~~%0.2lf ns\n",DT4_0,DT4_1);
	
	printf("输入你需要死区时间(ns)--尽量选在区域内的范围的死区时间\n");
	scanf("%lf",&time);
	
	double ret = 0;
	int result = 0;
	if(time ==DT1 || time < DT1)
	{
		DTG[7]=0;
		printf("属于第一情况:最高永远为 0 \n");
		printf("DT1:%0.2lf\n",Tdtg1);
		ret =  time / Tdtg1+0.5; 四舍五入
		printf("time / Tdtg1 =%0.0lf\n",ret);
		result = dectobin(ret);
		printf("位数不够7位时,在给寄存器赋值时要自已在前面手动补0----DTG[6:0]\n");
		printf("DTG[6:0]:%d\n",result);
		printf("DTG[7:0]:0b0 %d\n",result);
		return 0;
	}
	if((time >DT2_0 && time < DT2_1) || time == DT2_0 || time == DT2_1)
	{
		DTG[0]=1;DTG[1]=1;
		printf("属于第二情况:DTG[7:6]永远为 10 \n");
		printf("DT1:%0.2lf\n",Tdtg2);
		ret =  time / Tdtg2 - 64+0.5;    四舍五入
		printf("time / Tdtg2 =%0.0lf\n",ret);
		result =dectobin(ret);
		printf("位数不够6位时,在给寄存器赋值时要自已在前面手动补0----DTG[5:0]\n");
		printf("DTG[5:0]:%d\n",result);	
		printf("DTG[7:0]:0b10 %d\n",result);
		return 0;
	}
	if((time >DT3_0 && time < DT3_1 )|| time == DT3_0 || time == DT3_1)
	{
		DTG[0]=1;DTG[1]=1;DTG[2]=0;
		printf("属于第三情况:DTG[7:5]永远为 110 \n");
		printf("DT1:%0.2lf\n",Tdtg3);
		ret =  time / Tdtg3 - 32+0.5;    四舍五入
		printf("time / Tdtg3 =%0.0lf\n",ret);
		result =dectobin(ret);
		printf("位数不够5位时,在给寄存器赋值时要自已在前面手动补0----DTG[4:0]\n");
		printf("DTG[4:0]:%d\n",result);	
		printf("DTG[7:0]:0b110 %d\n",result);
		return 0;
	}
	if((time >DT4_0 && time < DT4_1 )|| time == DT4_0 || time == DT4_1)
	{
		DTG[0]=1;DTG[1]=1;DTG[2]=1;
		printf("属于第四情况:DTG[7:5]永远为 111 \n");
		printf("DT1:%0.2lf\n",Tdtg4);
		ret =  time / Tdtg4 - 32+0.5;    四舍五入
		printf("time / Tdtg4 =%0.0lf\n",ret);
		result =dectobin(ret);
		printf("位数不够5位时,在给寄存器赋值时要自已在前面手动补0----DTG[4:0]\n");
		printf("DTG[4:0]:%d\n",result);	
		printf("DTG[7:0]:0b110 %d\n",result);
	}
	return 0;
}

本次代码实现有些瑕疵,

例如第三种情况: 定时器频率:72Mhz 死区时间:5000ns

在这里插入图片描述
**

这里的DTG[4:0]应该是 01101;DTG[7:0]应该是0b110 01101;

有能力者可以帮我完善一下也行!哈哈哈!## 有错误请指出

**

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值