AnsweredAssumed Answered

a problem with set ClockDivision

Question asked by hossein hosseini on Mar 28, 2018
Latest reply on Mar 29, 2018 by Clive One

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

Outcomes