今天试了一下STM32F103的PWM脚输出PWM.
代码如下:
TIM_TimeBaseStructInit(&TIM1_TimeBaseStructure);
/* Time Base configuration */
TIM1_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM1_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1; //0x0020 中间对齐1模式
TIM1_TimeBaseStructure.TIM_Period = 1999; //
TIM1_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV2; //0x0100
// Initial condition is REP=0 to set the UPDATE only on the underflow
TIM1_TimeBaseStructure.TIM_RepetitionCounter = 1; //1
TIM_TimeBaseInit(TIM1, &TIM1_TimeBaseStructure); //
TIM_OCStructInit(&TIM1_OCInitStructure);
/* Channel 1, 2,3 in PWM mode */
TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM1_OCInitStructure.TIM_Pulse = 1000; //dummy value
TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
// TIM1_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM1_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
// TIM1_OCInitStructure.TIM_OCNIdleState = LOW_SIDE_POLARITY;
TIM1_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;//LOW_SIDE_POLARITY;
TIM_OC1Init(TIM1, &TIM1_OCInitStructure);
// Resynch to have the Update evend during Undeflow
TIM_GenerateEvent(TIM1, TIM_EventSource_Update);
// Clear Update Flag
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE);
TIM_ITConfig(TIM1, TIM_IT_CC4,DISABLE);
现在输出有了,但是很奇怪,
TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
这二项应该是输出使能对应到脚上,我改成Disable,完全不影响PWM输出!!!!!!