输入捕捉高电平的时间,用定时器TIM3,通道3、通道4.但是捕捉通过串口输出RC_CH[4]、RC_CH[5]数组值为0,不知道什么原因,是不是定时器没有配置好,代码如下:
[mw_shl_code=c,true]u8 TIM3CH3_CAPTURE_STA=0; //通道3输入捕获状态
u16 TIM3CH3_CAPTURE_UPVAL; //通道3输入捕获值
u16 TIM3CH3_CAPTURE_DOWNVAL;//通道3输入捕获值
u8 TIM3CH4_CAPTURE_STA=0; //通道4输入捕获状态
u16 TIM3CH4_CAPTURE_UPVAL; //通道4输入捕获值
u16 TIM3CH4_CAPTURE_DOWNVAL;//通道4输入捕获值
u16 RC_CH[8];
void TIM3_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM3_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//设置NVIC中TIM3中断
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;//通道设置为TIM3
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;//响应3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//打开中断通道
NVIC_Init(&NVIC_InitStructure);//写入配置
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//使能TIM3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//GPIOB 时钟开启
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
GPIO_Init(GPIOB, &GPIO_InitStructure);//GPIOB 0 1口配置
GPIO_ResetBits(GPIOB,GPIO_Pin_0|GPIO_Pin_1);
TIM_TimeBaseStructure.TIM_Period = 20000-1;
TIM_TimeBaseStructure.TIM_Prescaler = (72 - 1);//时钟预分频数 72M/72(1M/20000) = 50HZ
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//设置时钟分频系数:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式(0->?)
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);//配置TIM2
//初始化TIM3输入捕获参数
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01 选择输入端 IC1映射到TI1上
TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM3_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM3, &TIM3_ICInitStructure);
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_4; //CC1S=01 选择输入端 IC1映射到TI1上
TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM3_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM3, &TIM3_ICInitStructure);
TIM_ITConfig(TIM3,TIM_IT_CC3,ENABLE);//允许CC3IE捕获中断
TIM_ITConfig(TIM3,TIM_IT_CC4,ENABLE);//允许CC4IE捕获中断
TIM_Cmd(TIM3,ENABLE);//使能定时器2
}
//定时器3中断服务程序
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_CC3) == SET)//捕获1发生捕获事件
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);//清除捕获1标志位
if(TIM3CH3_CAPTURE_STA == 0)//捕获到上升沿
{
TIM3CH3_CAPTURE_UPVAL = TIM_GetCapture1(TIM3);//获取上升沿的数据
TIM3CH3_CAPTURE_STA = 1; //标记以捕获到了上升沿
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling);//设置为下降沿捕获
}
else //捕获到下降沿 (已经捕获到一个完整的高电平脉冲!)
{
TIM3CH3_CAPTURE_DOWNVAL = TIM_GetCapture1(TIM3);//获取下降沿的数据
//判读是否超出发生了溢出,计算高电平脉冲时间us
if(TIM3CH3_CAPTURE_DOWNVAL<TIM3CH3_CAPTURE_UPVAL)
{
RC_CH[4] = 20000 - TIM3CH3_CAPTURE_UPVAL + TIM3CH3_CAPTURE_DOWNVAL;
}
else
{
RC_CH[4] = TIM3CH3_CAPTURE_DOWNVAL- TIM3CH3_CAPTURE_UPVAL;
}
TIM3CH3_CAPTURE_STA = 0;
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising); //设置为上升沿捕获
}
}
if(TIM_GetITStatus(TIM3, TIM_IT_CC4) == SET)//捕获1发生捕获事件
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);//清除捕获1标志位
if(TIM3CH4_CAPTURE_STA == 0)//捕获到上升沿
{
TIM3CH4_CAPTURE_UPVAL = TIM_GetCapture1(TIM3);//获取上升沿的数据
TIM3CH4_CAPTURE_STA = 1; //标记以捕获到了上升沿
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling);//设置为下降沿捕获
}
else //捕获到下降沿 (已经捕获到一个完整的高电平脉冲!)
{
TIM3CH4_CAPTURE_DOWNVAL = TIM_GetCapture1(TIM3);//获取下降沿的数据
//判读是否超出发生了溢出,计算高电平脉冲时间us
if(TIM3CH4_CAPTURE_DOWNVAL<TIM3CH4_CAPTURE_UPVAL)
{
RC_CH[5] = 20000 - TIM3CH4_CAPTURE_UPVAL + TIM3CH4_CAPTURE_DOWNVAL;
}
else
{
RC_CH[5] = TIM3CH4_CAPTURE_DOWNVAL- TIM3CH4_CAPTURE_UPVAL;
}
TIM3CH4_CAPTURE_STA = 0;
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising); //设置为上升沿捕获
}
}
}[/mw_shl_code]
|