官方发布的例程都是一个定时器只工作于一种方式的,我现在让TIM2的两个通道分别工作于不同的方式,得到的实际结果与用两个定时器工作于这两种方式得到的结果有差别,我没有找到相关的解释,请香版主指教。
以下为代码和相关说明。
/* TIM2CLK = 36 MHz, Prescaler = 0x0, TIM2 counter clock = 36 MHz */
/* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR1_Val; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
/* Output Compare Toggle Mode configuration: Channel2 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR2_Val; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC2Init(TIM2, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
TIM_ARRPreloadConfig(TIM2, ENABLE); /* TIM IT enable */ TIM_ITConfig(TIM2,TIM_IT_CC2, ENABLE);
/* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE);
通道一工作于PWM方式,输出频率应该是 TIM2 counter clock/TIM_Period =549.316 Hz
通道二工作于输出比较方式,输出频率应该是TIM2 counter clock/CCR2_Val =4394.53 Hz
实测的结果:通道一输出频率没错,但是通道二输出频率却是2197.37 Hz,少了一半,不知道这个2197.37是怎么得到的? |