/* ---------------------------------------------------------------
TIM2 Configuration: Output Compare Toggle Mode:
TIM2CLK = 36 MHz, Prescaler = 0x2, TIM2 counter clock = 12 MHz
CC1 update rate = TIM2 counter clock / CCR1_Val = 366.2 Hz
CC2 update rate = TIM2 counter clock / CCR2_Val = 732.4 Hz
CC3 update rate = TIM2 counter clock / CCR3_Val = 1464.8 Hz
CC4 update rate = TIM2 counter clock / CCR4_Val = 2929.6 Hz
--------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x02;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Output Compare Toggle Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 2047;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1023;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 511;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 255;
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
以上是例程里的一段程序,功能是对TIM2定时器进行控制,使得TIM2通道1产生频率为183.1Hz的方波,通道2产生频率为366.2Hz的方波,通道3产生频率为732.4赫兹的方波,通道4产生频率为1464.8赫兹的方波。在软件设计里面也写了“配置TIM2各通道均为输出比较模式(预分频系数设为2),设置各通道的参数(TIM2 CC1=0x8000、TIM2 CC2=0x4000、TIM2 CC31=0x2000、TIM2 CC4=0x1000、).在相应的TIM2定时器中断服务处理程序中根据定时器的值翻转输出电平,以输出方波。”
我现在的疑问是各通道输出的频率是怎样计算出来的?
TIM_OCInitStructure.TIM_Pulse = 2047 和 TIM2 CC1=0x8000是什么关系?
请明白的人不吝赐教,我琢磨了好几天了都没有琢磨出来。:dizzy: |