产生驱动电机的PWM波
时间:10-02
整理:3721RD
点击:
通过定时器4产生频率为2k,占空比为50%的PWM波。
定时器的设置如下:
void TIM4_Mode_Config(u16 m,u16 n)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// u16 CCR1_Val = 500;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = m; //μ±?¨ê±?÷′ó0??êyμ?999£??′?a1000′?£??aò????¨ê±?ü?ú
TIM_TimeBaseStructure.TIM_Prescaler = 0; //éè???¤·??μ£o2??¤·??μ£??′?a72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1 ; //éè??ê±?ó·??μ?μêy£o2?·??μ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down; //?òé???êy?£ê?
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = n; //éè??ì?±??μ£?μ±??êy?÷??êyμ??a???μê±£?μ???·¢éúì?±?
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //?????aPWM?£ê?1
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //μ±?¨ê±?÷??êy?μD?óúCCR1_Valê±?a??μ???
TIM_OC2Init(TIM4, &TIM_OCInitStructure); //ê1?üí¨μà1
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //?????aPWM?£ê?1
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //μ±?¨ê±?÷??êy?μD?óúCCR1_Valê±?a??μ???
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //ê1?üí¨μà1
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE); // ê1?üTIM4??????′??÷ARR
/* TIM3 enable counter */
TIM_Cmd(TIM4, ENABLE); //ê1?ü?¨ê±?÷4
}
仿真波形如下所述:
定时器的设置如下:
void TIM4_Mode_Config(u16 m,u16 n)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// u16 CCR1_Val = 500;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = m; //μ±?¨ê±?÷′ó0??êyμ?999£??′?a1000′?£??aò????¨ê±?ü?ú
TIM_TimeBaseStructure.TIM_Prescaler = 0; //éè???¤·??μ£o2??¤·??μ£??′?a72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1 ; //éè??ê±?ó·??μ?μêy£o2?·??μ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down; //?òé???êy?£ê?
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = n; //éè??ì?±??μ£?μ±??êy?÷??êyμ??a???μê±£?μ???·¢éúì?±?
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //?????aPWM?£ê?1
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //μ±?¨ê±?÷??êy?μD?óúCCR1_Valê±?a??μ???
TIM_OC2Init(TIM4, &TIM_OCInitStructure); //ê1?üí¨μà1
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //?????aPWM?£ê?1
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //μ±?¨ê±?÷??êy?μD?óúCCR1_Valê±?a??μ???
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //ê1?üí¨μà1
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE); // ê1?üTIM4??????′??÷ARR
/* TIM3 enable counter */
TIM_Cmd(TIM4, ENABLE); //ê1?ü?¨ê±?÷4
}
仿真波形如下所述: