From: Kevin Townsend Date: Wed, 9 May 2012 21:39:24 +0000 (+0200) Subject: Improved accuracy X-Git-Url: http://git.rohieb.name/hackover2013-badge-firmware.git/commitdiff_plain/735f9134948b8079d76dddafec6e84e8d160445a?ds=sidebyside Improved accuracy --- diff --git a/core/pwm/pwm.c b/core/pwm/pwm.c index b94329f..7f1e2e5 100644 --- a/core/pwm/pwm.c +++ b/core/pwm/pwm.c @@ -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 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 index 0000000..42a9eeb Binary files /dev/null and b/core/pwm/pwmtest_100ticks_50percent.png differ