cancel
Showing results for 
Search instead for 
Did you mean: 

TIM3 Issue in STM32F215

thirukannan
Associate II
Posted on June 05, 2014 at 14:30

Hi,

I am using TIM3(PC6 to PC9) for PWM output in STM32F215.

In this I want three 50Hz PWM signals and one 8KHz signal. So I have configured PC7,PC8,PC9 as 50Hz PWM  signals. For this i have assigned

PrescalerValue_50 = (uint16_t) ((SystemCoreClock/2 ) / 33335) - 1;

And I have configured PC6 as 80KHz PWM signal.For this i have assigned

PrescalerValue_10 = (uint16_t) ((SystemCoreClock/2 ) / 5000000) - 1;

But i am getting 8KHz in all pins. What is problem i do'nt know. Please help me asap. Below is my code

void TIM_Config(void)

{

    GPIO_InitTypeDef GPIO_InitStructure;

    /* TIM3 clock enable */

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    /* GPIOC clock enable */

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

    /* GPIOC Configuration: TIM3 CH1 (PC6), TIM3 CH2 (PC7), TIM3 CH3 (PC8) and TIM3 CH4 (PC9) */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;

    GPIO_Init(GPIOC, &GPIO_InitStructure);

    /* Connect TIM3 pins to AF2 */  

    //GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);

    GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);

    GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM3);

    GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM3);

    /* Compute the prescaler value */

    PrescalerValue_50 = (uint16_t) ((SystemCoreClock/2 ) / 33335) - 1;

    /* Time base configuration */

    TIM_TimeBaseStructure.TIM_Period = 665;

    TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue_50;

    TIM_TimeBaseStructure.TIM_ClockDivision = 0;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

    /* PWM1 Mode configuration: Channel1 */

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

}

void TIM_Cong1(uint16_t Dutycycle)

{

    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;

    GPIO_Init(GPIOC, &GPIO_InitStructure);

    /* Connect TIM3 pins to AF2 */  

    GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);

    PrescalerValue_10 = (uint16_t) ((SystemCoreClock/2 ) / 5000000) - 1;

    /* Time base configuration */

    TIM_TimeBaseStructure.TIM_Period = 665;

    TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue_10;

    TIM_TimeBaseStructure.TIM_ClockDivision = 0;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

    /* PWM1 Mode configuration: Channel1 */

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

//    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = Dutycycle;

    TIM_OC1Init(TIM3, &TIM_OCInitStructure);

    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

    TIM_ARRPreloadConfig(TIM3, ENABLE);

    TIM_Cmd(TIM3, ENABLE);

    

}

void PWM_Generation1(uint16_t Dutycycle)

{

//    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = Dutycycle;

    TIM_OC4Init(TIM3, &TIM_OCInitStructure);

    TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

//    TIM_ARRPreloadConfig(TIM3, ENABLE);

    TIM_Cmd(TIM3, ENABLE);

}

void PWM_Generation2(uint16_t Dutycycle)

{

//    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = Dutycycle;

    TIM_OC3Init(TIM3, &TIM_OCInitStructure);

    TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);

//     TIM_ARRPreloadConfig(TIM3, ENABLE);

    TIM_Cmd(TIM3, ENABLE);

}

void PWM_Generation3(uint16_t Dutycycle)

{

//    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = Dutycycle;

    TIM_OC2Init(TIM3, &TIM_OCInitStructure);

    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

//    TIM_ARRPreloadConfig(TIM3, ENABLE);

    TIM_Cmd(TIM3, ENABLE);    

}

Regards,

Thirukkannan.
5 REPLIES 5
Posted on June 05, 2014 at 15:28

There is only one counter in each timer (and 4 capture-compare units, but they don't count), so you reprogram it to reload at a different rate when you call the other routine.

Review the timer chapter in the user manual, focus on the schematic at the beginning of the chapter.

JW

Posted on June 05, 2014 at 17:59

You're going about this all wrong.

Use TWO timers

For 50 Hz, set the Prescaler to divide to 1 MHz, ie (SystemCoreClock / 1000000) -1, and the Period to 20000-1 (20 ms 50 Hz), you can then set the Pulse width in 1us units for the 3 channels.

For 8 KHz, set the Prescaler to 0, and Period to (SystemCoreClock / 8000) -1

This would be for APB2 based timers, assuming CPU @ 120 MHz, APB2 at 60 MHz, for APB1 at 30 MHz, you'd need to use (SystemCoreClock / 2) for your TIMCLK

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
thirukannan
Associate II
Posted on June 26, 2014 at 11:56

Hi waclawek.jan,

Thanks for your valuable reply.

thirukannan
Associate II
Posted on June 26, 2014 at 12:01

Hi clive1,

Thanks for your quick and valuable reply.

And How your calculating the Prescaler Value and Period based on frequency range.

Posted on June 26, 2014 at 13:41

TIMEBASE = TIMCLK / ((PRESCALER+1) * (PERIOD+1))

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..