如何使用定时器捕获一路PWM波信号的频率和占空比

本次实验将采用定时器2的通道2产生两路频率和占空比均可调的PWM信号,然后使用定时器3的通道1来捕获其中的一路PWM波的频率和占空比。
1、首先来看下产生PWM波的程序,也就是和上篇博客是一样的,只不过换了个定时器而已:

void PWMOut_Init_Adjust_Duty_Fre(void)//输出两路频率可调、占空比可调的PWM波
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

	/* GPIOA and GPIOB clock enable */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_1 | GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

	GPIO_Init(GPIOA, &GPIO_InitStructure);

	
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	  /* Time base configuration */
	TIM_TimeBaseStructure.TIM_Period = 65535;//必须是这个数值65535
	TIM_TimeBaseStructure.TIM_Prescaler = 71;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

	/* Output Compare Toggle Mode configuration: Channel1 */
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;//输出比较翻转模式
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = CCR1_Val;//跳变值,也可初始化为CCR1_Val*Duty1
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OC2Init(TIM2, &TIM_OCInitStructure);

	TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable); //使能或者失能TIMx在CCR1上的预装载寄存器

	/* Output Compare Toggle Mode configuration: Channel2 */
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = CCR2_Val;//跳变值,也可初始化为CCR2_Val*Duty2
	TIM_OC3Init(TIM2, &TIM_OCInitStructure);

	TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
	
	TIM_Cmd(TIM2,ENABLE);
	TIM_ITConfig(TIM2,TIM_IT_CC2 | TIM_IT_CC3,ENABLE);
}

2、定时器2的中断服务函数:(产生PWM波)

void TIM2_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM2,TIM_IT_CC2) != RESET)//TIM_IT_CC1为捕获中断
	{
		TIM_ClearITPendingBit(TIM2,TIM_IT_CC2);
		capture = TIM_GetCapture2(TIM2);//获取当前计数器中的值
		if(PA6_state == 0)
		{
			TIM_SetCompare2(TIM2,capture + (u16)(CCR1_Val*Duty1));//注意此时的CCR1_Val是指的整个一个PWM周期内的总计数值,而在频率可调、占空比固定位50%的程序中,CCR1_Val指的是半个周期的计数值
			PA6_state = 1;
		}
		else if(PA6_state == 1)
		{
			TIM_SetCompare2(TIM2,capture + (u16)(CCR1_Val*(1-Duty1)));
			PA6_state = 0;
		}
	}
	if(TIM_GetITStatus(TIM2,TIM_IT_CC3) != RESET)
	{
		TIM_ClearITPendingBit(TIM2,TIM_IT_CC3);
		capture = TIM_GetCapture3(TIM2);
		if(PA7_state == 0)
		{
			TIM_SetCompare3(TIM2,capture + (u16)(CCR2_Val*Duty2));
			PA7_state = 1;
		}
		else if(PA7_state == 1)
		{
			TIM_SetCompare3(TIM2,capture + (u16)(CCR2_Val*(1-Duty2)));
			PA7_state = 0;
		}
	}
}

3、读取PWM波的定时器3的配置程序:

void PWM_Input_Init(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_ICInitTypeDef  TIM_ICInitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	//注意一定不要忘了配置TIM_TimeBaseInit(),每次只要使用定时器的任何模式,都需要使用TIM_TimeBaseInit()
	//不要在定时器2里面配置过了,在这个定时器3的配置程序中就忘了,他们是互不影响的
	TIM_TimeBaseStructure.TIM_Period = 65535;
	TIM_TimeBaseStructure.TIM_Prescaler = 71;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    	
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICFilter = 0x0;

    TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);
	
    TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);

    TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);

    TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);

    TIM_Cmd(TIM3, ENABLE);

    TIM_ITConfig(TIM3, TIM_IT_CC1,ENABLE);
}

4、读取PWM波的中断服务函数:

void TIM3_IRQHandler(void)
{
  TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
  IC2Value = TIM_GetCapture1(TIM3);

  if (IC2Value != 0)
  {
    DutyCycle = (TIM_GetCapture2(TIM3) * 100) / IC2Value;
    Frequency = SystemCoreClock / IC2Value;
  }
  else
  {
    DutyCycle = 0;
    Frequency = 0;
  }
}

5、显示函数:

sprintf((char*)str,"Frequency=%d",1000000/CCR1_Val);//72分频,所以计数频率是1MHz,所以PWM波频率就是1000000/CCR1_Val
LCD_DisplayStringLine(Line1,str);
sprintf((char*)str,"Duty1=%.1f",Duty1);
LCD_DisplayStringLine(Line2,str);
sprintf((char*)str,"Frequency=%d",Frequency/72);//读取的PWM波的频率,除以72是因为定时器计数的时候进行了72分频
LCD_DisplayStringLine(Line3,str);
sprintf((char*)str,"DutyCycle=%d%s",DutyCycle,"%");//读取PWM波的周期
LCD_DisplayStringLine(Line4,str);

我们读取完数据之后在进行计算PWM波频率的时候,如果你的定时器进行了分频,那么最后算出来的结果也需要除以相应的分频系数才是正确的结果,例如我在配置定时器的时候,进行了72分频,然后我在LCD进行显示的时候,让变量Frequency又除以了72,这样算出来的结果才是PWM波的真正的频率。

接下来简单说一下捕获PWM波的基本原理:

  • 每个定时器有四个输入捕获通道IC1、IC2、IC3、IC4,且IC1和IC2为一组,IC3和IC4为一组,都可以设置管脚和寄存器的对应关系
  • 同一个TIM通道映射了两个ICx信号
  • 两个边沿信号中的一个被选为触发信号(也就是上升沿触发或者下降沿触发)
  • 当触发信号来临时,被设置成触发信号的捕获寄存器,已经捕获了“一个PWM”周期(即连续的两个上升沿或者下降沿),它里面的值就等于一个PWM波周期内TIM的计数值n
  • 同样另一个捕获通道捕获触发信号和下一个相反极性的边信号的计数个数,即一个PWM周期内的高电平持续的时间或者低电平持续的时间m
  • 由此可得:
PWM波的周期为:
T=(1/定时器计数频率)*n
PWM波的频率为:
f=1/T=定时器计数频率/n

若m为高电平的持续时间,则PWM波的占空比为:

Duty=(m/n)*100(百分制)

若m为低电平的持续时间,则PWM波的占空比为:

Duty=((n-m)/n)*100(百分制)

因为计数器为16位,所以一个周期最多计数到65535,所以测得的最小频率=

定时器时钟频率/65535

如果想测量更低的频率,我们可以使用另外一种方法进行测量:
1、定时器配置程序:

u8 TIM3CH2_Capture_Sta = 0;
u16 TIM3CH2_Capture_Val = 0;
void PWMInput_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	TIM_ICInitTypeDef  TIM_ICInitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	
	TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
	TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICFilter = 0x0;
	TIM_ICInit(TIM3,&TIM_ICInitStructure);
	
	
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
	
	TIM_ITConfig(TIM3,TIM_IT_CC2 | TIM_IT_Update,ENABLE);
	TIM_Cmd(TIM3, ENABLE);
}

2、中断处理函数:

void TIM3_IRQHandler(void)
{
	if((TIM3CH2_Capture_Sta & 0x80) == 0)
	{
		if(TIM_GetITStatus(TIM3,TIM_IT_Update) != RESET)
		{
			if(TIM3CH2_Capture_Sta & 0x40)
			{
				if((TIM3CH2_Capture_Sta & 0x3f) == 0x3f)
				{
					TIM3CH2_Capture_Sta |= 0x80;
					TIM3CH2_Capture_Val = 0xffff;
				}
			}
			else
			{
				TIM3CH2_Capture_Sta++;
			}
		}
		if(TIM_GetITStatus(TIM3,TIM_IT_CC2) != RESET)
		{
			if(TIM3CH2_Capture_Sta & 0x40)
			{
				TIM3CH2_Capture_Sta |= 0x80;
				TIM3CH2_Capture_Val = TIM_GetCapture2(TIM3);
				TIM_OC2NPolarityConfig(TIM3,TIM_ICPolarity_Rising);
			}
			else
			{
				TIM3CH2_Capture_Sta = 0;
				TIM3CH2_Capture_Val = 0;
				TIM3CH2_Capture_Sta |= 0x40;
				TIM_SetCounter(TIM3,0);
				TIM_OC2NPolarityConfig(TIM3,TIM_ICPolarity_Falling);
			}
		}
	}
	TIM_ClearITPendingBit(TIM3,TIM_IT_CC2 | TIM_IT_Update);
}

使用这种方法,虽然可以测量低频,但是中断服务函数稍微复杂了点,如果知道第一种测量方法已经足够用了,就尽量不选用第二种方法。关于第二种方法的基本原理在我之前的一篇博客里有讲解:
博客链接:https://blog.csdn.net/qq_36554582/article/details/81611272
参考资料:
https://blog.csdn.net/dainifeixiang/article/details/5499485

你可能感兴趣的:(蓝桥杯嵌入式比赛)