//选定工作频率
IWDG_SetPrescaler(IWDG_Prescaler_32);
//设置重装载值
IWDG_SetReload(230);
//按照重装载器的值重装载IWDG计数器的值-----------此动作相当于“喂狗”
IWDG_ReloadCounter();
//使能IWDG
IWDG_Enable();
}
/ *******************
/
TIM_DeInit(TIM1);//将外设TIMx寄存器重设为缺省值
//2、基础设置
TIM1_TimeBaseStructure.TIM_Prescaler = 0x0; //预分频,此值+1为分频的除数得到计数周期;
TIM1_TimeBaseStructure.TIM_Period = 0xFFFF;//计数周期(即当计数值达到“计数值”时,刚好用时一个计数周期)
TIM1_TimeBaseStructure.TIM_ClockDivision = 0x0;//时钟因子
TIM1_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM1_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数器模式
//TIM_CounterMode_Dowm向下计数
//TIM_CounterMode_CenterAligned1 中心对齐方式1
// TIM_CounterMode_CenterAligned2 中心对齐方式2
// TIM_CounterMode_CenterAligned3 中心对齐方式3
TIM_TimeBaseInit(TIM1,&TIM1_TimeBaseStructure);
// 3、输出通道设置
TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
//TIM_OCMode_Timing输出比较时间模式(输出引脚冻结无效)
//TIM_OCMode_Active 输出比较主动模式(匹配时设置输出引脚为有效电平,当计数值为比较/捕获寄存器值相同时,强制输出为高平)TIM_OCMode_Inactive; 输出比较非主动模式 (匹配时设置输出引脚为无效电平,当计数值为比较/捕获寄存器值相同时,强制输出为低电平)
//TIM_OCMode_Toggle 输出比较触发模式(翻转。当计数值与比较/捕获寄存器值相同时,翻转输出引脚的电平)
//TIM_OCMode_PWM1向上计数时,当TIMx_CNT < TIMx_CCR*时,输出电平有效,否则为无效,
//向下计数时,当TIMx_CNT > TIMx_CCR*时,输出电平无效,否则为有效
//TIM_OCMode_PWM2 ;与PWM1模式相反
TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM1_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM1_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM1_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1,&TIM1_OCInitStructure);
TIM1_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM1,&TIM1_OCInitStructure);
TIM1_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM1,&TIM1_OCInitStructure);
TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM1_BDTRInitStructure.TIM_DeadTime = 0x75;
TIM1_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStructure);
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);