How Arduino IDE Handles PWM Output at 15 Bit

Asked 2 years ago, Updated 2 years ago, 75 views

I'd like to use Arduino IDE to output PWM in Spresense, but the IDE defaults to 8Bit, so I can't change it.(except ArduinoDue)
However, Spresense has a resolution of 15 bits.

It's a little hard to handle that the resolution is limited to 256, so please let me know if there is a function that can output PWM at 32768 (15Bit).

Arduino Due writes like analogWriteResolution(bits).

environment:
Arduino 1.8.12
Board Ver1.5.1
Windows 10 Home

spresense

2022-09-30 17:52

2 Answers

Unfortunately, the PWM counter on the ATmega328 on the Arduino CPU is only 8 bits wide.
Therefore, it is impossible to have a resolution greater than 8 bits.


2022-09-30 17:52

You may be able to hit the nuttx driver directly from Arduino IDE.The PWM driver definitions for Spresense are as follows:

https://github.com/SPRESENSE/spresense-nuttx/blob/master/include/nuttx/drivers/pwm.h

The drivers for the pwm are described as follows:

structure pwm_info_s
{
  uint32_t frequency; /* Frequency of the pulse train*/

# ifdef CONFIG_PWM_MULTICHAN
                                /* Per-channel output state*/
  structure pwm_chan_s channels [CONFIG_PWM_NCHANNELS];

# else
  ub16_t duty; /* Duty of the pulse train, "1"-to-"0" duration.
                                 * Maximum: 65535/65536 (0x0000ffff)
                                 * Minimum: 1/65536 (0x00000001)*/
#  ifdef CONFIG_PWM_PULSECOUNT
  uint32_t count; /* The number of pulse to generate.0 means to
                                 * generate an infinite number of pulses*/
#  endif
#endif /*CONFIG_PWM_MULTICHAN*/
};

I think the following sample will help you with how to use it. (If you download the Spresense SDK, it will be merged into examples.)

https://github.com/jodersky/nuttx/blob/master/apps/examples/pwm/pwm_main.c

You can use the same driver as Linux.

pwm_devinit();

  /* Open the PWM device for reading*/
  fd = open(g_pwmstate.devpath, O_RDONLY);

  /* Configure the characteristics of the pulse train*/
  info.frequency=frequency;
  info.duty = duty;

  ioctl(fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)(uintptr_t)&info));

  /* Then start the pulse train. Since the driver was opened in blocking
   * mode, this call will block if the count value is greater than zero.*/
  ioctl(fd, PWMIOC_START, 0);

That's how it feels.How to call NuttX drivers from Arduino IDE has been answered in the past stack overflow, so please refer to it.

Can I use the SPRESENSE SDK with Arduino IDE?

It may be a half-hearted answer, but I hope you'll find it helpful!


2022-09-30 17:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.