输入捕捉模式和PWM输入模式的区别
Stm32的通用定时器具备基本的输入捕捉功能。所谓输入捕捉功能,是指通用定时器可以通过检测输入信号的跳变沿,检测到跳变沿的同时将计数器的当前值写入相应的寄存器。我们可以利用定制器的输入捕捉模式可以测量输入信号的高电平时间、占空比和频率。
1、输入捕捉模式
Stm32的通用TIM2、3、4、5都具有输入捕捉的功能,每个定时器具有四个通道,并且每一个通道都可以单独配置为输入捕捉模式,主要用于测量输入信号的高电平时间,也可测量信号的频率(可能不太精确,尤其对于频率很高的信号),如果用于测量信号的高电平时间,配置时需要注意定时器的时基频率,下面的程序中有所说明。
下面是具体的程序,配置TIM2的四个通道为基本输入捕捉模式,定时器的时基频率为1M,用于测量输入信号的高电平时间。
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Period =0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision =0;
TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2 ,&TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM2 , 71 ,TIM_PSCReloadMode_Immediate); //即Ft=72M/72=1M
TIM2->CCMR1 &=(u16)0x0000; //清零
TIM2->CCMR1 |=(u16)0x0101; //TIM输入2与IC2相连1与IC1相连
TIM2->CCMR2 &=(u16)0x0000; //清零
TIM2->CCMR2 |=(u16)0x0101; //TIM输入3与IC3相连 4与IC4相连
TIM2->CCER = (u16)0x1111; //上升沿捕捉 捕捉使能
//开启 CC中断
//使能 Time
TIM2->DIER =(u16)0x001E; //TIM_IT_CC1 TIM_IT_CC2 TIM_IT_CC3 TIM_IT_CC4
//TIM2->DIER = (u16)0x0002;
TIM2->CR1=((u16)0x0001); //CR1_CEN_Set
u16 IC_Dat[9] = {0,0,0,0,0,0,0,0,0};
u16 CaptureNumber1 = 0,CaptureNumber2 = 0,CaptureNumber3 =0,CaptureNumber4 = 0;
vu32 Capture1 = 0,Capture2 = 0,Capture3 = 0,Capture4 = 0;
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_CC1) == SET) //判断CH1是否出现跳变沿
{
if(CaptureNumber1 ==0) //出现上升沿
{
IC_Dat[1] =TIM_GetCapture1(TIM2); //得到CH1捕捉值1
CaptureNumber1 = 1;
TIM2->CCER &=(u16)0xFFFD; //改变跳变沿
TIM2->CCER |= (u16)0x0002;
}
elseif(CaptureNumber1 ==1) //出现下降沿
{
IC_Dat[2] = TIM_GetCapture1(TIM2);//得到CH1捕捉值2
TIM2->CCER &=(u16)0xFFFD; //改变跳变沿
TIM2->CCER |= (u16)0x0000;
if (IC_Dat[2] > IC_Dat[1])
{
Capture1 = (IC_Dat[2] - IC_Dat[1]);
}
else
{
Capture1 = ((0xFFFF - IC_Dat[1]) + IC_Dat[2]);
}
CaptureNumber1 = 0;
//printf("rn TheIC12of input pulse is %d rn" ,IC_Dat[2]);
//printf("rn TheIC11of input pulse is %d rn" ,IC_Dat[1]);
printf("rn TheCapture1of input pulse is %d rn" ,Capture1);
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
}
else if(TIM_GetITStatus(TIM2, TIM_IT_CC2) ==SET)//判断CH2是否出现跳变沿
{
if(CaptureNumber2 ==0) //出现上升沿
{
IC_Dat[3] =TIM_GetCapture2(TIM2); //得到CH2捕捉值1
CaptureNumber2 = 1;
TIM2->CCER &=(u16)0xFFDF; //改变跳变沿
TIM2->CCER |= (u16)0x0020;
}
elseif(CaptureNumber2 ==1) //出现下降沿
{
IC_Dat[4] = TIM_GetCapture2(TIM2); //得到CH2捕捉值2
TIM2->CCER &=(u16)0xFFDF; //改变跳变沿
TIM2->CCER |= (u16)0x0000;
if (IC_Dat[4] > IC_Dat[3])
{
Capture2 = (IC_Dat[4] - IC_Dat[3]);
}
else
{
Capture2 = ((0xFFFF - IC_Dat[3]) + IC_Dat[4]);
}
CaptureNumber2 = 0;
//printf("rn TheIC22of input pulse is %d rn" ,IC_Dat[4]);
// printf("rn The IC21ofinput pulse is %d rn" , IC_Dat[3]);
printf("rn TheCapture2of input pulse is %d rn" ,Capture2);
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
}
else if(TIM_GetITStatus(TIM2, TIM_IT_CC3) ==SET)//判断CH3是否出现跳变沿
{
if(CaptureNumber3 ==0) //出现上升沿
{
IC_Dat[5] =TIM_GetCapture3(TIM2); //得到CH3捕捉值1
CaptureNumber3 = 1;
TIM2->CCER &=(u16)0xFDFF; //改变跳变沿
TIM2->CCER |= (u16)0x0200;
}
elseif(CaptureNumber3 ==1) //出现下降沿
{
IC_Dat[6] = TIM_GetCapture3(TIM2); //得到CH3捕捉值2
TIM2->CCER &=(u16)0xFDFF; //改变跳变沿
TIM2->CCER |= (u16)0x0000;
if (IC_Dat[6] > IC_Dat[5])
{
Capture3 = (IC_Dat[6] - IC_Dat[5]);
}
else
{
Capture3 = ((0xFFFF - IC_Dat[5]) + IC_Dat[6]);
}
CaptureNumber3 = 0;
//printf("rn TheIC32of input pulse is %d rn" ,IC_Dat[6]);
//printf("rn TheIC31of input pulse is %d rn" ,IC_Dat[5]);
printf("rn TheCapture3of input pulse is %d rn" ,Capture3);
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
}
else if(TIM_GetITStatus(TIM2, TIM_IT_CC4) ==SET)//判断CH3是否出现跳变沿
{
if(CaptureNumber4 ==0) //出现上升沿
{
IC_Dat[7] =TIM_GetCapture4(TIM2); //得到CH3捕捉值1
CaptureNumber4 = 1;
TIM2->CCER &=(u16)0xDFFF; //改变跳变沿
TIM2->CCER |= (u16)0x2000;
}
elseif(CaptureNumber4 ==1) //出现下降沿
{
IC_Dat[8] = TIM_GetCapture4(TIM2); //得到CH3捕捉值2
TIM2->CCER &=(u16)0xDFFF; //改变跳变沿
TIM2->CCER |= (u16)0x0000;
if (IC_Dat[8] > IC_Dat[7])
{
Capture4 = (IC_Dat[8] - IC_Dat[7]);
}
else
{
Capture4 = ((0xFFFF - IC_Dat[7]) + IC_Dat[8]);
}
CaptureNumber4 = 0;
// printf("rn The IC42ofinput pulse is %d rn" , IC_Dat[8]);
// printf("rn The IC41ofinput pulse is %d rn" , IC_Dat[6]);
printf("rn TheCapture4of input pulse is %d rn" ,Capture4);
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
}
}
需要注意的问题:
定时器的时基频率不能太高,如果定时器工作于36M,采集50Hz的信号就会出现偏差(实测),所以程序中将定时器的时基频率配置为了1M;
如果存在两个以上中断,需要设置中断优先级,否则容易出问题。
2、PWM输入模式
PWM输入模式是输入捕捉模式的高级应用,对于测量频率较高的输入信号的频率特别精确,当然,为了实现这个模式,也得做出一点牺牲。相比于基本输入捕捉功能的实现来说,PWM输入模式中,一路输入信号同时映射到两个引脚,而且只有第一和第二通道可以配置为这种模式,换句话说,每个通用定时器只能测量一路输入信号,具体配置过程,详见程序说明。
下面是TIM2的第二通道工作于PWM输入捕捉模式时的模块配置程序:
void TIM_Configuration(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
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 =0x00;
TIM_PWMIConfig(TIM2,&TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM2,TIM_TS_TI2FP2);
TIM_SelectSlaveMode(TIM2,TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
voidTIM2_IRQHandler(void)
{
static float IC2Value =0;
static float DutyCycle =0;
static float Frequency =0;
static float Paulse = 0;
IC2Value = TIM_GetCapture2(TIM2);
Paulse = TIM_GetCapture1(TIM2);
DutyCycle = Paulse /IC2Value;
Frequency = 72000000 / IC2Value;
printf("rn The DutyCycleofinput pulse is %%%d rn" , (u32)(DutyCycle * 100));
printf("rn The Frequencyofinput pulse is %.2fKHzrn" , (Frequency / 1000));
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
}
关于PWM输入模式工作过程的详细说明:(打字麻烦,所以直接截图过来)