Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

How to control duty cycle precision? #3

Answered by khoih-prog
pixpop asked this question in Q&A
Discussion options

You must be logged in to vote

Currently, the Arduino rp2040-related mbed cores (mbed_nano, mbed_rp2040) don't provide a way to specify the resolution, and I believe, as common sense, they will use the best 16-bit resolution.

The DC is using float to provide the better precision.

You can reduce, IMO not what you'd like, the precision by using fake mapping such as analogWrite() by

void analogWrite(PinName pin, int val)
{
  pin_size_t idx = PinNameToIndex(pin);
  if (idx != NOT_A_PIN) {
    analogWrite(idx, val);
  } else {
    mbed::PwmOut* pwm = new mbed::PwmOut(pin);
    pwm->period_ms(2); //500Hz
    float percent = (float)val/(float)((1 << write_resolution)-1);
    pwm->write(percent);
  }
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@pixpop
Comment options

Answer selected by pixpop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants