你好, 我将X033的TIM3用作计数器模式, 想实现100us中断一次, 但自己实现后发现频率不正确, 代码如下:
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的系统核心主频默认是48MHz, 根据此代码传参(arr = 100 - 1, psc = SystemCoreClock / 1,000,000 - 1), 理应100us中断一次.
我在中断函数里, 每次调用就改变一次GPIO的状态:
// 每计数一次, 代表100us g_TIM3_Counter++; GPIO_WriteBit(GPIOA, GPIO_Pin_6, g_TIM3_Counter & 0x01);
如果一切没问题, 那么我在GPIO A6管脚处用示波器采样, 采样的频率周期应该是200us, 正脉宽100us, 负脉宽100us.
但实际采样的结果却是频率周期为37.20us, 正脉宽18.40us, 负脉宽18.80us, 与预计的结果相差甚远, 这是为什么?
热门产品 :
CH592: RISC-V内核BLE5.4无线MCU