Seeed-Studio / ArduinoCore-samd

49 stars 27 forks source link

Accurate PWM frequency for SAMD21 #23

Closed dachristensen closed 4 years ago

dachristensen commented 4 years ago

I ran into some issues trying to generate a 50 Hz PWM signal to drive an SG90 servo motor from a Seeeduino XIAO using an ATSAMD21. No matter what I tried I always ended up with a 733 Hz (48Mhz / 0xFFFF period counter). When I dug into the code it looked like the prescaler register wasn't being set and the PER register wasn't being leveraged to get an accurate frequency. This code resolves this issue and also addresses a couple of bugs that I encountered in the prescaler calculation. I tried to keep the code sane for the SAMD51, but to be honest I didn't have a dev board to test against. With these changes, I was able to generate and measure (using a USB logic analyzer) accurate PWM frequencies from 1Hz up to 46.8Khz (the max frequency attainable while preserving 10 bit resolution).

dachristensen commented 4 years ago

Two more notes about the commit:

  1. I changed toneMaxFrequency to F_CPU because toneMaxFrequency cut the clock in half and did not seem to match the clock that we actually use.
  2. I rearranged the order of the code in the loop that calculates the divider. I seem to have stumbled onto a bug. Consider the following: Assuming a frequency of 24Hz, we will drop below PER_COUNTER (0xffff) when i=3 and ccValue = 24,000,000(toneMaxFrequency) / 24(frequency) / 2<<3(i) = 62500. After this calculation the old code increments i to 4, checks that i=4 and then increments again to 5. After this we exit the loop because ccValue is less than PER_COUNTER. We then switch(i-1 = 5-1 = 4) and fall through to the default case. So, with a requested frequency of 24Hz we get a DIV1 instead of a DIV16.
LynnL4 commented 4 years ago

Thanks for you PR, I'll test it and then merge your PR.