|
这是找的相关代码
/* TIM2 channel 2 pin (PA.01) configuration */
//PWM capture PA1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* TIM3 channel 2 pin (PA.07) configuration */
//PWM capture PA7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
void TIM_Configuration(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//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 = 0x0;
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); /* Select the TIM2 Input Trigger: TI2FP2 */
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); /* Select the slave Mode: Reset Mode */
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); /* Enable the Master/Slave Mode */
TIM_Cmd(TIM2, ENABLE); /* TIM enable counter */
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); /* Enable the CC2 Interrupt Request */
/* Enable the TIM4 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_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_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_PWMIConfig(TIM3, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2); /* Select the TIM2 Input Trigger: TI2FP2 */
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); /* Select the slave Mode: Reset Mode */
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); /* Enable the Master/Slave Mode */
TIM_Cmd(TIM3, ENABLE); /* TIM enable counter */
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE); /* Enable the CC2 Interrupt Request */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM2_IRQ(void)
{
PlusCounter ++;
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); /* Clear TIM2 Capture compare interrupt pending bit */
IC2Value = TIM_GetCapture2(TIM2); /* Get the Input Capture value */
if (IC2Value != 0)
{
DutyCycle = (TIM_GetCapture1(TIM2) * 100) / IC2Value; /* Duty cycle computation */
Frequency = 72000000 / IC2Value; /* Frequency computation */
}
else
{
DutyCycle = 0;
Frequency = 0;
}
}
void TIM3_IRQ(void)
{
PlusCounter2 ++;
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); /* Clear TIM2 Capture compare interrupt pending bit */
IC2Value2 = TIM_GetCapture2(TIM3); /* Get the Input Capture value */
if (IC2Value2 != 0)
{
DutyCycle2 = (TIM_GetCapture1(TIM3) * 100) / IC2Value2; /* Duty cycle computation */
Frequency2 = 72000000 / IC2Value2; /* Frequency computation */
}
else
{
DutyCycle2 = 0;
Frequency2 = 0;
}
} |
|