21ic问答首页 - TMR3输入捕获
TMR3输入捕获
调通2024-05-11
配置了TMR3定时器输入捕获,但是全速运行会出错,代码如下:TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ICInitTypeDef TIM5_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* 开启定时器5时钟 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE); //使能TIM5时钟
TIM_TimeBaseInitStructure.TIM_Period = 0xffff; //设置定时器自动重装载值
TIM_TimeBaseInitStructure.TIM_Prescaler = 71; //以1MHz的频率计数,一次是1μs(微妙)
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStructure);//初始化TIM5的时间基数单位
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_2; //选择输入端,IC1映射到TI1上
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM5_ICInitStructure.TIM_ICFilter = 0x00; //IC1F=0000,配置滤波器,不滤波
TIM_ICInit(TIM5, &TIM5_ICInitStructure); //初始化TIM5输入捕获通道1
//中断分组初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //打开TIM5的全局中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; //抢占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; //响应优先级为1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3,ENABLE); //使能或者失能TIM5外设
TIM_ITConfig(TIM3, TIM_IT_Update|TIM_IT_CC2, ENABLE ); //使能或者失能指定的TIM中断
TIM_ICInitTypeDef TIM5_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* 开启定时器5时钟 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE); //使能TIM5时钟
TIM_TimeBaseInitStructure.TIM_Period = 0xffff; //设置定时器自动重装载值
TIM_TimeBaseInitStructure.TIM_Prescaler = 71; //以1MHz的频率计数,一次是1μs(微妙)
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStructure);//初始化TIM5的时间基数单位
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_2; //选择输入端,IC1映射到TI1上
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM5_ICInitStructure.TIM_ICFilter = 0x00; //IC1F=0000,配置滤波器,不滤波
TIM_ICInit(TIM5, &TIM5_ICInitStructure); //初始化TIM5输入捕获通道1
//中断分组初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //打开TIM5的全局中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; //抢占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; //响应优先级为1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3,ENABLE); //使能或者失能TIM5外设
TIM_ITConfig(TIM3, TIM_IT_Update|TIM_IT_CC2, ENABLE ); //使能或者失能指定的TIM中断
赞0
评论
2024-05-13
赞0
评论
2024-05-13
赞0
评论
2024-05-11
您需要登录后才可以回复 登录 | 注册