因项目使用外设较多不得已使用TIM2和TIM3的重映射,但不知为何两个计数器内无数据,已检查过了引脚与频率信号连接无问题,频率信号生成外设也有频率信号输出。
TIM2_CH2重映射到PB3
TIM3_CH1重映射到PB4
系统时钟频率144MHz
而且不知道是我程序有问题还是怎么回事之前采用TIM8的重映射进行编码器模式采集编码器信号也不成功,后无奈改成Tim4就可以。
代码如下:
void?VFC_Init(void)?{ ????TIM_DeInit(TIM2); ????TIM_DeInit(TIM3); ????TIM_DeInit(TIM7); ????TIM_TimeBaseInitTypeDef?TIM_TimeBaseInitStructure?=?{?0?}; ????NVIC_InitTypeDef?NVIC_InitStructure?=?{?0?}; ????RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7,?ENABLE); ????TIM_TimeBaseInitStructure.TIM_Prescaler?=?14400?-?1;??//定时器分频 ????TIM_TimeBaseInitStructure.TIM_CounterMode?=?TIM_CounterMode_Up;?//向上计数模式 ????TIM_TimeBaseInitStructure.TIM_Period?=?2000?-?1;???//自动重装载值 ????TIM_TimeBaseInitStructure.TIM_ClockDivision?=?TIM_CKD_DIV1; ????TIM_TimeBaseInit(TIM7,?&TIM_TimeBaseInitStructure); ????NVIC_InitStructure.NVIC_IRQChannel?=?TIM7_IRQn; ????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority?=?0;???//抢占优先级0 ????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0;????????????????//子优先级0 ????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE;????????????????????//IRQ通道使能 ????NVIC_Init(&NVIC_InitStructure);????????//根据指定的参数初始化VIC寄存器、 ????TIM_ITConfig(TIM7,?TIM_IT_Update,?ENABLE);????????//允许更新中断?,允许CC1IE捕获中断 ????TIM2_Init(); ????TIM3_Init(); ????TIM_Cmd(TIM7,?ENABLE);?????????//使能定时器7 } void?TIM3_Init(void)?{ ????GPIO_InitTypeDef?GPIO_InitStructure?=?{?0?}; ????TIM_TimeBaseInitTypeDef?TIM_TimeBaseInitStructure?=?{?0?}; ????RCC_APB2PeriphClockCmd(?RCC_APB2Periph_GPIOB?|?RCC_APB2Periph_AFIO,?ENABLE); ????GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,?ENABLE); ????RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,?ENABLE); ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_4; ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IN_FLOATING; ????GPIO_Init(?GPIOB,?&GPIO_InitStructure); ????GPIO_ResetBits(?GPIOB,?GPIO_Pin_4); ????TIM_TimeBaseInitStructure.TIM_Period?=?0xFFFF; ????TIM_TimeBaseInitStructure.TIM_Prescaler?=?0x0; ????TIM_TimeBaseInitStructure.TIM_ClockDivision?=?TIM_CKD_DIV1; ????TIM_TimeBaseInitStructure.TIM_CounterMode?=?TIM_CounterMode_Up; ????TIM_TimeBaseInitStructure.TIM_RepetitionCounter?=?0x00; ????TIM_TimeBaseInit(?TIM3,?&TIM_TimeBaseInitStructure); ????TIM_ITRxExternalClockConfig(TIM3,?TIM_TS_ETRF); ????TIM_TIxExternalClockConfig(TIM3,?TIM_TIxExternalCLK1Source_TI1, ????TIM_ICPolarity_Rising,?0);?????????//外部时钟源 ????TIM_Cmd(TIM3,?ENABLE);?????????//使能定时器2 } void?TIM2_Init(void)?{ ????GPIO_InitTypeDef?GPIO_InitStructure?=?{?0?}; ????TIM_TimeBaseInitTypeDef?TIM_TimeBaseInitStructure?=?{?0?}; ????RCC_APB2PeriphClockCmd(?RCC_APB2Periph_GPIOB?|?RCC_APB2Periph_AFIO,?ENABLE); ????GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,?ENABLE); ????RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,?ENABLE); ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_3; ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IN_FLOATING; ????GPIO_Init(?GPIOB,?&GPIO_InitStructure); ????GPIO_ResetBits(?GPIOB,?GPIO_Pin_3); ????TIM_TimeBaseInitStructure.TIM_Period?=?0xFFFF; ????TIM_TimeBaseInitStructure.TIM_Prescaler?=?0x0; ????TIM_TimeBaseInitStructure.TIM_ClockDivision?=?TIM_CKD_DIV1; ????TIM_TimeBaseInitStructure.TIM_CounterMode?=?TIM_CounterMode_Up; ????TIM_TimeBaseInitStructure.TIM_RepetitionCounter?=?0x00; ????TIM_TimeBaseInit(?TIM2,?&TIM_TimeBaseInitStructure); ????TIM_TIxExternalClockConfig(TIM2,?TIM_TIxExternalCLK1Source_TI2, ????TIM_ICPolarity_Rising,?0);?????????//外部时钟源 ????TIM_Cmd(TIM2,?ENABLE);?????????//使能定时器2 } u32?volatile?TIM7_Last2Cnt,?TIM7_Last3Cnt; u32?volatile?TIM2_ExtCntFreq,?TIM3_ExtCntFreq; //频率为TIM_ExtCntFreq void?TIM7_IRQHandler(void)?{ ????char?str[32]; ????TIM2_ExtCntFreq?=?(TIM2->CNT?-?TIM7_Last2Cnt)?*?(1?/?SAMPLE_PERIOD);?//?SAMPLE_PERIOD为采样周期0.1s ????TIM3_ExtCntFreq?=?(TIM3->CNT?-?TIM7_Last3Cnt)?*?(1?/?SAMPLE_PERIOD);?//?SAMPLE_PERIOD为采样周期0.1s ????sprintf(str,?"%3.3f",?TIM3_ExtCntFreq?/?1000.0);?????????//必须加这一句,莫明其妙 ????sprintf(str,?"%3.3f",?TIM2_ExtCntFreq?/?1000.0); ????printf("tim3?Hz:%d\n",?TIM3_ExtCntFreq); ????printf("tim2?Hz:%d\n",?TIM2_ExtCntFreq); ????TIM7_Last2Cnt?=?TIM2->CNT; ????TIM7_Last3Cnt?=?TIM3->CNT; ????TIM_ClearITPendingBit(TIM7,?TIM_IT_Update); }
热门产品 :
CH592: RISC-V内核BLE5.4无线MCU