cancel
Showing results for 
Search instead for 
Did you mean: 

TIM1 jumping count in the encoder mode

Kolab
Senior

I'm using a TTC encoder connected to stm8s003f . TIM1 counts correctly until it reaches the value before the overflow value . After that it either jumps to 0 or it shows the overflow value. What is that due to?Thank you.

3 REPLIES 3

What's the maximal value of the TIM configured too? Show code.

Is the overflow just an alert, or does it have secondary consequences?​

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

void Filter_Value(uint8_t value);

void clock_setup(void);

void GPIO_setup(void);

unsigned int present_value = 0x0000;

unsigned int previous_value = 0x0000;

void TIM1_setup(void);

void main(void)

{

  clock_setup();

  GPIO_setup();

  TIM1_setup();

  /* Infinite loop */

   while(1){

    present_value = TIM1_GetCounter()/2;

    if(present_value != previous_value)

       GPIO_WriteReverse(GPIOA, GPIO_PIN_1);

    previous_value = present_value;

  }   

}

void TIM1_setup(void){

  TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, 10, 0);

  TIM1_EncoderInterfaceConfig(TIM1_ENCODERMODE_TI12, TIM1_ICPOLARITY_RISING, TIM1_ICPOLARITY_RISING);

  enableInterrupts();

  Filter_Value(0);

  TIM1_Cmd(ENABLE);

}

void Filter_Value(uint8_t value){

 TIM1->CCMR1|=(uint8_t)(value<<4);

 TIM1->CCMR2|=(uint8_t)(value<<4); 

}

void clock_setup(void)

{

    CLK_DeInit();

    CLK_HSECmd(DISABLE);

    CLK_LSICmd(DISABLE);

    CLK_HSICmd(ENABLE);

    while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == 0);

    CLK_ClockSwitchCmd(ENABLE);

    CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);                     

    CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);

    CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE,

    CLK_CURRENTCLOCKSTATE_ENABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, ENABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);

}

void GPIO_setup(void)

{

    GPIO_DeInit(GPIOC);

    GPIO_Init(GPIOC,

         ((GPIO_Pin_TypeDef)(GPIO_PIN_7 | GPIO_PIN_6)),

         GPIO_MODE_IN_PU_NO_IT); 

    GPIO_DeInit(GPIOA);

    GPIO_Init(GPIOA, GPIO_PIN_1, GPIO_MODE_OUT_PP_HIGH_SLOW);

    

}

AA1
Senior III

If you read RM0016 page 185, it seems what you said, is the correct behavior.

The counter counts continuously between 0 and the auto-reload value in the TIM1_ARR register (0 to ARR or ARR down to 0 depending on the direction).