cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G0 Output Compare formula (NUCLEO-G071RB)

Karan 123
Senior

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

1 ACCEPTED SOLUTION

Accepted Solutions
  // 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)
);

View solution in original post

20 REPLIES 20
TDK
Guru

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

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 ?

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

Thanks Sorry ...

​But I​ am asking for 1KHz Square wave.

--

Karan​

Ok. And you have (period 499, prescaler 63, pulse can be any value from 0 to 499).

Thanks for clarification ..

But what is role of Pulse "Register" in any case.?

Karan​

It has many roles depending on output mode. For example can store duty cycle value in PWM modes, pulse width in one-pulse mode, phase shift if toggling on multiple outputs etc. Read STM32G071 Reference manual ...

TDK
Guru

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

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