简体   繁体   中英

STM32F103RC TIM3 not working

I want to use TIM3 for controlling servos. 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. 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? I've checked my project and am sure that I haven't used TIM3 any where else.

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_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.
It is the conflict that caused the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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