Skip to main content
Explorer
November 15, 2024
Solved

PWM generation at very low Duty-cycle

  • November 15, 2024
  • 3 replies
  • 1376 views

Hello , 

I am using the STM32G0C1RET6 MCU and want to generate the frequency in between 20Khz to 200Khz, with dutycyle in between 1 to 100%, but frequency below 10 percent is not generating, as my CCR value gets 0, due to low ARR value. Am I going in right direction or not I am not getting it, and if have understood the correct use of CCR and ARR registers.

here is a snippet of my code I am using,

for setting the frequency I am using this logic

case TIMER1:

if (freq_input > 100000)

TIM1->PSC = 32-1;

else

TIM1->PSC = 64-1;

new_arr = (HAL_RCC_GetHCLKFreq() / (freq_input * TIM1->PSC)) - 1;

TIM1->ARR=new_arr;

for setting Dutycycle i am using,

uint32_t CCR = ((duty_input) * new_arr) /100 ;

switch(timer) {

case TIMER1:

if (channel == 1)

TIM1->CCR1=CCR;

here, HAL_RCC_GetHCLKFreq = 64MHz.

here using the less or higher prescalar affects the frequency, so i chose accordingly.

    This topic has been closed for replies.
    Best answer by MM..1

    64000000 / 200000 = 320 

    Then for 200kHz divider for 64M must be 1 and PSC=0 . What you dont understand?

    ARR = 319

    And CCR 0 .. 319 duty step around 0,3%

     

    64000000 / 20000 = 3200   ARR 3199   PSC 0  ...

    3 replies

    Technical Moderator
    November 15, 2024

    Hello @Rohit007 ,

    Please make sure you post your thread in the correct forum board to increase your chance to get an answer and please use </> button to share your code.

    Thank you for your understanding.

    Super User
    November 15, 2024

    Use PSC=0.

    JW

    Graduate II
    November 15, 2024

    Your choiced PSC result to max ARR and max ARR result to duty resolution.

    Better is start with PSC 1 (-1) as JW and calc ARR, if ARR is over for 16 bit timer 65535 then increase PSC ...

    Rohit007Author
    Explorer
    November 15, 2024

    @MM..1 I have tried using these different PSC I got better Frequency output and dutycycle PSC = 32 and PSC = 64, but I didn't got PWM for less than 10%. Any other solution.

    MM..1Answer
    Graduate II
    November 15, 2024

    64000000 / 200000 = 320 

    Then for 200kHz divider for 64M must be 1 and PSC=0 . What you dont understand?

    ARR = 319

    And CCR 0 .. 319 duty step around 0,3%

     

    64000000 / 20000 = 3200   ARR 3199   PSC 0  ...