cancel
Showing results for 
Search instead for 
Did you mean: 

PWM generation at very low Duty-cycle

Rohit007
Associate III

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.

5 REPLIES 5
SofLit
ST Employee

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.

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.

Use PSC=0.

JW

MM..1
Chief II

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 ...

@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.

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  ...