2024-11-15 04:37 AM - last edited on 2024-11-15 04:42 AM by SofLit
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.
2024-11-15 04:44 AM
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.
2024-11-15 05:02 AM
Use PSC=0.
JW
2024-11-15 05:23 AM - edited 2024-11-15 05:24 AM
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 ...
2024-11-15 10:06 AM
@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.
2024-11-15 10:15 AM
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 ...