This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 0a7c308f1 examples/pwm: Fix PWM range
0a7c308f1 is described below

commit 0a7c308f1b609a5a41f0e8246a528f66f5deb284
Author: Lucas Saavedra Vaz <[email protected]>
AuthorDate: Thu Jul 13 11:18:44 2023 -0300

    examples/pwm: Fix PWM range
    
    This commit fixes the overflow that happens when setting the duty cycle to 
100% in the PWM example.
    It now correctly passes the value 0xffff rather than 0x0000 when setting 
the duty cycle to 100 and makes sure that no underflow happens when setting the 
duty cycle to 0.
---
 examples/pwm/pwm_main.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/examples/pwm/pwm_main.c b/examples/pwm/pwm_main.c
index 497d58abb..c5d4424a8 100644
--- a/examples/pwm/pwm_main.c
+++ b/examples/pwm/pwm_main.c
@@ -523,7 +523,8 @@ int main(int argc, FAR char *argv[])
   for (i = 0; i < CONFIG_PWM_NCHANNELS; i++)
     {
       info.channels[i].channel = g_pwmstate.channels[i];
-      info.channels[i].duty = b16divi(uitoub16(g_pwmstate.duties[i]), 100);
+      info.channels[i].duty = g_pwmstate.duties[i] ? \
+        b16divi(uitoub16(g_pwmstate.duties[i]) - 1, 100) : 0;
       printf(" channel: %d duty: %08" PRIx32,
         info.channels[i].channel, info.channels[i].duty);
     }
@@ -531,7 +532,8 @@ int main(int argc, FAR char *argv[])
   printf("\n");
 
 #else
-  info.duty  = b16divi(uitoub16(g_pwmstate.duty), 100);
+  info.duty = g_pwmstate.duty ? \
+    b16divi(uitoub16(g_pwmstate.duty) - 1, 100) : 0;
 #  ifdef CONFIG_PWM_PULSECOUNT
   info.count = g_pwmstate.count;
 

Reply via email to