Improved accuracy
authorKevin Townsend <kevin@ktownsend.com>
Wed, 9 May 2012 21:39:24 +0000 (23:39 +0200)
committerKevin Townsend <kevin@ktownsend.com>
Wed, 9 May 2012 21:39:24 +0000 (23:39 +0200)
core/pwm/pwm.c
core/pwm/pwm_100us_50percent.png [deleted file]
core/pwm/pwmtest_100ticks_50percent.png [new file with mode: 0644]

index b94329f..7f1e2e5 100644 (file)
@@ -178,7 +178,7 @@ int pwmSetDutyCycle(uint32_t percentage)
   }
 
   /* Set Duty Cycle (MR0) */
-  TMR_TMR16B1MR0 = (pwmPulseWidth * (100 - (pwmDutyCycle = percentage))) / 100;
+  TMR_TMR16B1MR0 = ((pwmPulseWidth * (100 - (pwmDutyCycle = percentage))) / 100) + 1;
 
   return 0;
 }
@@ -202,7 +202,7 @@ int pwmSetFrequencyInTicks(uint16_t ticks)
   }
 
   /* Set Pulse Width (MR3)*/
-  TMR_TMR16B1MR3 = (pwmPulseWidth = ticks);
+  TMR_TMR16B1MR3 = (pwmPulseWidth = ticks - 1);
 
   /* Adjust Duty Cycle (MR0) */
   TMR_TMR16B1MR0 = (pwmPulseWidth * (100 - pwmDutyCycle)) / 100;
@@ -224,8 +224,8 @@ int pwmSetFrequencyInTicks(uint16_t ticks)
     @Warning    Because a 16-bit timer is used here by default, the
                 maximum frequency is quite small.  Running at 36MHz, the
                 largest possible pulse-width/frequency is ~1,82mS or
-                1820 microSeconds.  At 12MHz its 5461 uS, and at 48MHz
-                its 1365 uS.
+                1820 microSeconds.  At 12MHz its 5461 uS, at 48MHz
+                its 1365 uS, at 72MHz its 909uS.
 */
 /**************************************************************************/
 int pwmSetFrequencyInMicroseconds(uint16_t us)
@@ -243,10 +243,10 @@ int pwmSetFrequencyInMicroseconds(uint16_t us)
   }
 
   /* Set Pulse Width (MR3)*/
-  TMR_TMR16B1MR3 = (pwmPulseWidth = ticks);
+  TMR_TMR16B1MR3 = (pwmPulseWidth = ticks - 1);
 
   /* Adjust Duty Cycle (MR0) */
-  TMR_TMR16B1MR0 = (pwmPulseWidth * (100 - pwmDutyCycle)) / 100;
+  TMR_TMR16B1MR0 = ((pwmPulseWidth * (100 - pwmDutyCycle)) / 100) + 1;
 
   return 0;  
 }
diff --git a/core/pwm/pwm_100us_50percent.png b/core/pwm/pwm_100us_50percent.png
deleted file mode 100644 (file)
index efed5f4..0000000
Binary files a/core/pwm/pwm_100us_50percent.png and /dev/null differ
diff --git a/core/pwm/pwmtest_100ticks_50percent.png b/core/pwm/pwmtest_100ticks_50percent.png
new file mode 100644 (file)
index 0000000..42a9eeb
Binary files /dev/null and b/core/pwm/pwmtest_100ticks_50percent.png differ
This page took 0.028479 seconds and 4 git commands to generate.