一、基本概念理解 1. 输出比较:打开一个TIMx计数器,再打开TIMx的一路或几路输出比较器(共4路),都配置好以后,计数器开始计数,当计数器里的值和 比较寄存器里的值相等时,产生输出比较中断,在中断中将计数器中的值读出,与翻转周期相加再写道比较寄存器中,使得和 下一个事件有相同的翻转周期。 2. 举例说明:例如TIM时钟频率设置为12MHZ,输出比较寄存器中的自装载值为600(高电平或低电平计数值),则输出的PWM频率为 frequency = 12MHZ/(600*2)=10KHZ。 二、程序设计与分析 1. TIM计数器和输出比较器的配置 uint16_t capture = 0;
extern __IO uint16_t CCR1_Val;
extern __IO uint16_t CCR2_Val;
extern __IO uint16_t CCR3_Val;
extern __IO uint16_t CCR4_Val;
void TIM_Configuration(void)
{ /* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535; //这里必须是65535
TIM_TimeBaseStructure.TIM_Prescaler = 2; //3分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //初始化TIM3 /* Output Compare Toggle Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //输出比较翻转模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能通道1
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //待装入输出比较寄存器中的脉冲值 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出为正逻辑
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //写入配置 TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable); //使能或者失能TIMx在CCR1上的预装载寄存器 /* Output Compare Toggle Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val; TIM_OC2Init(TIM3, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable); /* Output Compare Toggle Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val; TIM_OC3Init(TIM3, &TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable); /* Output Compare Toggle Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val; TIM_OC4Init(TIM3, &TIM_OCInitStructure); TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable); /* TIM enable counter */
TIM_Cmd(TIM3, ENABLE); //开启计数器 /* TIM IT enable */ //TIM1中断源设置,开启相应通道的捕捉比较中断
TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE); }
2. 时钟配置 3. GPIO配置 4. 中断服务程序 void TIM3_IRQHandler(void)
{
/* TIM3_CH1 toggling with frequency = 183.1 Hz */
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET) //检查指定的TIM中断发生与否
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 ); //清除TIMx的中断待处理位 capture = TIM_GetCapture1(TIM3); //获得输入捕获值,即计数器中的计数值
TIM_SetCompare1(TIM3, capture + CCR1_Val ); //将计数值加上翻转的脉冲值写入输出比较寄存器中,以保证下一个TIM事 //件也是相同的脉冲数
} /* TIM3_CH2 toggling with frequency = 366.2 Hz */
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
capture = TIM_GetCapture2(TIM3);
TIM_SetCompare2(TIM3, capture + CCR2_Val); } /* TIM3_CH3 toggling with frequency = 732.4 Hz */
if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
capture = TIM_GetCapture3(TIM3);
TIM_SetCompare3(TIM3, capture + CCR3_Val);
} /* TIM3_CH4 toggling with frequency = 1464.8 Hz */
if (TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
capture = TIM_GetCapture4(TIM3);
TIM_SetCompare4(TIM3, capture + CCR4_Val);
}
}
|