How to Change the Frequency of pwm in the Spresense Arduino Library

Asked 2 years ago, Updated 2 years ago, 95 views

The analogWrite() in the SpresenseArduinoLibrary shows PWM output at a period of 490 Hz. How do I operate at 6.5 MHz?
https://developer.sony.com/ja/develop/spresense/developer-tools/hardware-documentation#_pwm%E3%81%AE%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95

spresense

2022-09-30 19:12

1 Answers

I am in charge of SPRESENSE support for Sony.

I would like to answer two questions: how to change the PWM frequency and how to output a 6.5 MHz PWM.

In version 1.1.1, an API has been added to change the PWM frequency.
Update the library using the Arduino IDE Board Manager.

You have added two APIs: analogWriteFreq and analogWriteSetDefaultFreq.

You can change the PWM frequency by using analogWriteFreq instead of analogWrite, or by using analogWriteSetDefaultFreq before using analogWrite.

Use Cases

when using analogWriteFreq
Use PWM with a Duty ratio of 100/255* at a frequency of 2000 Hz for PIN_PWM_0

analogWriteFreq(PIN_PWM_0,100,2000);

when using analogWriteSetDefaultFreq
Use PWM with a Duty ratio of 100/255* at a frequency of 2000 Hz for PIN_PWM_0

analogWriteSetDefaultFreq(2000);
analogWrite(PIN_PWM_0,100);

(*) The Duty ratio is 255.If 255 is set, the Duty ratio is 100%.

The maximum PWM frequency for the Spresense Arduino Library is 4.1 MHz.
The maximum frequency can only be set to a Duty ratio of 50%.

6.5 MHz clock output can be achieved by changing the configuration of the Spresense SDK.
Please open the Spresense SDK configuration settings and

CXD56xx Configuration
  ->Sensor Control Unit (SCU)
   ->SCU clock mode (RCOSC)

In , change "RCOSC" to "XOSC".

6.5 MHz is the maximum frequency, so you can only set the Duty ratio to 50%.

Thank you for your cooperation.


2022-09-30 19:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.