我想使用TIM3来控制伺服系统。我已经将TIM2 CH1CH2用于两个伺服系统,但我还需要四个。问题是我发现引脚没有PWM输出。 下面是我的代码,只要TIM2已经在工作,我就会感到困惑,怎么会出错呢。会有什么冲突吗?我已经检查了我的项目,并确信我还没有在任何其他地方使用过TIM3。
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_DeInit(TIM2);
TIM_DeInit(TIM3);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 500;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV2;
TIM_TimeBaseStructure.TIM_Prescaler = 7199; // 50 Hz
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel 1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //produce output when counter < CCR
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //set "high" to be effective output
TIM_OCInitStructure.TIM_Pulse = 0; // ccr_val
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM2, ENABLE);
TIM_Cmd(TIM3, ENABLE);
TIM_CtrlPWMOutputs(TIM2, ENABLE);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
答案 0 :(得分:2)
问题解决了。我使用:
打开所有RCC时钟RCC_APB1PeriphClockCmd(((uint32_t)0xFFFFFFFF,ENABLE);
RCC_APB2PeriphClockCmd(((uint32_t)0xFFFFFFFF),ENABLE);
然而,SPI(以及我尚未验证的其他一些外围设备)和PA6,7上的TIM3之间存在冲突。
正是冲突导致了这个问题。