相利用PWM产生一个可调的 时钟 源,结果发现输出来的频率很不准,但测量MCO的频率是很准的。搞了半天也没弄明白。向大家求教:
程序如下:
void ConfigTimer(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO,ENABLE); //AFIO时钟必须开启。
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3,ENABLE);
/* STM32 默认也 TIM3是在 PA6,PA7,PB0,PB1 脚,需要Remap到新的管脚上去。 */
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
CP_1;
////TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Prescaler =0; //不再对时钟分频,时钟源是72MHZ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//trig down
TIM_TimeBaseStructure.TIM_Period =9;//用来计数,控制输出频率. 想输出8MHZ,结果只有7.2M,感觉奇怪。
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
//
//Channel1 configuration
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性设置
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse =4;//Tcp_ref; 设置占空比
TIM_OC2Init(TIM3,&TIM_OCInitStructure);
//TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable); //这句写和不写效果一样。
//test PWM
TIM_CtrlPWMOutputs(TIM3, ENABLE);
TIM_Cmd(TIM3,ENABLE);
} |