cancel
Showing results for 
Search instead for 
Did you mean: 

Timer One Pulse Mode issues

toddberk
Associate II
Posted on November 10, 2010 at 09:58

Timer One Pulse Mode issues

11 REPLIES 11
Posted on May 17, 2011 at 14:14

SYSClock = 72Mhz

AHB prescalar = 1

APB1 prescalar = 2

PCLK1 = 36 Mhz

Look at the clock tree again, I'm pretty sure that the timer clock will still be 72 MHz

I picked values to get the 20 ms exact. But yes you just reduce the scale to give you finer granularity in the width control, while keeping the numbers below 65536. You should be able to get the counter to generate an interrupt, and modulate the width there.

Fix to my code

printf(''TIM4 rate %d Hz, width %d us\n'',TIM4_Frequency, TIM4_Width);

You could use floats to provide decimals places.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lowpowermcu
Associate II
Posted on May 17, 2011 at 14:14

Hi,

I have tailored John F. suggestion.

In TIM1 I have added TIM_SelectOutputTrigger() and a new function Timer2_Configuration()

It needs GPIO configuration for timer 2 pins (PA0....)

I hope that this helps.

#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();

  Timer2_Configuration();

 

  //TIM1 enable counter

  TIM_Cmd(TIM1, ENABLE);

  //TIM2 enable counter

  TIM_Cmd(TIM2, ENABLE);

 

  while(1)

  {

    ;

  }

}

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);

}

/* TIM1 must be configured in such way to generate update event every 20 ms */

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);

  /* Select update event as trigger output ''TRGO'' */

  TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update)

 

  TIM_CtrlPWMOutputs(TIM1, ENABLE);

}

void Timer2_Configuration(void)

{ TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  TIM_OCInitTypeDef  TIM_OCInitStructure;

 

 

  /* enable TIM2 APB clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* Add GPIO configuration for TIM2 channels: PA0... */

  /***************************************/

  /***************************************/

  /***************************************/

 

  TIM_TimeBaseStructure.TIM_Period = 100;

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

  TIM_TimeBaseInit(TIM2, &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 = 10;

  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;

  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  TIM_OC2Init(TIM2, &TIM_OCInitStructure);

   

  /* Select one pulse mode */

  TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);

  /* connect TIM1 TRGO (update event in our case) to TIM2 trigger */

  TIM_SelectInputTrigger(TIM2, TIM_TS_ITR0);/* refer to TIMx Internal trigger connection in RM008 */

  /* select slave mode */

  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Trigger);

}