因項目使用外設較多不得已使用TIM2和TIM3的重映射,但不知為何兩個計數(shù)器內(nèi)無數(shù)據(jù),已檢查過了引腳與頻率信號連接無問題,頻率信號生成外設也有頻率信號輸出。
TIM2_CH2重映射到PB3
TIM3_CH1重映射到PB4
系統(tǒng)時鐘頻率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;?//向上計數(shù)模式 ????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;???//搶占優(yōu)先級0 ????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0;????????????????//子優(yōu)先級0 ????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE;????????????????????//IRQ通道使能 ????NVIC_Init(&NVIC_InitStructure);????????//根據(jù)指定的參數(shù)初始化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); }
熱門產(chǎn)品 :
CH397: 高集成度、低功耗USB網(wǎng)卡芯片