stm32PWM输入捕获模式详解

一、概念理解

PWM输入捕获模式是输入捕获模式的特例,自己理解如下

  1. 每个定时器有四个输入捕获通道IC1、IC2、IC3、IC4。且IC1 IC2一组,IC3 IC4一组。并且可是设置管脚和寄存器的对应关系。

  2. 同一个TIx输入映射了两个ICx信号。

  3. 这两个ICx信号分别在相反的极性边沿有效。

  4. 两个边沿信号中的一个被选为触发信号,并且从模式控制器被设置成复位模式。

  5. 当触发信号来临时,被设置成触发输入信号的捕获寄存器,捕获“一个PWM周期(即连续的两个上升沿或下降沿)”,它等于包含TIM

    时钟周期的个数(即捕获寄存器中捕获的为TIM的计数个数n)。

  6. 同样另一个捕获通道捕获触发信号和下一个相反极性的边沿信号的计数个数m,即(即高电平的周期或低电平的周期)

  7. 由此可以计算出PWM的时钟周期和占空比了

    frequency=f(TIM时钟频率)/n。

    duty cycle=(高电平计数个数/n),

    若m为高电平计数个数,则duty cycle=m/n

    若m为高电平计数个数,则duty cycle=(n-m)/n

    注:因为计数器为16位,所以一个周期最多技术65535个,所以测得的 最小频率= TIM时钟频率/65535。

    二、程序设计与分析

  8. 程序概述:选择TIM3作为PWM输入捕获。IC2设置为上升沿,并设置为有效的触发输入信号。所以IC2的捕获寄存器捕获PWM周期,

    IC1的捕获寄存器捕获PWM的高电平周期。

    2.程序代码如下:



    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟配置



    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);



    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //GPIO配置

    PIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOA, &GPIO_InitStructure);



    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //NVIC配置

    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_2; //通道选择

    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿触发

    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //管脚与寄存器对应关系

    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //输入预分频。意思是控制在多少个输入周期做一次捕获,如果

    //输入的信号频率没有变,测得的周期也不会变。比如选择4分频,则每四个输入周期才做一次捕获,这样在输入信号变化不频繁的情况下,

    //可以减少软件被不断中断的次数。

    TIM_ICInitStructure.TIM_ICFilter = 0x0; //滤波设置,经历几个周期跳变认定波形稳定0x0~0xF
    TIM_PWMIConfig(TIM3, &TIM_ICInitStructure); //根据参数配置TIM外设信息

    TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2); //选择IC2为始终触发源

    TIM_SelectSlaveMode(TIM3, TI***aveMode_Reset);//TIM从模式:触发信号的上升沿重新初始化计数器和触发寄存器的更新事件

    TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); //启动定时器的被动触发

    TIM_Cmd(TIM3, ENABLE); //启动TIM2

    TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE); //打开中断
    中断处理函数
    void TIM3_IRQHandler(void)
    {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); //清楚TIM的中断待处理位

    IC2Value = TIM_GetCapture2(TIM3); //读取IC2捕获寄存器的值,即为PWM周期的计数值
    if (IC2Value != 0)
    {
    DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value; //读取IC1捕获寄存器的值,并计算占空比

    Frequency = 72000000 / IC2Value; //计算PWM频率。
    }
    else
    {
    DutyCycle = 0;
    Frequency = 0;
    }
    }

注(一):若想改变测量的PWM频率范围,可将TIM时钟频率做分频处理

TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //周期0~FFFF

TIM_TimeBaseStructure.TIM_Prescaler = 5; //时钟分频,分频数为5+1即6分频

TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分割

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//模式

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);//基本初始化

注注(二):定时器TIM的倍频器X1或X2。在APB分频为1时,倍频值为1,否则为2。



用的输入捕获功能







IO配置

GPIO_InitTypeDef GPIO_InitStructure; //

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);





GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_ResetBits(GPIOB,GPIO_Pin_7);

中断配置

void NVIC_Configuration_Cap(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM

NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //占先优先级、副优先级的资源分配


NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //指定中断源
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //占先优先级设定
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //副优先级设定
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
时钟配置
void TIM4_Cap_Init(u16 arr, u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE);

TIM_DeInit(TIM4); //初始化TIM4为缺省值 0

TIM_TimeBaseStructure.TIM_Period = arr; //arr=10000
TIM_TimeBaseStructure.TIM_Prescaler = psc; //psc=71
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //上升沿捕获
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
//TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2 ); //选择时钟触发源,这里只能用1或2,意味着通道34不能用输入触发
//TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);//触发方式
// TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable); //启动定时器的被动触发
TIM_Cmd(TIM4, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE); //允许捕获中断
TIM_ITConfig(TIM4,TIM_IT_Update, ENABLE); //允许更新中断


TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //清除更新中断标记
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2); //清除捕获中断标记
}




中断服务函数


u16 STA=0;
u16 VAL;
u8 CAP_N=0;
void TIM4_IRQHandler(void)
{
if((STA&0x8000)==0) //捕获开启标志
{
if(TIM_GetITStatus(TIM4,TIM_IT_Update)!=RESET) //进入更新中断
{
if(CAP_N==1)
{
if((STA&0X3FFF)==0X3FFF)//高电平太长了
{STA|=0X8000;//标记成功捕获了一次
VAL=0XFFFF;
}
else STA++;
}
}
if(TIM_GetITStatus(TIM4,TIM_IT_CC2)!=RESET) //进入捕获中断
{
if(CAP_N==1)
{STA|=0x8000;//标志完成捕获过程,进入主函数处理部分}
VAL=TIM_GetCapture2(TIM4);
}
else
{STA=0;
VAL=0;
TIM_SetCounter(TIM4,0);
CAP_N=1;
}
}
}
TIM_ClearITPendingBit(TIM4, TIM_IT_Update|TIM_IT_CC2);
}




数据处理主函数


void TIM_CAP(void)
{
u32 CAP_temp=0;
NVIC_Configuration_Cap();
TIM4_Cap_Init(9999,71);

while(1)
{
delay_ms(10);
if(STA&0x8000)
{
CAP_temp=STA&0x3fff;
printf("\r\nSTA=%d",CAP_temp);
CAP_temp*=10000;
CAP_temp+=VAL;
//printf("\r\nVAL=%d",VAL);
printf("\r\n周期:%d us",CAP_temp);


CAP_N=0;
STA=0;
}
}
}
PWM输入功能


void Confiuration_PWM_CAP(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_7; //GPIO配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //NVIC配置
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);


TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //周期0~FFFF
TIM_TimeBaseStructure.TIM_Prescaler = 0; //时钟分频,分频数为5+1即6分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);//基本初始化


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; //输入预分频。意思是控制在多少个输入周期做一次捕获,如果
//输入的信号频率没有变,测得的周期也不会变。比如选择4分频,则每四个输入周期才做一次捕获,这样在输入信号变化不频繁的情况下,
//可以减少软件被不断中断的次数。
TIM_ICInitStructure.TIM_ICFilter = 0x0; //滤波设置,经历几个周期跳变认定波形稳定0x0~0xF
TIM_PWMIConfig(TIM3, &TIM_ICInitStructure); //根据参数配置TIM外设信息


TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2); //选择IC2为始终触发源
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);//TIM从模式:触发信号的上升沿重新初始化计数器和触发寄存器的更新事件
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); //启动定时器的被动触发

TIM_Cmd(TIM3, ENABLE); //启动TIM2
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE); //打开中断


}


void PWM_CAP(void)
{
Confiuration_PWM_CAP();
while(1)
{
printf("\r\nFrequency=%d",Frequency);
printf("\r\nDutyCycle=%f",DutyCycle);
}


}




中断服务函数


float IC2Value;
float DutyCycle;
u16 Frequency;
void TIM3_IRQHandler(void)
{

TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); //清除TIM的中断待处理位

IC2Value = TIM_GetCapture2(TIM3); //读取IC2捕获寄存器的值,即为PWM周期的计数值
if (IC2Value != 0)
{

DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value; //读取IC1捕获寄存器的值,并计算占空比
Frequency = 72000000 / IC2Value; //计算PWM频率。
}
else
{
DutyCycle = 0;
Frequency = 0;
}
}

你可能感兴趣的:(单片机学习,嵌入式)