cancel
Showing results for 
Search instead for 
Did you mean: 

STM8AL3146 Timer not producing calculated PWM frequency at output timer channel

RGatk.1
Associate

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Peter BENSCH
ST Employee

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

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

1 REPLY 1
Peter BENSCH
ST Employee

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

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.