cancel
Showing results for 
Search instead for 
Did you mean: 

TIM4 PWM Out

Were-Rabbit
Associate II
Posted on July 19, 2010 at 13:25

TIM4 PWM Out

4 REPLIES 4
John F.
Senior
Posted on May 17, 2011 at 13:59

This worked for Timer1. Do you need to use function, TIM_CtrlPWMOutputs(TIMx, ENABLE) ?

John F.

#include ''stm32f10x.h''

void RCC_Configuration(void);

void GPIO_Configuration(void);

void Timer1_Configuration(void);

//note with SYSCLK_FREQ_72MHz defined, PCLK1 = 36MHz and PCLK2 = 72MHz

uint16_t CCR1_Val = 1000; //1500 = 1.5ms (1000 = 1ms, 2000 = 2ms);

int main(void)

{

  RCC_Configuration();

  GPIO_Configuration();

  Timer1_Configuration();

  while(1)

  {

    GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_SET);

    GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_RESET);

  }

}

void RCC_Configuration(void)

{

  //Setup the microcontroller system. Initialize the Embedded Flash Interface, 

  //initialize the PLL and update the SystemFrequency variable.

  SystemInit();

 

  // Enable GPIOB, AFIO and Timer1 clocks

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_TIM1, ENABLE);

}

void GPIO_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  //partial remap of Timer1

  GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1, ENABLE);

  //GPIOB Configuration: TIM1 channel 2N as alternate function push-pull

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

}

void Timer1_Configuration(void)

{

  //Timer1 is clocked by PCLK2 = 72MHz

  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  TIM_OCInitTypeDef  TIM_OCInitStructure;

  TIM_DeInit(TIM1);

  //Time base configuration

  TIM_TimeBaseStructure.TIM_Prescaler = 71; //clocked at 72MHz / 72 = 1MHz

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseStructure.TIM_Period = 19999; //period = (72MHz / 72) / 20000 = 50Hz (20ms period)

  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  /* Channel 1 Configuration in PWM mode */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;

  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;

  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;

  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

 

  TIM_OC2Init(TIM1, &TIM_OCInitStructure);

  TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);

  TIM_CtrlPWMOutputs(TIM1, ENABLE);

  //TIM1 enable counter

  TIM_Cmd(TIM1, ENABLE);

}

swhite2
Associate III
Posted on May 17, 2011 at 13:59

You only need that for Timers 1 and 8 (if present). You do, however, need to enable the GPIO port, alternate function and timer clocks though.

Were-Rabbit
Associate II
Posted on May 17, 2011 at 13:59

Thank You, PWM1 is working

cris
Associate
Posted on May 17, 2011 at 13:59

Hello Mr. John F.

Im using the same code to generate a PWM that you posted here but i can`t get any signal.

I want to use it to control a RGB LED so i changed the GPIO configuration but i cant get any signal...

is there anything else i need to do it?

Thank you