cancel
Showing results for 
Search instead for 
Did you mean: 

STM8S PWM jitter

klaasdc
Associate II
Posted on October 16, 2010 at 18:09

STM8S PWM jitter

12 REPLIES 12
lowpowermcu
Associate II
Posted on May 17, 2011 at 15:11

Hi klass,

Here below a code that I suggested in my previous message. This could help. I didn't  compiled it but this is the idea.

  TIM1_DeInit();

  /* Set TIM1 Frequency to 10 kHz (=2Mhz/200), thus 1 unit = 0.1ms */

  TIM1_TimeBaseInit(200, TIM1_COUNTERMODE_UP, 2500, 0);

  /* TIM1 channel 1 is used (PC1) with rising edge and no divider nor filter */

  TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI,  TIM1_ICPSC_DIV1, 0x0);

  /* the trigger input (TRGI) to used to synchronize the counter is TI1FP1: Filtered timer input 1 (TI1FP1) */

  TIM1_SelectInputTrigger(TIM1_TS_TI1FP1);

  /* Reset mode - Rising edge of the selected trigger signal (TI1FP1 in this case) re-initializes the counter and

     generates an update of the registers. */

  TIM1_SelectSlaveMode(TIM1_SLAVEMODE_RESET);

  /* Overflow interrupt after 250ms = 2500*0.1ms; Capture1 interrupt enabled */

  TIM1_ITConfig(/*TIM1_IT_UPDATE | */TIM1_IT_CC1, ENABLE);

  /* generate an update event */

  TIM1_GenerateEvent(TIM1_EVENTSOURCE_UPDATE);

  /* Clear CC1 and update Flag */

  TIM1_ClearFlag(TIM1_FLAG_CC1 | TIM1_FLAG_UPDATE);

  /* Enable TIM1 */

  TIM1_Cmd(ENABLE);

  /* enable global interrupts */

  enableInterrupts();

 

    /* Set the code below in TIM1 capture interrupt */

    time = 10000/((double)TIM1_GetCapture1()); //by the way check that compiler is well handling this operation

    if (time > 180){

        time = 180;

    }

    pwm = ((u16)50*time+1400);

    TIM2_SetCompare1(pwm);

Herzlich,

MCU Lufter

klaasdc
Associate II
Posted on May 17, 2011 at 15:11

hi lowpowermcu,

Your solution worked! Thanks a lot!

I only added a filter to the input because my signal gets a bit noisy in the high rpm range.

There was a caveat though. I'm using the STM8S discovery board and I hadn't noticed that TIMER1 Channel1 and 3 is occupied by the Touch sensing button. So I spent some hours checking and checking before I found that I could not put decent signals on those channels... and then I saw it... Haha

If only I could buy you a beer now :)

Klaas

lowpowermcu
Associate II
Posted on May 17, 2011 at 15:11

Hi klaas,

Felicitation !!!

regards,

MCU Lüfter