小学生
最后登录1970-1-1
在线时间 小时
注册时间2014-7-29
|
我的方法是先将触发方式选为上升沿触发,然后在第一次上升沿来到的时候将触发方式改为下降沿触发,读到此时的寄存器的值即为高电平程序时间。但是寄存器的值读出来还是方波一个周期的值,并不是高电平的值!!!
static void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 5000-1;
TIM_TimeBaseStructure.TIM_Prescaler = 719; // FreÎa20Hz
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter = 0; // ¶ÔêäèëDÅoÅÂË2¨
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ′¥·¢
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; // ¶Ôía2¿DÅoÅ2»·ÖÆμ
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; // 2»ê1óý»2æ′¥·¢
//TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //ϽμÑØ′¥·¢
TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
TIM_ICInit(TIM3, &TIM_ICInitStructure);
NVIC_Config_TIM3Capture();
TIM_Cmd(TIM3, ENABLE);
}
这是初始化的时候设置为上升沿触发。
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_CC1) == SET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
if(flag == 0) // μúò»¸öéÏéyÑØ′¥·¢ÖD¶Ï
{
flag = 1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
//TIM_SetCounter(TIM3, 0);
}
if(flag == 1) // μú¶t¸öéÏéyÑØ′¥·¢ÖD¶Ï
{
flag = 0;
printf("%d\n", TIM_GetCapture1(TIM3));
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
}
}
}
这是在中断中设置为下降沿触发。但是打印的值一直是输入信号一个周期的值!!!!
|
|