你好, 我將X033的TIM3用作計數(shù)器模式, 想實現(xiàn)100us中斷一次, 但自己實現(xiàn)后發(fā)現(xiàn)頻率不正確, 代碼如下:
void?TIM3_Init(?uint16_t?arr,?uint16_t?psc?) { ????TIM_TimeBaseInitTypeDef?TIM_TimeBaseStructure?=?{?0?}; ????NVIC_InitTypeDef?NVIC_InitStructure?=?{?0?}; ????/** ?????*?100us: ?????*??????????arr?=?100?-?1 ?????*??????????psc?=?SystemCoreClock?/?1,000,000?-?1 ?????*/ ????/*?Initialize?Timer3?*/ ????TIM_TimeBaseStructure.TIM_Period?=?arr; ????TIM_TimeBaseStructure.TIM_Prescaler?=?psc; ????TIM_TimeBaseStructure.TIM_ClockDivision?=?TIM_CKD_DIV1; ????TIM_TimeBaseStructure.TIM_CounterMode?=?TIM_CounterMode_Up; ????TIM_TimeBaseInit(?TIM3,?&TIM_TimeBaseStructure?); ????TIM_ITConfig(?TIM3,?TIM_IT_Update,?ENABLE?); ????NVIC_InitStructure.NVIC_IRQChannel?=?TIM3_IRQn; ????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority?=?1; ????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0; ????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE; ????NVIC_Init(?&NVIC_InitStructure?); ????//?TIM_SelectOutputTrigger(TIM3,?TIM_TRGOSource_Update); ????/*?Enable?Timer3?*/ ????TIM_Cmd(?TIM3,?ENABLE?); }
X033的系統(tǒng)核心主頻默認(rèn)是48MHz, 根據(jù)此代碼傳參(arr = 100 - 1,? psc = SystemCoreClock / 1,000,000 - 1), 理應(yīng)100us中斷一次.
我在中斷函數(shù)里, 每次調(diào)用就改變一次GPIO的狀態(tài):
????????//?每計數(shù)一次,?代表100us ????????g_TIM3_Counter++; ????????GPIO_WriteBit(GPIOA,?GPIO_Pin_6,?g_TIM3_Counter?&?0x01);
如果一切沒問題, 那么我在GPIO A6管腳處用示波器采樣, 采樣的頻率周期應(yīng)該是200us, 正脈寬100us, 負(fù)脈寬100us.
但實際采樣的結(jié)果卻是頻率周期為37.20us, 正脈寬18.40us, 負(fù)脈寬18.80us, 與預(yù)計的結(jié)果相差甚遠(yuǎn), 這是為什么?