Skip to main content
hossein hosseini
Associate III
March 28, 2018
Question

a problem with set ClockDivision

  • March 28, 2018
  • 3 replies
  • 1019 views
Posted on March 28, 2018 at 11:44

hi dears .

i am trying to use toggle mode in stm32f103re with IAR and cmsis.

When do i set  TIM_ClockDivision  in my code , the duty  is 50% , but i know i set the Compare4 to other value in time base init , and it does not be 50%.

And when i put this line to comment, the code do correctly.it does not be 50% duty cycle.i see that with oscope.

i dont know why this activity occer.

the register in default mode has 0x0000 value , and when i comment the line of set clockDivision , it has zero value,and if i  uncommented this line and set to zero , the code do the activity wrong.why??

i mean this line:

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

this is my code with the uncommented line that there is above:

thanks alot.

#include 'stm32f10x.h'

#include 'stm32f10x_gpio.h'  // for Enable LCD

#include 'stm32f10x_rcc.h'   //for Enable LCD

#include 'stm32f10x_tim.h'

#include 'LCDLib.h'

#include 'delay.h'

void tim_init(void);

void GPIO_init(void);

int i = 0;

int main()

{

 

  lcd_init();

  lcdPutString ('Believe Your Dreams.');  

 

  GPIO_init();

  tim_init();

 

 

  while(1){

   for(i = 0 ; i<=200 ; i=50)

   {

    TIM_SetCompare4(TIM3, i);

    lcdGoxy(1 , 4);

    lcdPutInt(TIM_GetCounter(TIM3));

    delay_ms(80);

   }

//   for(i = 500 ; i>=0 ; i--)

//   {

//    TIM_SetCompare4(TIM3, i);

//    delay_ms(8);

//   }

    

  }  

 

}

void tim_init(){    

 

 

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

 

  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  TIM_OCInitTypeDef  TIM_OCInitStructure;  

 

  TIM_TimeBaseStructure.TIM_Prescaler = 30;

  TIM_TimeBaseStructure.TIM_Period = 200;  

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

 

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

 

 

    

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle   ;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 0;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

 

  TIM_OC4Init(TIM3, &TIM_OCInitStructure);

  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

 

  TIM_Cmd(TIM3, ENABLE);

   

}

void GPIO_init(void){

 

 

  RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOC| RCC_APB2Periph_AFIO, ENABLE );  

 

  GPIO_InitTypeDef gpioAa;     

  gpioAa.GPIO_Mode = GPIO_Mode_AF_PP ;

  gpioAa.GPIO_Pin = GPIO_Pin_9;

  gpioAa.GPIO_Speed = GPIO_Speed_50MHz;  

  GPIO_Init( GPIOC,  &gpioAa );

  GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);

 

}

    This topic has been closed for replies.

    3 replies

    Tesla DeLorean
    Guru
    March 28, 2018
    Posted on March 28, 2018 at 13:40

    Auto/local variables/structures have random values, downstream code may malfunction if all fields have not been initialized.

    Use the form

      TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure = {0};

      TIM_OCInitTypeDef  TIM_OCInitStructure = {0};  
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    hossein hosseini
    Associate III
    March 29, 2018
    Posted on March 29, 2018 at 20:36

    yeah , got it.

    but when is do that, i think the toggle mode dose not work.

    i mean , when i set a value to CCR4 register :

      TIM_OCInitStructure.TIM_Pulse = 30;

    however i have 50% duty cycle in a period.

    the period value is 200 and i know that  is wrong in toggle mode,but when i comment the line TIM_ClockDivision , the toggle mode work correct.

    i think when the timer value = pulse  then toggle happent.

    but here toggle happent at half of period always.

    0690X00000604WPQAY.jpg

    when TIM_ClockDivision compile with other line.

    0690X0000060AJSQA2.png

    when TIM_ClockDivision not  compile with other line.and in my idea this activity is true in output compare mode toggle.

    thank you.

    Tesla DeLorean
    Guru
    March 29, 2018
    Posted on March 29, 2018 at 21:41

    Yes, does sound a bit odd, not really something I'm looking to replicate/validate on an F1

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