我用stm32F103定时器4捕获四路遥控器PWM信号,现在捕获基本可以实现,但是有一个问题就是始终有一路捕获到的数据是异常的,比如通道三的高电平时间大于某一数值时就会引起通道4的异常,只有通道一不会异常,其他通道都有异常的可能。有人遇到过吗,已经弄了好几天了,求大神解救啊
我把定时器的配置和中断函数贴出来,麻烦各位看一下~感激不尽
void TIM4_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM4_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能TIM4时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIOB时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; //PB6789 清除之前设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PB6789 下拉输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9); //PA0 下拉
//初始化定时器4 TIM4
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(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM4输入捕获参数
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 选择输入端 IC1映射到TI1上
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_2; //CC1S=01 选择输入端 IC1映射到TI1上
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01 选择输入端 IC1映射到TI1上
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_4; //CC1S=01 选择输入端 IC1映射到TI1上
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
//中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级1级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_ITConfig(TIM4, TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE); //允许CC1IE,CC2IE,CC3IE,CC4IE捕获中断
TIM_Cmd(TIM4,ENABLE ); //使能定时器4
}
u8 TIM4CH1_CAPTURE_STA=0; //输入捕获状态
u16 TIM4CH1_CAPTURE_UPVAL; //输入捕获值
u16 TIM4CH1_CAPTURE_DOWNVAL; //输入捕获值
u8 TIM4CH2_CAPTURE_STA=0; //输入捕获状态
u16 TIM4CH2_CAPTURE_UPVAL; //输入捕获值
u16 TIM4CH2_CAPTURE_DOWNVAL; //输入捕获值
u8 TIM4CH3_CAPTURE_STA=0; //输入捕获状态
u16 TIM4CH3_CAPTURE_UPVAL; //输入捕获值
u16 TIM4CH3_CAPTURE_DOWNVAL; //输入捕获值
u8 TIM4CH4_CAPTURE_STA=0; //输入捕获状态
u16 TIM4CH4_CAPTURE_UPVAL; //输入捕获值
u16 TIM4CH4_CAPTURE_DOWNVAL; //输入捕获值
u32 tempup1 = 0; //捕获总高电平的时间
u32 tempup2 = 0; //捕获总高电平的时间
u32 tempup3 = 0; //捕获总高电平的时间
u32 tempup4 = 0; //捕获总高电平的时间
//定时器4中断服务程序
void TIM4_IRQHandler(void)
{
/***************通道1****************/
if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET) //如果捕获到了高电平
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1); ////////////////中断清除
if(TIM4CH1_CAPTURE_STA)//已经捕获到下降沿了
{
TIM4CH1_CAPTURE_DOWNVAL=TIM_GetCapture1(TIM4); //读取下降沿时寄存器的计数值
TIM4CH1_CAPTURE_STA = 0; //捕获标志位清零
if(TIM4CH1_CAPTURE_DOWNVAL>TIM4CH1_CAPTURE_UPVAL)
tempup1 = (TIM4CH1_CAPTURE_DOWNVAL - TIM4CH1_CAPTURE_UPVAL) ;//+ tm4ch1_out_cnt*0xffff; //得到总的高电平的时间
else
tempup1 = (TIM4CH1_CAPTURE_DOWNVAL - TIM4CH1_CAPTURE_UPVAL)+0xffff ;
TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
}
else //发生捕获时间但不是下降沿,第一次捕获到上升沿,记录此时的定时器计数值
{
TIM4CH1_CAPTURE_UPVAL =TIM_GetCapture1(TIM4);
TIM4CH1_CAPTURE_STA = 1; //标记已捕获到上升沿
//tm4ch1_out_cnt = 0;
TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
}
}
/***************通道2****************/
if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET) //如果捕获到了高电平
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2); ////////////////中断清除
if(TIM4CH2_CAPTURE_STA)//已经捕获到下降沿了
{
TIM4CH2_CAPTURE_DOWNVAL=TIM_GetCapture2(TIM4); //读取下降沿时寄存器的计数值
TIM4CH2_CAPTURE_STA = 0; //捕获标志位清零
if(TIM4CH2_CAPTURE_DOWNVAL > TIM4CH2_CAPTURE_UPVAL)
tempup2 = (TIM4CH2_CAPTURE_DOWNVAL - TIM4CH2_CAPTURE_UPVAL); //得到总的高电平的时间
else
tempup2 = (TIM4CH2_CAPTURE_DOWNVAL - TIM4CH2_CAPTURE_UPVAL)+0xffff;
TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC2P=0 设置为上升沿捕获
}
else //发生捕获时间但不是下降沿,第一次捕获到上升沿,记录此时的定时器计数值
{
TIM4CH2_CAPTURE_UPVAL =TIM_GetCapture2(TIM4);
TIM4CH2_CAPTURE_STA = 1; //标记已捕获到上升沿
tm4ch2_out_cnt = 0;
TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC2P=1 设置为下降沿捕获
}
}
/***************通道3****************/
if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET) //如果捕获到了高电平
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC3); ////////////////中断清除
if(TIM4CH3_CAPTURE_STA)//已经捕获到下降沿了
{
TIM4CH3_CAPTURE_DOWNVAL=TIM_GetCapture3(TIM4); //读取下降沿时寄存器的计数值
TIM4CH3_CAPTURE_STA = 0; //捕获标志位清零
if(TIM4CH3_CAPTURE_DOWNVAL > TIM4CH3_CAPTURE_UPVAL)
tempup3 = (TIM4CH3_CAPTURE_DOWNVAL - TIM4CH3_CAPTURE_UPVAL); //得到总的高电平的时间
else
tempup3 = (TIM4CH3_CAPTURE_DOWNVAL - TIM4CH3_CAPTURE_UPVAL)+0xffff;
TIM_OC3PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC3P=0 设置为上升沿捕获
}
else //发生捕获时间但不是下降沿,第一次捕获到上升沿,记录此时的定时器计数值
{
TIM4CH3_CAPTURE_UPVAL =TIM_GetCapture3(TIM4);
TIM4CH3_CAPTURE_STA = 1; //标记已捕获到上升沿
tm4ch3_out_cnt = 0;
TIM_OC3PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC3P=1 设置为下降沿捕获
}
}
/***************通道4****************/
if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET) //如果捕获到了高电平
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC4); ////////////////中断清除
if(TIM4CH4_CAPTURE_STA)//已经捕获到下降沿了
{
TIM4CH4_CAPTURE_DOWNVAL=TIM_GetCapture4(TIM4); //读取下降沿时寄存器的计数值
TIM4CH4_CAPTURE_STA = 0; //捕获标志位清零
if(TIM4CH4_CAPTURE_DOWNVAL> TIM4CH4_CAPTURE_UPVAL)
tempup4 = (TIM4CH4_CAPTURE_DOWNVAL - TIM4CH4_CAPTURE_UPVAL); //得到总的高电平的时间
else
tempup4 = (TIM4CH4_CAPTURE_DOWNVAL - TIM4CH4_CAPTURE_UPVAL)+0xffff;
TIM_OC4PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC4P=0 设置为上升沿捕获
}
else //发生捕获时间但不是下降沿,第一次捕获到上升沿,记录此时的定时器计数值
{
TIM4CH4_CAPTURE_UPVAL =TIM_GetCapture4(TIM4);
TIM4CH4_CAPTURE_STA = 1; //标记已捕获到上升沿
tm4ch4_out_cnt = 0;
TIM_OC4PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC4P=1 设置为下降沿捕获
}
}
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4); //清除中断标志位 |TIM_IT_Update
}
|