TIM-PWM边沿检测时TI1F_ED的应用解析
TIM-PWM边沿检测时TI1F_ED的应用解析
定时器框图TI1F_ED指示如下所示:
以TIMx_CH1的信号为例:
TI1FP1和TI1FP2解析:从TIMx_CH1进入的TI1信号,经过输入滤波器生成TI1F,再经过边沿检测器(捕获极性选择上升沿或下降沿)生成TI1FP1和TI1FP2。
TI1F_ED解析:TI1经过边沿检测器后生成TI1F_ED信号(双边沿检测,在捕获到TI1F上的边沿后,生成脉冲信号,详解如下图所示)
TRC解析:TI1FED信号经过选择器生成TRC信号,将IC1的输入捕获信号配置成TRC信号,即之前边沿检测器检测TI1F_ED的信号,无论捕获极性选择的是检测上升沿还是下降沿,都可以检测到TI1F信号的双边沿。
使用限制:从定时器框图可以看出,只有TIMx_CH1的信号是直接通过选择器生成TI1信号的,第二个选择则是TIMx_CH1、TIMx_CH2、TIMx_CH3三个通道的信号经过异或生成的TI1信号。
使用场景:TI1F_ED的应用实际上多用于使用限制中的第二个选择,使用场景为有感无刷电机应用中,三个定时器通道引脚对应着霍尔传感器的是哪个信号检测位置;三个通道的信号经过异或后生成新的TI1信号,在间隔60°相位上的每个边沿指导电机进行换相的操作,即一旦捕获到边沿即进行换相的操作。
附CH32V307VCT6使用TIM1_CH1的TI1F_ED捕获通道双边沿例程
#include "debug.h"
void TIM1_CC_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM1_CC_IRQHandler(void)
{
if( TIM_GetITStatus( TIM1, TIM_IT_CC1 ) != RESET )
{
GPIOA->BCR=GPIO_Pin_7;
GPIOA->BSHR=GPIO_Pin_7;
TIM_SetCounter( TIM1, 0 );
}
if( TIM_GetITStatus( TIM1, TIM_IT_Trigger ) != RESET )
{
GPIOA->BCR=GPIO_Pin_6;
GPIOA->BSHR=GPIO_Pin_6;
TIM_SetCounter( TIM1, 0 );
}
TIM_ClearITPendingBit( TIM1, TIM_IT_CC1 );
}
void Input_Capture_Init( u16 arr, u16 psc )
{
GPIO_InitTypeDef GPIO_InitStructure={0};
TIM_ICInitTypeDef TIM_ICInitStructure={0};
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};
NVIC_InitTypeDef NVIC_InitStructure={0};
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_TIM1, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init( GPIOA, &GPIO_InitStructure);
GPIO_ResetBits( GPIOA, GPIO_Pin_8 );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOA, &GPIO_InitStructure);
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0x00;
TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_TRC;
TIM_ICInit( TIM1, &TIM_ICInitStructure );
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig( TIM1, TIM_IT_CC1|TIM_IT_Trigger, ENABLE );
TIM_SelectInputTrigger( TIM1, TIM_TS_TI1F_ED );
TIM_SelectSlaveMode( TIM1, TIM_SlaveMode_Reset );
TIM_SelectMasterSlaveMode( TIM1, TIM_MasterSlaveMode_Enable );
// TIM_SelectHallSensor(TIM1,ENABLE);
TIM_Cmd( TIM1, ENABLE );
}
int main(void)
{
SystemCoreClockUpdate();
Input_Capture_Init( 0xFFFF, 144-1 );
while(1);
}