使用32TIM1输出PWM调制信号,将程序下载到芯片上时,能正常工作,输出目标PWM,但断电后再重新上电PWM输出就不正常;论坛香主说的外设还没有准备就绪,或者程序配置顺序有问题,我有些不懂,请教一下一下配置程序:
请问有什么问题么?怎么一掉电就不行了???怎样等待让外设上完电????
void InitSpwmTime1(void)
{
#if (SpwmMode==__STM32__)
TIM_TimeBaseInitTypeDef TIM1_BaseInitStruct; //Tim1基本配置
TIM_OCInitTypeDef TIM1_OCInitStruct; //配置互补推挽设置
TIM_BDTRInitTypeDef TIM1_BDTRInitStruct; //死区时间配置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);//使能定时器1时钟
TIM_DeInit(TIM1); //复位寄存器
delay(1000); //等待外设上电稳定
/*=================配置定时器=========================*/
TIM1_BaseInitStruct.TIM_Period =SPWM.Time; //中断周期
TIM1_BaseInitStruct.TIM_Prescaler=SpwmTimePrescaler;//不分频,时钟为72M
//配置定时器为中央对齐模式2技术模式
TIM1_BaseInitStruct.TIM_CounterMode=TIM_CounterMode_CenterAligned2;
TIM_TimeBaseInit(TIM1,&TIM1_BaseInitStruct); //初始化
TIM_ClearFlag(TIM1, TIM_FLAG_Update); //清除中断标志
TIM1_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1; //配置为PWM模式
//使能输出比较
TIM1_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
//使能互补输出
TIM1_OCInitStruct.TIM_OutputNState=TIM_OutputNState_Enable;
//输出比较极性为高
TIM1_OCInitStruct.TIM_Pulse =SPWM.Data[0]; //提取第一个数据
TIM1_OCInitStruct.TIM_OCPolarity =TIM_OCPolarity_High;
TIM1_OCInitStruct.TIM_OCNPolarity =TIM_OCNPolarity_High;
//
TIM1_OCInitStruct.TIM_OCIdleState =TIM_OCIdleState_Reset;
TIM1_OCInitStruct.TIM_OCNIdleState=TIM_OCNIdleState_Reset;//TIM_OCNIdleState_Set;
TIM_OC1Init(TIM1,&TIM1_OCInitStruct); //配置通道1
TIM_OC2Init(TIM1,&TIM1_OCInitStruct); //配置通道2
TIM_OC3Init(TIM1,&TIM1_OCInitStruct); //
/*===================死区时间处理======================*/
TIM1_BDTRInitStruct.TIM_OSSRState =TIM_OSSRState_Enable;
TIM1_BDTRInitStruct.TIM_OSSIState =TIM_OSSIState_Enable;
TIM1_BDTRInitStruct.TIM_DeadTime =(u8)((float)SpwmDeadTime*MasterTimeFreq); //先配置死区时间,在上锁
TIM1_BDTRInitStruct.TIM_LOCKLevel =TIM_LOCKLevel_1;//上锁
//使能自动输出
TIM1_BDTRInitStruct.TIM_AutomaticOutput=TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStruct); //
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); //定时器更新中断
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1,ENABLE); //使能定时器
SPWM.ContA=1; //
#elif (SpwmMode==__STM8__)
#endif
}
是要主程序当中等待么
void main()
{
delay(100);
然后使能外设时钟??
}
|