简体   繁体   English

STM32F103RC TIM3不起作用

[英]STM32F103RC TIM3 not working

I want to use TIM3 for controlling servos. 我想使用TIM3来控制伺服器。 I've already used TIM2 CH1CH2 for two servos but I need four more.The problem is that I found no PWM output in the pin. 我已经将TIM2 CH1CH2用于两个伺服器,但是我还需要四个伺服器,问题是我发现该引脚上没有PWM输出。 Below is my code and I'm rather confusing about how can it be wrong as long as TIM2 is already working.Will there be any conflict? 下面是我的代码,只要TIM2已经正常工作,我就对它怎么会出错感到困惑,会不会有任何冲突? I've checked my project and am sure that I haven't used TIM3 any where else. 我已经检查了我的项目,并确保没有在其他任何地方使用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);

Problem solved. 问题解决了。 I opened all the RCC Clock using: 我使用以下命令打开了所有RCC时钟:

RCC_APB1PeriphClockCmd(((uint32_t)0xFFFFFFFF,ENABLE);
RCC_APB2PeriphClockCmd(((uint32_t)0xFFFFFFFF),ENABLE);

However there is a conflict between SPI (and some other Peripherals that I haven't verified) and TIM3 at PA6,7. 但是,SPI(和我尚未验证的其他一些外设)与PA6,7上的TIM3之间存在冲突。
It is the conflict that caused the problem. 导致问题的是冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM