中级会员
- 积分
- 291
- 金钱
- 291
- 注册时间
- 2013-11-6
- 在线时间
- 31 小时
|
1金钱
hi
以下是我使用TIM1 CH4 配置输入捕获功能,计算输入波形高电平持续的时间,不确定是哪里有问题,我还尝试了TIM1的其他通道,TIM1始终捕获不到高电平,仿真调试发现根本没有进入中断,我的配置哪里有问题?请帮我分析下,不胜感激!
可以确认输入信号是没有问题的,我使用 TIM4_CH1 PB6,TIM2_CH2 PA1,TIM5_CH1 PA0均是可以的。
timer1Ch4CaptureInit(0XFFFF,72-1);//配置TIM1以1Mhz的频率计数
配置函数
//RR ENC: TIM1_CH4 PA11
void timer1Ch4CaptureInit(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_11);
//初始化定时器1
TIM_TimeBaseStructure.TIM_Period = arr; //设定计数器自动重装值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频器
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM1输入捕获参数
TIM1_ICInitStructure.TIM_Channel = TIM_Channel_4; //CC1S=01 选择输入端 IC1映射到TI1上
TIM1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM1_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM1, &TIM1_ICInitStructure);
//中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; //TIM1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_ITConfig(TIM1,TIM_IT_Update|TIM_IT_CC4,ENABLE);//允许更新中断 ,允许CC1IE捕获中断
TIM_Cmd(TIM1,ENABLE); //使能定时器1
}
//定时器1中断服务程序
void TIM1_CC_IRQnHandler(void)
{
if((TIM1CH4_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
if(TIM1CH4_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM1CH4_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM1CH4_CAPTURE_STA|=0X80;//标记成功捕获了一次(高电平)
TIM1CH4_CAPTURE_VAL=0XFFFF;
}else TIM1CH4_CAPTURE_STA++;
}else{
rrHighPeriod = 0;
}
}
if (TIM_GetITStatus(TIM1, TIM_IT_CC4) != RESET)//捕获1发生捕获事件
{
if(TIM1CH4_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM1CH4_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿(高电平)
TIM1CH4_CAPTURE_VAL=TIM_GetCapture1(TIM1);
TIM_OC4PolarityConfig(TIM1,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
}else //还未开始,第一次捕获上升沿
{
TIM1CH4_CAPTURE_STA=0; //清空
TIM1CH4_CAPTURE_VAL=0;
TIM_SetCounter(TIM1,0);
TIM1CH4_CAPTURE_STA|=0X40; //标记捕获到了上升沿
TIM_OC4PolarityConfig(TIM1,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
}
}
}
TIM_ClearITPendingBit(TIM1, TIM_IT_CC4|TIM_IT_Update); //清除中断标志位
}
//高电平时间计算
if(TIM1CH4_CAPTURE_STA&0X80)
{
rrHighPeriod=TIM1CH4_CAPTURE_STA&0X3F;
rrHighPeriod*=65536;//溢出时间总和
rrHighPeriod+=TIM1CH4_CAPTURE_VAL;//得到总的高电平时间
printf("HIGH:%d us\r\n",rrHighPeriod);//打印总的高点平时间
TIM1CH4_CAPTURE_STA=0;//开启下一次捕获
}
|
|