现在问题是我将TIM8设置为PWM输出。每次直接设置 TIM8_ARR TIM8_CCR1
总是等4到5秒才看到输出频率变化,为什么没有设置完后就生效呢。
下面是代码TIM8初始化
void TIM8_Configuration(void) //模拟频率(PC7)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure,TIM_BaseInitStructure;
TIM_TimeBaseInitTypeDef timInitStruct;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
TIM_DeInit(TIM8);
///////////////////////////////////////////////////////////////////////////////////
//下面代码是的配置是让TIM8产生PWM输出
///////////////////////////////////////////////////////////////////////////////////
TIM_ClearFlag(TIM8, TIM_FLAG_Update); //清除更新标志位
TIM_TimeBaseStructure.TIM_Period =2000;// 2000; //TIM_Period设置也是TIM_Period =(Prescaler +1) ;计数器向上计数到1000后产生更新事件,计数值归零,设定的最大计数值2000,最大计数值是0xffff
TIM_TimeBaseStructure.TIM_Prescaler = 3599; //时钟分频3599TIM2时钟 = 72Mhz / (3599 +1)= 20khz
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // 时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down; //TIM_CounterMode_Up计数方向向上计数
// TIM_BaseInitStructure.TIM_RepetitionCounter = 0; //TIM_RepetitionCounter(TIM1_RCR)=0,每次向上溢出都产生更新事件
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
// Output Compare Toggle Mode configuration: Channel1 (PC6)
//TIM1_OC1模块设置(设置1通道占空比)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 120;
TIM_OC1Init(TIM8, &TIM_OCInitStructure);
//启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置)
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);
//TIM2_OC2模块设置(设置通道2占空比)(PC7)
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // TIM_OCPolarity_High ; //高电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_Pulse = 680;
TIM_OC2Init(TIM8, &TIM_OCInitStructure);
//启用CCR2寄存器的影子寄存器(直到产生更新事件才更改设置)
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);
//TIM2_OC3模块设置(设置2通道占空比)
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
//启用CCR2寄存器的影子寄存器(直到产生更新事件才更改设置)
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);
//TIM2_OC4模块设置(设置2通道占空比)
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OC4Init(TIM8, &TIM_OCInitStructure);
//启用CCR2寄存器的影子寄存器(直到产生更新事件才更改设置)
TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable);
//死区设置
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStructure.TIM_DeadTime = 0x90; //这里调整死区大小0-0xff
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM8, &TIM_BDTRInitStructure);
// TIM_ITConfig(TIM8, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);//使能定时器的中断
TIM_ARRPreloadConfig(TIM8, ENABLE); // 使能或禁用TIMx外设ARR预载寄存器.
TIM_CtrlPWMOutputs(TIM8,ENABLE); // 使能或禁用TIM外围主输出
/////////////////////////////////////////////////////////////////////////////////////////
//关闭N通道的比较输出
TIM_CCxNCmd(TIM8,TIM_Channel_1,TIM_CCxN_Disable);
TIM_CCxNCmd(TIM8,TIM_Channel_2,TIM_CCxN_Disable);
TIM_CCxNCmd(TIM8,TIM_Channel_3,TIM_CCxN_Disable);
TIM_CCxNCmd(TIM8,TIM_Channel_4,TIM_CCxN_Disable);
///////////////////////////////////////////////////////////////////////////////////////////
TIM_Cmd(TIM8, ENABLE); //TIM1 enable counter |