想用定时器1用作捕捉实验:
想配置TIM1_CH1为捕捉通道,通过更新和捕捉触发来测量按键输入
问题是:按照通用定时器来配置,并没有执行定时器的捕捉中断函数,也就是说定时器的捕捉没有开启成功
想问的是高级定时器怎么配置?
(库函数)程序如下:
void TIM1CH1_CAP_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_Initstrue;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstrue;
TIM_ICInitTypeDef TIM_ICInitstrue;
NVIC_InitTypeDef NVIC_Initstrue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1|RCC_APB2Periph_GPIOA,ENABLE);
/*GPIOA的8引脚为上拉输入*/
GPIO_Initstrue.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Initstrue.GPIO_Pin=GPIO_Pin_8;
GPIO_Initstrue.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_Initstrue);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
/*配置定时器1计数,向上计数*/
TIM_TimeBaseInitstrue.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitstrue.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitstrue.TIM_Prescaler=psc;
TIM_TimeBaseInitstrue.TIM_Period=arr;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitstrue);
/*打开定时器通道1并设置初始为下降沿触发*/
TIM_ICInitstrue.TIM_Channel=TIM_Channel_1;
TIM_ICInitstrue.TIM_ICPolarity=TIM_ICPolarity_Falling;
TIM_ICInitstrue.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitstrue.TIM_ICFilter=0x00;
TIM_ICInitstrue.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInit(TIM1,&TIM_ICInitstrue);
/**/
NVIC_Initstrue.NVIC_IRQChannel=TIM1_CC_IRQn|TIM1_UP_IRQn;
NVIC_Initstrue.NVIC_IRQChannelCmd=ENABLE;
NVIC_Initstrue.NVIC_IRQChannelPreemptionPriority=2;
NVIC_Initstrue.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_Initstrue);
TIM_ITConfig(TIM1,TIM_IT_Update|TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM1,ENABLE);
}
u8 TIM1CH1_CAPTURE_STA=0; //输入捕获状态 bit0-6为更新溢出,bit7为低电平标志,bit8为一次测量成功标志
u16 TIM1CH1_CAPTURE_VAL;
void TIM1_CC_IRQHandler(void)
{
printf("chuan:TIM1_CC_IRQHandler()....
");
if((TIM1CH1_CAPTURE_STA&0x80)==0)//如果还没测量成功
{
if(TIM_GetITStatus(TIM1,TIM_IT_Update)!=RESET)//一次更新中断到来
{
if(TIM1CH1_CAPTURE_STA&0x40)//查看是否上次捕捉是否为下降沿
{
if((TIM1CH1_CAPTURE_STA&0x3f)==0x3f) //如果更新溢出时间超出定时时间则直接赋满值
{
TIM1CH1_CAPTURE_STA|=0x80;
TIM1CH1_CAPTURE_VAL=0xffff;
}else TIM1CH1_CAPTURE_STA++;//否则记录1次溢出
}
}
}
if(TIM_GetITStatus(TIM1,TIM_IT_CC1)!=RESET)//1次捕捉触发
{
if(TIM1CH1_CAPTURE_STA&0x40)//如果上一次是下降沿,则说明这一次是上升沿,即一次测量成功
{
TIM1CH1_CAPTURE_STA|=0x80;//测量成功标志
TIM1CH1_CAPTURE_VAL=TIM_GetCapture1(TIM1);//记录当前捕捉值
TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Falling);//设置下次为下降沿
}
else
{/*若上次不是下降沿,说明一次新的测量*/
TIM1CH1_CAPTURE_STA=0;
TIM1CH1_CAPTURE_VAL=0;
TIM_SetCounter(TIM1,0);
TIM1CH1_CAPTURE_STA|=0x40;
TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Rising);//设置为上升沿
}
}
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1|TIM_IT_Update); //清除中断标志
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>