Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK_PWM_PERIOD_COMPAT_MODE logic incorrect #24

Open
zym060050 opened this issue Apr 2, 2018 · 2 comments
Open

SDK_PWM_PERIOD_COMPAT_MODE logic incorrect #24

zym060050 opened this issue Apr 2, 2018 · 2 comments

Comments

@zym060050
Copy link

Hi,

The logic for the SDK_PWM_PERIOD_COMPAT_MODE appears to be incorrect?

#if SDK_PWM_PERIOD_COMPAT_MODE
#define PWM_PERIOD_TO_TICKS(x) (x * 0.2)
#define PWM_DUTY_TO_TICKS(x) (x * 5)
#define PWM_MAX_DUTY (PWM_MAX_TICKS * 0.2)
#define PWM_MAX_PERIOD (PWM_MAX_TICKS * 5)
#else
...
#endif

In README:

By default there is one small difference to the SDK. The code uses a unit of 200ns for both period and duty. E.g. for 10% duty cycle at 1kHz you need to specify a period value of 5000 and a duty cycle value of 500, a duty cycle of 5000 or above switches the channel to full on.

To have full compatibility with the SDK, you have to set the SDK_PWM_PERIOD_COMPAT_MODE define to 1. If set, the code will use 1us for PWM period and 40ns for the duty cycle. E.g. 10% duty cycle at 1kHz is set by a period value of 1000 and a duty cycle value of 2500, full duty at 25000 and above.

Let's say for 1KHZ, if SDK_PWM_PERIOD_COMPAT_MODE is defined to be 1. Then period should multiply by 5 (1000*5=5000) not divided by 5.

The correct logic should be:

#if SDK_PWM_PERIOD_COMPAT_MODE
#define PWM_PERIOD_TO_TICKS(x) (x * 5)
#define PWM_DUTY_TO_TICKS(x) (x * 0.2)
#define PWM_MAX_DUTY (PWM_MAX_TICKS * 5)
#define PWM_MAX_PERIOD (PWM_MAX_TICKS * 0.2)
#else
...
#endif

Thanks.

@ge0rg
Copy link

ge0rg commented May 13, 2018

I just came here to say the same thing. From my experiments a long time ago, I found out that the maximum duty at a period of 1000 on my H801 module is actually around 50505 (don't ask me why or how I found out, I think I re-calculated it from one of the Espressif SDK examples).

But even with 25000 it doesn't work without the change by @zym060050.

perpernorbi referenced this issue in perpernorbi/ESP8266_new_pwm Aug 23, 2018
The conversion was just backwards. In COMPAT_MODE, period value of 1000
corresponds to 1kHz, while, without COMPAT_MODE, 5000 corresponds to
the same period. So when SDK_PWM_PERIOD_COMPAT_MODE is set, period
should be multiplied by 5. For the same reason, duty needs to be
divided by 5.
@perpernorbi
Copy link

I have created a pull request (#26), which will fix this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants