Skip to main content
Karan 123
Senior
January 16, 2021
Solved

STM32G0 Output Compare formula (NUCLEO-G071RB)

  • January 16, 2021
  • 5 replies
  • 3513 views

Hi,

I have NUCLEO-G071RB board . How to calculate 1KHz frequency with STM32G071 Output compare ?

My System Clock frequency is 64 MHz .

0693W000007BXkMQAW.png 

0693W000007BXkWQAW.png 

--

Karan

This topic has been closed for replies.
Best answer by waclawek.jan
 // GPIOx_MODER - 2 bits per pin
#define GPIO_Mode_In 0x00 // GPIO Input Mode
#define GPIO_Mode_Out 0x01 // GPIO Output Mode
#define GPIO_Mode_AlternateFunction 0x02 // GPIO Alternate function Mode
#define GPIO_Mode_AF GPIO_Mode_AlternateFunction
#define GPIO_Mode_Analog 0x03 // GPIO Analog Mode
 
GPIOB->MODER = (GPIOB->MODER & ~(0 
 | GPIO_MODER_MODE6
)) | (0 
 | (GPIO_Mode_Out * GPIO_MODER_MODE6_0)
);

5 replies

TDK
January 16, 2021

timer frequency = (timer clock) / (PSC + 1) / (ARR + 1)

PSC is "Prescaler", ARR is "Period"

So for a 64 MHz timer:

prescaler=63

period=999

And for 50% duty cycle, set pulse = (ARR + 1) / 2 = 500.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Karan 123
Karan 123Author
Senior
January 17, 2021

Thanks... But...

These calculation gives 500Hz instead of 1KHz on Digital Storage Oscilloscope.

F = 64/(63 + 1) /999 + 1 ;

F = 1/1000 ;

T = 1000

0693W000007BYD4QAO.png0693W000007BYD9QAO.png 

While below calculation gives 1KHz.

prescaler = 63 ;

period = 499 ;

pulse = 499 ;

Why so ?

Michal Dudka
Lead
January 17, 2021

It is simle. In one timer period comes one Compare event. If you toggle output on compare event you have one toggle per (timer) period. To generate square wave you need two toggles (rising edge and falling edge). Whole period of your waveform takes two periods of timer. TDK suggest you both situations that you are describing (probably because your question was unclear - asking for 1kHz square wave or 1kHz Copmare events).

TDK
January 17, 2021

> Why so ?

You have toggle on match mode. Set it to PWM mode, or double the frequency.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Karan 123
Karan 123Author
Senior
January 18, 2021

Thanks for update.

If I have 1KHz (1ms) square wave output with output compare.

Will it possible to switched off Output Compare module

in between ​say 356 us and it should low untill next start of output compare.

---​

Karan

Karan 123
Karan 123Author
Senior
January 19, 2021

0693W000007Bk0bQAC.pngWill it possible to switched off Output Compare module like above?

Michal Dudka
Lead
January 19, 2021

It is possible. You can use PWM mode (instead toggle) and set duty cycle to 50% (generating square wave). When you need you can set duty cycle to another value (say 356us) and after single period set it back to 500us (50%). If you have to "shut down" output by external signal, you can use break function (if your ouput is from timer with that feature). You can manually force output to low (search in timer section of reference manual "Forced output mode") whenever you need. And probably there will be another mechanisms.

Karan 123
Karan 123Author
Senior
January 19, 2021

I don't want to set duty cycle at fixed value ( Say 356us)

I want to switch off either PWM or Output Compare randomly in between.

Karan

waclawek.jan
Super User
January 19, 2021

> Will it possible to switched off Output Compare module like above?

Yes, in several ways.

Maybe the most straightforward is to change given pin seeing in GPIO_MODER from AF to OUT.

Our use the Forced Low Output Compare mode in TIMx_CCMRx.

Read the TIM chapter in RM.

JW

Karan 123
Karan 123Author
Senior
January 19, 2021

"Maybe the . . . ."​

You mean to say change GPIO from Alternate Function to Output or Input what?

Is there any method with HAL library function.?

--

​Karan

waclawek.jan
Super User
January 19, 2021

> You mean to say change GPIO from Alternate Function to Output or Input what?

You probably want the pin to output a defined level all the time, so from Alternate Function to Output.

> Is there any method with HAL library function.?

I don't know, I don't use Cube/HAL.

JW

Karan 123
Karan 123Author
Senior
January 19, 2021

How to do that without HAL or at register level.?

-​

Karan​

waclawek.jan
waclawek.janBest answer
Super User
January 19, 2021
 // GPIOx_MODER - 2 bits per pin
#define GPIO_Mode_In 0x00 // GPIO Input Mode
#define GPIO_Mode_Out 0x01 // GPIO Output Mode
#define GPIO_Mode_AlternateFunction 0x02 // GPIO Alternate function Mode
#define GPIO_Mode_AF GPIO_Mode_AlternateFunction
#define GPIO_Mode_Analog 0x03 // GPIO Analog Mode
 
GPIOB->MODER = (GPIOB->MODER & ~(0 
 | GPIO_MODER_MODE6
)) | (0 
 | (GPIO_Mode_Out * GPIO_MODER_MODE6_0)
);