Skip to main content
Visitor II
December 21, 2021
Solved

STM8AL3146 Timer not producing calculated PWM frequency at output timer channel

  • December 21, 2021
  • 1 reply
  • 697 views

Hi all,

please suggest some solution for this,

I am trying to produce 14.7khz PWM signal with below timer1 configuration for 16Mhz clock frequency and i am getting 14.53Khz to 14.83Khz.

Why am i getting this frequency variation. Is ST timer can not produce exact output PWM frequency?

F_CK_PSC(prescaler frequency) is 16Mhz and Timer1 configuration are as below.

More than 1 timer channel have been used and all shows same frequency variation

  // enable peripheral clock

  CLK_PCKENR2_bit.PCKEN21 = 1;

  // PWM Mode 1 (output low while TIM1_CNT < TIM1_CCR1); Output-Compare preload enable; CC1 output

  TIM1_CCMR1 = 0x68;

  // PWM Mode 1 (output low while TIM1_CNT < TIM1_CCR2); Output-Compare preload enable; CC2 output

  TIM1_CCMR2 = 0x68;

  // PWM Mode 1 (output low while TIM1_CNT < TIM1_CCR3); Output-Compare preload enable; CC3 output

  TIM1_CCMR3 = 0x68;

  // Enable Timer 1 Ch 1+2 output

  TIM1_CCER1 = 0x11;

  // Enable Timer 1 Ch 3+4 output

  TIM1_CCER2 = 0x01;

  // Main output enable

  TIM1_BKR = 0x81;

  // Set auto-reload register and clock divider to get a base-frequency of 14.7 kHz

  // Calculated by: T_Interrupt = (ARR+1)/f_timer => ARR = 1087; PSCR = 0

  // Note: Maximum ARR , as CCR needs to be lower than ARR

  // Count 0x043F for 14.7KHz

  // Count 0x087F for 7.35KHz

  //TIM1_ARRH = 0x04; 

  //TIM1_ARRL = 0x3F;  

  // Prescalar is 0

  TIM1_PSCRH = 0x00;

  TIM1_PSCRL = 0x00;

  // Generate update request for setting new register values

  TIM1_EGR_bit.UG = 1;

  // TIM1_ARR unbuffered; Edge-aligned mode; up-counter direction;

  // Counter not stopping on update event; Counter enabled

  TIM1_CR1 = 0x01;

let me know if more information is needed.

    This topic has been closed for replies.
    Best answer by Peter BENSCH

    Welcome, @RGatk.1​, to the community!

    Of course, the timer can provide exact values. However, only if the input frequency of the timer is also exact.

    I guess you used the 16MHz HSI as the input frequency for the timer?

    However, the HSI uncalibrated has a deviation of 5%, calibrated it is still 1%. Assuming you have calibrated the HSI in your program, your generated frequency will be in the range of 14.553...14.847kHz, uncalibrated 13.965...15.435kHz, which fits your observation quite well.

    Regards

    /Peter

    1 reply

    Technical Moderator
    December 21, 2021

    Welcome, @RGatk.1​, to the community!

    Of course, the timer can provide exact values. However, only if the input frequency of the timer is also exact.

    I guess you used the 16MHz HSI as the input frequency for the timer?

    However, the HSI uncalibrated has a deviation of 5%, calibrated it is still 1%. Assuming you have calibrated the HSI in your program, your generated frequency will be in the range of 14.553...14.847kHz, uncalibrated 13.965...15.435kHz, which fits your observation quite well.

    Regards

    /Peter