在输入捕获源代码中:
//定时器3通道CH3输入捕获配置
TIM_ICInitTypeDef TIM3_ICInitStructure;
void TIM3_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟使能
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); //Timer3 引脚全部重映射
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PC8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PC8 下拉输入
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC,GPIO_Pin_8); //PC8 设为0
//初始化定时器3 TIM3
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(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM3输入捕获参数
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC3S=01 选择输入端 IC3映射到TI3上
TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //上升沿捕获
TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI3上
TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM3_ICInitStructure.TIM_ICFilter = 0x03;//IC3F=0011 配置输入滤波器 8个采样都是低电平认为是真的低电平到来
TIM_ICInit(TIM3, &TIM3_ICInitStructure);
//中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
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(TIM3,TIM_IT_Update|TIM_IT_CC3,ENABLE);//允许更新中断 ,允许CC3IE捕获中断
TIM_Cmd(TIM3,ENABLE ); //使能定时器3
}
u8 TIM3CH3_CAPTURE_STA=0; //输入捕获状态
u16 TIM3CH3_CAPTURE_VAL; //输入捕获值
//定时器3中断服务程序
void TIM3_IRQHandler(void)
{
if((TIM3CH3_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
if(TIM3CH3_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM3CH3_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM3CH3_CAPTURE_STA|=0X80;//标记成功捕获了一次
TIM3CH3_CAPTURE_VAL=0XFFFF;
}else TIM3CH3_CAPTURE_STA++;
}
}
if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)//捕获3发生捕获事件
{
if(TIM3CH3_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM3CH3_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
TIM3CH3_CAPTURE_VAL=TIM_GetCapture3(TIM3);
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC3P=0 设置为上升沿捕获
}else //还未开始,第一次捕获上升沿
{
TIM3CH3_CAPTURE_STA=0; //清空
TIM3CH3_CAPTURE_VAL=0;
TIM_SetCounter(TIM3,0);
TIM3CH3_CAPTURE_STA|=0X40; //标记捕获到了上升沿
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC3P=1 设置为下降沿捕获
}
}
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3|TIM_IT_Update); //清除中断标志位
}
中断服务子程序void TIM3_IRQHandler(void)中改变检测边沿极性的函数:原子哥给的是TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Rising)
但是void TIM_OC3PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
{
uint16_t tmpccer = 0;
/* Check the parameters */
assert_param(IS_TIM_LIST3_PERIPH(TIMx));
assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
tmpccer = TIMx->CCER;
/* Set or Reset the CC3P Bit */
tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC3P);
tmpccer |= (uint16_t)(TIM_OCPolarity << 8);
/* Write to TIMx CCER register */
TIMx->CCER = tmpccer;
}
#define IS_TIM_OC_POLARITY(POLARITY) (((POLARITY) == TIM_OCPolarity_High) ||
((POLARITY) == TIM_OCPolarity_Low))
并没有TIM_ICPolarity_Rising,并且OC不是输出吗,IC不是输入吗,这样做可以吗?新人新手经验不多,请多多包涵。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>