这里再注意一点上面左图最顶上的那句话:当外部触发信号被选为ADC规则或注入转换时,只有它的上升沿可以启动转换。这跟下面的定时器2的正确配置关系很大。
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Period = 10000; //设置100mS一次TIM2比较的周期
TIM_TimeBaseStructure.TIM_Prescaler = 719; //系统主频72M,这里分频720,相当于100K的定时器2时钟
//采集时间 = TIM_Period/(系统主频/TIM_Prescaler) = 10000/(72*10^6/720) = 100mS
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //下面详细说明
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_Pulse = 5000; //TIM_Pulse < TIM_Period,也可直接置入10000,TIM_Period = TIM_Pulse
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //如果是PWM1要为Low,PWM2则为High
TIM_OC2Init(TIM2, & TIM_OCInitStructure);
TIM_Cmd(TIM2, ENABLE);
TIM_InternalClockConfig(TIM2);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_UpdateDisableConfig(TIM2, DISABLE);
}
|