0

I have an STM32L496 MCU, and I want to generate a 3MHz square wave. I would like to know what would be the accuracy of this signal.

The system clock frequency of this MCU is 80MHz. If I use a prescaler of 80MHz / 3MHz = 26.667 (can I do that?), then the timer will tick at a rate of 3MHz. If I use a 16-bit timer (TIMER16), it would count to 65 535 maximum, which means it would increment once every 0.33 microseconds.

That is how far I understood, but I am not sure how to calculate the accuracy of this signal. Any help would be much appreciated!

1 Answers1

0

If the core is 80MHz you can't make 3MHz exactly with a timer clocked from the same source as the core.

You can make 3.076923 MHz with even mark space ratio (prescaler 1, compare value 13 reset value 26), or you can make 2.962962 MHz (which is slightly closer) with a 13:14 mark space ratio (prescaler 1, compare value 13 or 14, reset value 27).

To get 3MHz you would have to underclock your core down to 78MHz.

I don't know the exact part you are using. You might be able to get it exactly using one of the clock outputs or a PLLs other than the one that drives the core, eg: if you have a 12MHz crystal you can output 3MHz easily on an MCO pin.

Tom V
  • 4,827
  • 2
  • 5
  • 22
  • Thank you a lot for your answer, and I apologize for the late reply. I only do not quite understand what are these compare values or mark space ratio of 13:14? – Gabriel Sep 15 '21 at 11:14
  • Is it something you can set in STM32CubeIDE? I tried to find information about that, but it was not a success. – Gabriel Sep 15 '21 at 11:15
  • The code generator in the IDE can only get you so far, eventually you have to write some software. The manual for you part is https://www.st.com/resource/en/reference_manual/rm0351-stm32l47xxx-stm32l48xxx-stm32l49xxx-and-stm32l4axxx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf You need to read section 32.5 especially 32.5.10. Mark-space ratio is a term that you can learn about with google. – Tom V Sep 15 '21 at 15:50