本帖最后由 Kayiji3039 于 2017-8-26 00:35 编辑
遇到一个非常玄学的问题。。
截图是我写的PWM代码中子函数的部分,如果我将79和80行对调可以实现预期功能,如果写成截图中的顺序则无任何预期现象,有人知道这是为什么吗?这么写有违背什么C语言的语法规则吗?如果有的话,为什么MDK都能进行正常编译?
该子函数完整代码如下:
[mw_shl_code=c,true]void xTIM1_initPwn(uint16_t gateVal,uint16_t pwmMod)
{
GPIO_InitTypeDef g;
TIM_OCInitTypeDef to;
TIM_TimeBaseInitTypeDef t;
/* --- reset GPIO parameter --- */
g.GPIO_Pin = GPIO_Pin_8;
g.GPIO_Mode = GPIO_Mode_AF_PP;
g.GPIO_Speed = GPIO_Speed_50MHz; /* max speed:50MHz */
GPIO_Init(GPIOA,&g);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
//TODO
t.TIM_Period = 899;
t.TIM_Prescaler = 0;
t.TIM_ClockDivision = 0;
t.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &t);
/* --- set parameter of PWM output --- */
//to.TIM_OCIdleState
to.TIM_OCMode = pwmMod; /* recommend to fill with TIM_OCMode_PWM2 */
//to.TIM_OCNIdleState
//to.TIM_OCNPolarity
to.TIM_OCPolarity = TIM_OCPolarity_High;
//to.TIM_OutputNState
to.TIM_OutputState = TIM_OutputState_Enable;
to.TIM_Pulse = 0; /* set compare value(point to CCR register) */
TIM_OC1Init(TIM1,&to); /* using channel-1(this channel is connected to LED0) */
/* --- enable preload action --- */
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
/* --- launch --- */
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE);
}[/mw_shl_code]
一周热门 更多>