本人使用PWM输出时,出现比较大的误差,-/+几百Hz。以下是我的初始化代码,望各位大神多多指教。
#define SYSCLOCK 16000000UL
#define CARRIER_WAVE_FREQUENCY 38000UL
/***************************************************************************
* @Func
tion : IR_Init()
* @Description : init IR gpio and pwm
* @parameter : void
* @return : void
***************************************************************************/
void IR_Init(void)
{
TIM1_DeInit();
/*
TIM1_Period = SYSCLOCK/CARRIER_WAVE_FREQUENCY-1
TIM1_Prescaler = 0
TIM1_CounterMode = TIM1_COUNTERMODE_UP
TIM1_RepetitionCounter = 0
*/
TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, SYSCLOCK/CARRIER_WAVE_FREQUENCY-1, 0);
TIM1_OC1Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE,
TIM1_OUTPUTNSTATE_DISABLE, SYSCLOCK/CARRIER_WAVE_FREQUENCY/2-1,
TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_HIGH,
TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_SET);
TIM1_Cmd(ENABLE);
//TIM1_OC2PreloadConfig(ENABLE);
TIM1_CtrlPWMOutputs(ENABLE);
}