- Important Change from v1.2.0
- Why do we need this SAMD_Slow_PWM library
- Changelog
- Prerequisites
- Installation
- Packages' Patches
- HOWTO Fix
Multiple Definitions
Linker Error - Usage
- Examples
- Example SAMD51 ISR_16_PWMs_Array_Complex
- Debug Terminal Output Samples
- Debug
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
Please have a look at HOWTO Fix Multiple Definitions
Linker Error
As more complex calculation and check inside ISR are introduced from v1.2.0, there are consequences as follows
- For SAMD21 (slower) => using min 50uS and max 4 PWM channels
// Use 50uS for slow SAMD21
#define HW_TIMER_INTERVAL_US 50L
- For SAMD51 (faster) => using min 20uS and max 16 PWM channels
// Use 30uS for faster SAMD51
#define HW_TIMER_INTERVAL_US 20L
You certainly can modify to use better values according to your board and use-case, just remember to test and reverse to conservative values if crash happens.
Why do we need this SAMD_Slow_PWM library
This library enables you to use Hardware Timers on SAMD21/SAMD51 boards such as NANO_33_IOT, ITSYBITSY_M4, SEEED_XIAO_M0, SparkFun_SAMD51_Thing_Plus, etc., to create and output PWM to pins. Because this library doesn't use the powerful hardware-controlled PWM with limitations, the maximum PWM frequency is currently limited at 1000Hz, which is suitable for many real-life applications.
This library enables you to use Interrupt from Hardware Timers on SAMD21/SAMD51 boards to create and output PWM to pins. It now supports 16 ISR-based synchronized PWM channels, while consuming only 1 Hardware Timer. PWM interval can be very long (uint32_t millisecs). The most important feature is they're ISR-based PWM channels. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks. These hardware PWM channels, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software PWM using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.
As Hardware Timers are rare, and very precious assets of any board, this library now enables you to use up to 16 ISR-based synchronized PWM channels, while consuming only 1 Hardware Timer. Timers' interval is very long (ulong millisecs).
Now with these new 16 ISR-based PWM-channels, the maximum interval is practically unlimited (limited only by unsigned long milliseconds) while the accuracy is nearly perfect compared to software timers.
The most important feature is they're ISR-based PWM channels. Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks.
The SAMD51 PWMs_Array_Complex example will demonstrate the nearly perfect accuracy, compared to software PWM, by printing the actual period / duty-cycle in microsecs
of each of PWM-channels.
Being ISR-based PWM, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet or Blynk services. You can also have many (up to 16)
timers to use.
This non-being-blocked important feature is absolutely necessary for mission-critical tasks.
You'll see software-based
SimpleTimer is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task
in loop(), using delay() function as an example. The elapsed time then is very unaccurate
Imagine you have a system with a mission-critical function, measuring water level and control the sump pump or doing something much more important. You normally use a software timer to poll, or even place the function in loop(). But what if another function is blocking the loop() or setup().
So your function might not be executed, and the result would be disastrous.
You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).
The correct choice is to use a Hardware Timer with Interrupt to call your function.
These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.
Functions using normal software timers, relying on loop() and calling millis(), won't work if the loop() or setup() is blocked by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services.
The catch is your function is now part of an ISR (Interrupt Service Routine), and must be lean / mean, and follow certain rules. More to read on:
- Arduino SAMD21 (ZERO, MKR, NANO_33_IOT, etc.).
- Adafruit SAM21 (Itsy-Bitsy M0, Metro M0, Feather M0, Gemma M0, etc.).
- Adafruit SAM51 (Itsy-Bitsy M4, Metro M4, Grand Central M4, Feather M4 Express, etc.).
- Seeeduino SAMD21/SAMD51 boards (SEEED_WIO_TERMINAL, SEEED_FEMTO_M0, SEEED_XIAO_M0, Wio_Lite_MG126, WIO_GPS_BOARD, SEEEDUINO_ZERO, SEEEDUINO_LORAWAN, SEEED_GROVE_UI_WIRELESS, etc.)
- Sparkfun SAMD21 boards such as SparkFun_RedBoard_Turbo, SparkFun_Qwiic_Micro, etc.
- Sparkfun SAMD51 boards such as SparkFun_SAMD51_Thing_Plus, SparkFun_SAMD51_MicroMod, etc.
-
Inside the attached function, delay() won’t work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.
-
Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as volatile.
-
Arduino SAMD core 1.8.13+
for SAMD ARM Cortex-M0+ boards. -
Adafruit SAMD core 1.7.11+
for SAMD ARM Cortex-M0+ and M4 boards (Nano 33 IoT, etc.). -
Seeeduino SAMD core 1.8.3+
for SAMD21/SAMD51 boards (XIAO M0, Wio Terminal, etc.). -
Sparkfun SAMD core 1.8.3+
for SAMD21/SAMD51 boards (SparkFun_RedBoard_Turbo, SparkFun_SAMD51_Thing_Plus, etc.). -
To use with certain example
SimpleTimer library
to use with some examples.
The best and easiest way is to use Arduino Library Manager
. Search for SAMD_Slow_PWM, then select / install the latest version.
You can also use this link for more detailed instructions.
Another way to install is to:
- Navigate to SAMD_Slow_PWM page.
- Download the latest release
SAMD_Slow_PWM-main.zip
. - Extract the zip file to
SAMD_Slow_PWM-main
directory - Copy whole
SAMD_Slow_PWM-main
folder to Arduino libraries' directory such as~/Arduino/libraries/
.
- Install VS Code
- Install PlatformIO
- Install SAMD_Slow_PWM library by using Library Manager. Search for SAMD_Slow_PWM in Platform.io Author's Libraries
- Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File
To be able to compile without error and automatically detect and display BOARD_NAME on Arduino SAMD (Nano-33-IoT, etc) boards, you have to copy the whole Arduino SAMD Packages_Patches directory into Arduino SAMD directory (~/.arduino15/packages/arduino/hardware/samd/1.8.13).
Supposing the Arduino SAMD version is 1.8.13. Now only one file must be copied into the directory:
~/.arduino15/packages/arduino/hardware/samd/1.8.13/platform.txt
Whenever a new version is installed, remember to copy this files into the new version directory. For example, new version is x.yy.zz
This file must be copied into the directory:
~/.arduino15/packages/arduino/hardware/samd/x.yy.zz/platform.txt
Supposing the Arduino SAMD version is 1.8.9. These files must be copied into the directory:
~/.arduino15/packages/arduino/hardware/samd/1.8.9/platform.txt
~/.arduino15/packages/arduino/hardware/samd/1.8.9/cores/arduino/Arduino.h
Whenever a new version is installed, remember to copy these files into the new version directory. For example, new version is x.yy.z
These files must be copied into the directory:
~/.arduino15/packages/arduino/hardware/samd/x.yy.z/platform.txt
~/.arduino15/packages/arduino/hardware/samd/x.yy.z/cores/arduino/Arduino.h
This is mandatory to fix the notorious Arduino SAMD compiler error. See Improve Arduino compatibility with the STL (min and max macro)
...\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:243:56: error: macro "min" passed 3 arguments, but takes just 2
min(const _Tp& __a, const _Tp& __b, _Compare __comp)
Whenever the above-mentioned compiler error issue is fixed with the new Arduino SAMD release, you don't need to copy the Arduino.h
file anymore.
To be able to compile without error and automatically detect and display BOARD_NAME on Adafruit SAMD (Itsy-Bitsy M4, etc) boards, you have to copy the files in Adafruit SAMD Packages_Patches into Adafruit samd directory (~/.arduino15/packages/adafruit/hardware/samd/1.7.11).
Supposing the Adafruit SAMD core version is 1.7.11. This file must be copied into the directory:
~/.arduino15/packages/adafruit/hardware/samd/1.7.11/platform.txt
~/.arduino15/packages/adafruit/hardware/samd/1.7.11/cores/arduino/Print.h
~/.arduino15/packages/adafruit/hardware/samd/1.7.11/cores/arduino/Print.cpp
Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz This file must be copied into the directory:
~/.arduino15/packages/adafruit/hardware/samd/x.yy.zz/platform.txt
~/.arduino15/packages/adafruit/hardware/samd/x.yy.zz/cores/arduino/Print.h
~/.arduino15/packages/adafruit/hardware/samd/x.yy.zz/cores/arduino/Print.cpp
To be able to compile without error and automatically detect and display BOARD_NAME on Seeeduino SAMD (XIAO M0, Wio Terminal, etc) boards, you have to copy the files in Seeeduino SAMD Packages_Patches into Seeeduino samd directory (~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3).
Supposing the Seeeduino SAMD core version is 1.8.3. This file must be copied into the directory:
~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/platform.txt
~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/cores/arduino/Arduino.h
~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/cores/arduino/Print.h
~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/cores/arduino/Print.cpp
Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz This file must be copied into the directory:
~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/platform.txt
~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/cores/arduino/Arduino.h
~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/cores/arduino/Print.h
~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/cores/arduino/Print.cpp
To be able to compile without error and automatically detect and display BOARD_NAME on SparkFun SAMD (XIAO SparkFun_RedBoard_Turbo, SparkFun_SAMD51_Thing_Plus, etc) boards, you have to copy the file SparkFun SAMD Packages_Patches into SparkFun samd directory (~/.arduino15/packages/SparkFun/hardware/samd/1.8.3).
Supposing the SparkFun SAMD core version is 1.8.3. This file must be copied into the directory:
~/.arduino15/packages/SparkFun/hardware/samd/1.8.3/cores/arduino/Print.h
~/.arduino15/packages/SparkFun/hardware/samd/1.8.3/cores/arduino/Print.cpp
~/.arduino15/packages/SparkFun/hardware/samd/1.8.3/cores/arduino51/Print.h
~/.arduino15/packages/SparkFun/hardware/samd/1.8.3/cores/arduino51/Print.cpp
Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz This file must be copied into the directory:
~/.arduino15/packages/SparkFun/hardware/samd/x.yy.zz/cores/arduino/Print.h
~/.arduino15/packages/SparkFun/hardware/samd/x.yy.zz/cores/arduino/Print.cpp
~/.arduino15/packages/SparkFun/hardware/samd/x.yy.zz/cores/arduino51/Print.h
~/.arduino15/packages/SparkFun/hardware/samd/x.yy.zz/cores/arduino51/Print.cpp
The current library implementation, using xyz-Impl.h
instead of standard xyz.cpp
, possibly creates certain Multiple Definitions
Linker error in certain use cases.
You can include this .hpp
file
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "SAMD_Slow_PWM.hpp" //https://github.com/khoih-prog/SAMD_Slow_PWM
in many files. But be sure to use the following .h
file in just 1 .h
, .cpp
or .ino
file, which must not be included in any other file, to avoid Multiple Definitions
Linker Error
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "SAMD_Slow_PWM.h" //https://github.com/khoih-prog/SAMD_Slow_PWM
Check the new SAMD21 multiFileProject example or SAMD51 multiFileProject example for a HOWTO
demo.
Have a look at the discussion in Different behaviour using the src_cpp or src_h lib #80
Before using any Timer for a PWM channel, you have to make sure the Timer has not been used by any other purpose.
// Depending on the board, you can select SAMD21 Hardware Timer from TC3-TCC
// SAMD21 Hardware Timer from TC3 or TCC
// SAMD51 Hardware Timer only TC3
SAMDTimer ITimer0(TIMER_TC3);
// You can only select SAMD51 Hardware Timer TC3
// Init SAMD timer TIMER_TC3
//SAMDTimer ITimer(TIMER_TC3);
// Init SAMD_Slow_PWM
SAMD_Slow_PWM ISR_PWM;
void irqCallbackStartFunc()
{
}
void irqCallbackStopFunc()
{
}
void setup()
{
....
// You can use this with PWM_Freq in Hz
ISR_PWM.setPWM(PWM_Pin, PWM_Freq, PWM_DutyCycle, irqCallbackStartFunc, irqCallbackStopFunc);
....
}
- ISR_4_PWMs_Array
- ISR_4_PWMs_Array_Complex
- ISR_4_PWMs_Array_Simple
- ISR_Changing_PWM
- ISR_Modify_PWM
- multiFileProject. New
- ISR_16_PWMs_Array
- ISR_16_PWMs_Array_Complex
- ISR_16_PWMs_Array_Simple
- ISR_Changing_PWM
- ISR_Modify_PWM
- multiFileProject. New
Example SAMD51 ISR_16_PWMs_Array_Complex
The following is the sample terminal output when running example ISR_16_PWMs_Array_Complex on SAMD_NANO_33_IOT to demonstrate how to use multiple PWM channels with complex callback functions, the accuracy of ISR Hardware PWM-channels, especially when system is very busy. The ISR PWM-channels is running exactly according to corresponding programmed periods and duty-cycles
Starting ISR_4_PWMs_Array_Complex on SAMD_NANO_33_IOT
SAMD_Slow_PWM v1.2.3
Starting ITimer OK, micros() = 2750362
Channel : 0 Period : 1000000 OnTime : 50000 Start_Time : 2750883
Channel : 1 Period : 500000 OnTime : 50000 Start_Time : 2751343
Channel : 2 Period : 250000 OnTime : 50000 Start_Time : 2751845
Channel : 3 Period : 200000 OnTime : 60000 Start_Time : 2752329
SimpleTimer (ms): 2000, us : 12752801, Dus : 10002150
PWM Channel : 0, programmed Period (us): 1000000.00, actual : 1000000, programmed DutyCycle : 5.00, actual : 5.00
PWM Channel : 1, programmed Period (us): 500000.00, actual : 499999, programmed DutyCycle : 10.00, actual : 10.00
PWM Channel : 2, programmed Period (us): 250000.00, actual : 249999, programmed DutyCycle : 20.00, actual : 20.00
PWM Channel : 3, programmed Period (us): 200000.00, actual : 199999, programmed DutyCycle : 30.00, actual : 30.00
SimpleTimer (ms): 2000, us : 22757321, Dus : 10004520
PWM Channel : 0, programmed Period (us): 1000000.00, actual : 1000000, programmed DutyCycle : 5.00, actual : 5.00
PWM Channel : 1, programmed Period (us): 500000.00, actual : 499999, programmed DutyCycle : 10.00, actual : 10.00
PWM Channel : 2, programmed Period (us): 250000.00, actual : 250000, programmed DutyCycle : 20.00, actual : 20.00
PWM Channel : 3, programmed Period (us): 200000.00, actual : 199999, programmed DutyCycle : 30.00, actual : 30.00
SimpleTimer (ms): 2000, us : 32761791, Dus : 10004470
PWM Channel : 0, programmed Period (us): 1000000.00, actual : 1000000, programmed DutyCycle : 5.00, actual : 5.00
PWM Channel : 1, programmed Period (us): 500000.00, actual : 499999, programmed DutyCycle : 10.00, actual : 10.00
PWM Channel : 2, programmed Period (us): 250000.00, actual : 249999, programmed DutyCycle : 20.00, actual : 20.00
PWM Channel : 3, programmed Period (us): 200000.00, actual : 199999, programmed DutyCycle : 30.00, actual : 30.00
SimpleTimer (ms): 2000, us : 42766231, Dus : 10004440
PWM Channel : 0, programmed Period (us): 1000000.00, actual : 1000000, programmed DutyCycle : 5.00, actual : 5.00
PWM Channel : 1, programmed Period (us): 500000.00, actual : 499999, programmed DutyCycle : 10.00, actual : 10.00
PWM Channel : 2, programmed Period (us): 250000.00, actual : 249999, programmed DutyCycle : 20.00, actual : 20.00
PWM Channel : 3, programmed Period (us): 200000.00, actual : 199999, programmed DutyCycle : 30.00, actual : 30.00
The following is the sample terminal output when running example ISR_16_PWMs_Array_Complex on ITSYBITSY_M4 to demonstrate how to use multiple PWM channels with complex callback functions, the accuracy of ISR Hardware PWM-channels, especially when system is very busy. The ISR PWM-channels is running exactly according to corresponding programmed periods and duty-cycles
Starting ISR_16_PWMs_Array_Complex on ITSYBITSY_M4
SAMD_Slow_PWM v1.2.3
Starting ITimer OK, micros() = 3830244
Channel : 0 Period : 1000000 OnTime : 50000 Start_Time : 3830538
Channel : 1 Period : 500000 OnTime : 50000 Start_Time : 3830903
Channel : 2 Period : 333333 OnTime : 66666 Start_Time : 3831277
Channel : 3 Period : 250000 OnTime : 75000 Start_Time : 3831708
Channel : 4 Period : 200000 OnTime : 80000 Start_Time : 3832077
Channel : 5 Period : 166666 OnTime : 74999 Start_Time : 3832451
Channel : 6 Period : 142857 OnTime : 71428 Start_Time : 3832874
Channel : 7 Period : 125000 OnTime : 68750 Start_Time : 3833251
Channel : 8 Period : 111111 OnTime : 66666 Start_Time : 3833671
Channel : 9 Period : 100000 OnTime : 65000 Start_Time : 3834049
Channel : 10 Period : 66666 OnTime : 46666 Start_Time : 3834419
Channel : 11 Period : 50000 OnTime : 37500 Start_Time : 3834849
Channel : 12 Period : 40000 OnTime : 32000 Start_Time : 3835219
Channel : 13 Period : 33333 OnTime : 28333 Start_Time : 3835639
Channel : 14 Period : 25000 OnTime : 22500 Start_Time : 3836016
Channel : 15 Period : 20000 OnTime : 19000 Start_Time : 3836393
SimpleTimer (ms): 2000, us : 13836817, Dus : 10006340
PWM Channel : 0, programmed Period (us): 1000000.00, actual : 1000000, programmed DutyCycle : 5.00, actual : 5.00
PWM Channel : 1, programmed Period (us): 500000.00, actual : 500000, programmed DutyCycle : 10.00, actual : 10.00
PWM Channel : 2, programmed Period (us): 333333.34, actual : 333340, programmed DutyCycle : 20.00, actual : 20.00
PWM Channel : 3, programmed Period (us): 250000.00, actual : 250000, programmed DutyCycle : 30.00, actual : 30.00
PWM Channel : 4, programmed Period (us): 200000.00, actual : 200000, programmed DutyCycle : 40.00, actual : 40.00
PWM Channel : 5, programmed Period (us): 166666.67, actual : 166679, programmed DutyCycle : 45.00, actual : 44.98
PWM Channel : 6, programmed Period (us): 142857.14, actual : 142860, programmed DutyCycle : 50.00, actual : 49.99
PWM Channel : 7, programmed Period (us): 125000.00, actual : 125000, programmed DutyCycle : 55.00, actual : 54.99
PWM Channel : 8, programmed Period (us): 111111.11, actual : 111120, programmed DutyCycle : 60.00, actual : 59.99
PWM Channel : 9, programmed Period (us): 100000.00, actual : 99999, programmed DutyCycle : 65.00, actual : 65.00
PWM Channel : 10, programmed Period (us): 66666.66, actual : 66679, programmed DutyCycle : 70.00, actual : 69.98
PWM Channel : 11, programmed Period (us): 50000.00, actual : 50000, programmed DutyCycle : 75.00, actual : 75.00
PWM Channel : 12, programmed Period (us): 40000.00, actual : 40000, programmed DutyCycle : 80.00, actual : 80.00
PWM Channel : 13, programmed Period (us): 33333.33, actual : 33339, programmed DutyCycle : 85.00, actual : 84.95
PWM Channel : 14, programmed Period (us): 25000.00, actual : 24999, programmed DutyCycle : 90.00, actual : 90.00
PWM Channel : 15, programmed Period (us): 20000.00, actual : 19999, programmed DutyCycle : 95.00, actual : 95.00
SimpleTimer (ms): 2000, us : 23848190, Dus : 10011373
PWM Channel : 0, programmed Period (us): 1000000.00, actual : 1000000, programmed DutyCycle : 5.00, actual : 5.00
PWM Channel : 1, programmed Period (us): 500000.00, actual : 500000, programmed DutyCycle : 10.00, actual : 10.00
PWM Channel : 2, programmed Period (us): 333333.34, actual : 333340, programmed DutyCycle : 20.00, actual : 20.00
PWM Channel : 3, programmed Period (us): 250000.00, actual : 250000, programmed DutyCycle : 30.00, actual : 30.00
PWM Channel : 4, programmed Period (us): 200000.00, actual : 200000, programmed DutyCycle : 40.00, actual : 40.00
PWM Channel : 5, programmed Period (us): 166666.67, actual : 166679, programmed DutyCycle : 45.00, actual : 44.98
PWM Channel : 6, programmed Period (us): 142857.14, actual : 142860, programmed DutyCycle : 50.00, actual : 49.99
PWM Channel : 7, programmed Period (us): 125000.00, actual : 125000, programmed DutyCycle : 55.00, actual : 54.99
PWM Channel : 8, programmed Period (us): 111111.11, actual : 111120, programmed DutyCycle : 60.00, actual : 59.99
PWM Channel : 9, programmed Period (us): 100000.00, actual : 99999, programmed DutyCycle : 65.00, actual : 65.00
PWM Channel : 10, programmed Period (us): 66666.66, actual : 66679, programmed DutyCycle : 70.00, actual : 69.98
PWM Channel : 11, programmed Period (us): 50000.00, actual : 50000, programmed DutyCycle : 75.00, actual : 75.00
PWM Channel : 12, programmed Period (us): 40000.00, actual : 40000, programmed DutyCycle : 80.00, actual : 80.00
PWM Channel : 13, programmed Period (us): 33333.33, actual : 33339, programmed DutyCycle : 85.00, actual : 84.94
PWM Channel : 14, programmed Period (us): 25000.00, actual : 24999, programmed DutyCycle : 90.00, actual : 90.01
PWM Channel : 15, programmed Period (us): 20000.00, actual : 20000, programmed DutyCycle : 95.00, actual : 95.00
The following is the sample terminal output when running example ISR_4_PWMs_Array_Complex on SEEED_XIAO_M0 to demonstrate how to use multiple PWM channels with complex callback functions, the accuracy of ISR Hardware PWM-channels, especially when system is very busy. The ISR PWM-channels is running exactly according to corresponding programmed periods and duty-cycles
Starting ISR_16_PWMs_Array_Complex on SEEED_XIAO_M0
SAMD_Slow_PWM v1.2.3
Starting ITimer OK, micros() = 2681651
Channel : 0 Period : 1000000 OnTime : 50000 Start_Time : 2682003
Channel : 1 Period : 500000 OnTime : 50000 Start_Time : 2682003
Channel : 2 Period : 66666 OnTime : 13333 Start_Time : 2682003
Channel : 3 Period : 50000 OnTime : 15000 Start_Time : 2682003
SimpleTimer (ms): 2000, us : 22699166, Dus : 10011622
PWM Channel : 0, programmed Period (us): 1000000, actual : 1000000, programmed DutyCycle : 5, actual : 5.00
PWM Channel : 1, programmed Period (us): 500000, actual : 500009, programmed DutyCycle : 10, actual : 10.00
PWM Channel : 2, programmed Period (us): 66666, actual : 66679, programmed DutyCycle : 20, actual : 19.98
PWM Channel : 3, programmed Period (us): 50000, actual : 50005, programmed DutyCycle : 30, actual : 29.99
SimpleTimer (ms): 2000, us : 32713108, Dus : 10013942
PWM Channel : 0, programmed Period (us): 1000000, actual : 1000000, programmed DutyCycle : 5, actual : 5.00
PWM Channel : 1, programmed Period (us): 500000, actual : 499999, programmed DutyCycle : 10, actual : 10.00
PWM Channel : 2, programmed Period (us): 66666, actual : 66679, programmed DutyCycle : 20, actual : 19.98
PWM Channel : 3, programmed Period (us): 50000, actual : 49999, programmed DutyCycle : 30, actual : 30.00
The following is the sample terminal output when running example ISR_Modify_PWM on SAMD_NANO_33_IOT to demonstrate how to modify PWM settings on-the-fly without deleting the PWM channel
Starting ISR_Modify_PWM on SAMD_NANO_33_IOT
SAMD_Slow_PWM v1.2.3
[PWM] SAMDTimerInterrupt: F_CPU (MHz) = 48 , TIMER_HZ = 48
[PWM] TC3_Timer::startTimer _Timer = 0x 42002c00 , TC3 = 0x 42002c00
Starting ITimer OK, micros() = 3761339
Using PWM Freq = 200.00, PWM DutyCycle = 1.00
Channel : 0 Period : 5000 OnTime : 50 Start_Time : 3762307
Channel : 0 Period : 10000 OnTime : 555 Start_Time : 13762340
Channel : 0 Period : 5000 OnTime : 50 Start_Time : 23757340
Channel : 0 Period : 10000 OnTime : 555 Start_Time : 33762390
Channel : 0 Period : 5000 OnTime : 50 Start_Time : 43757390
Channel : 0 Period : 10000 OnTime : 555 Start_Time : 53762390
Channel : 0 Period : 5000 OnTime : 50 Start_Time : 63767390
Channel : 0 Period : 10000 OnTime : 555 Start_Time : 73767390
Channel : 0 Period : 5000 OnTime : 50 Start_Time : 83762390
Channel : 0 Period : 10000 OnTime : 555 Start_Time : 93767390
Channel : 0 Period : 5000 OnTime : 50 Start_Time : 103762390
Channel : 0 Period : 10000 OnTime : 555 Start_Time : 113772440
Channel : 0 Period : 5000 OnTime : 50 Start_Time : 123767440
The following is the sample terminal output when running example ISR_Changing_PWM on SAMD_NANO_33_IOT to demonstrate how to modify PWM settings on-the-fly by deleting the PWM channel and reinit the PWM channel
Starting ISR_Changing_PWM on SAMD_NANO_33_IOT
SAMD_Slow_PWM v1.2.3
Starting ITimer OK, micros() = 2820370
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 2821335
Using PWM Freq = 2.00, PWM DutyCycle = 90.00
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 12822309
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 22823296
Using PWM Freq = 2.00, PWM DutyCycle = 90.00
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 32824232
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 42825239
Using PWM Freq = 2.00, PWM DutyCycle = 90.00
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 52826230
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 62827189
The following is the sample terminal output when running example ISR_Modify_PWM on ITSYBITSY_M4 to demonstrate how to modify PWM settings on-the-fly without deleting the PWM channel
Starting ISR_Modify_PWM on ITSYBITSY_M4
SAMD_Slow_PWM v1.2.3
Starting ITimer OK, micros() = 3320212
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 3320837
Channel : 0 New Period : 500000 OnTime : 450000 Start_Time : 13320852
Channel : 0 New Period : 1000000 OnTime : 500000 Start_Time : 23320852
Channel : 0 New Period : 500000 OnTime : 450000 Start_Time : 32820852
Channel : 0 New Period : 1000000 OnTime : 500000 Start_Time : 43320852
Channel : 0 New Period : 500000 OnTime : 450000 Start_Time : 52820852
Channel : 0 New Period : 1000000 OnTime : 500000 Start_Time : 63320852
Channel : 0 New Period : 500000 OnTime : 450000 Start_Time : 72820852
Channel : 0 New Period : 1000000 OnTime : 500000 Start_Time : 83320852
Channel : 0 New Period : 500000 OnTime : 450000 Start_Time : 92820852
Channel : 0 New Period : 1000000 OnTime : 500000 Start_Time : 103320852
Channel : 0 New Period : 500000 OnTime : 450000 Start_Time : 112820852
Channel : 0 New Period : 1000000 OnTime : 500000 Start_Time : 123320852
Channel : 0 New Period : 500000 OnTime : 450000 Start_Time : 132820852
Channel : 0 New Period : 1000000 OnTime : 500000 Start_Time : 143320852
Channel : 0 New Period : 500000 OnTime : 450000 Start_Time : 152820852
Channel : 0 New Period : 1000000 OnTime : 500000 Start_Time : 163320852
Channel : 0 New Period : 500000 OnTime : 450000 Start_Time : 172820852
The following is the sample terminal output when running example ISR_Changing_PWM on ITSYBITSY_M4 to demonstrate how to modify PWM settings on-the-fly by deleting the PWM channel and reinit the PWM channel
Starting ISR_Changing_PWM on ITSYBITSY_M4
SAMD_Slow_PWM v1.2.3
Starting ITimer OK, micros() = 3660252
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 3660870
Using PWM Freq = 2.00, PWM DutyCycle = 90.00
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 13661504
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 23662239
Debug is enabled by default on Serial.
You can also change the debugging level _PWM_LOGLEVEL_
from 0 to 4
// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define _PWM_LOGLEVEL_ 0
If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.
Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.
Submit issues to: SAMD_Slow_PWM issues
- Search for bug and improvement.
- Similar features for remaining Arduino boards
- Basic hardware multi-channel PWM for SAMD21/SAMD51 boards such as NANO_33_IOT, ITSYBITSY_M4, SEEED_XIAO_M0, SparkFun_SAMD51_Thing_Plus, etc. using Arduino, Adafruit or Sparkfun core
- Add Table of Contents
- Add functions to modify PWM settings on-the-fly
- Fix
multiple-definitions
linker error. Dropsrc_cpp
andsrc_h
directories - DutyCycle to be optionally updated at the end current PWM period instead of immediately.
- Add examples SAMD21 multiFileProject and SAMD51 multiFileProjectto demo for multiple-file project
- Improve accuracy by using
float
, instead ofuint32_t
fordutycycle
- Optimize library code by using
reference-passing
instead ofvalue-passing
- Add support to many more boards, such as
SAMD21E1xA
,SAMD21G1xA
andSAMD21J1xA
- Display informational warning only when
_PWM_LOGLEVEL_
> 3
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
- toniMephis to report
- issue missing header #2 leading to version v1.2.2
- issues float precisison of DutyCycle only sometimes working #3 and random dropouts #4 leading to version v1.2.3
⭐️ toniMephis |
If you want to contribute to this project:
- Report bugs and errors
- Ask for enhancements
- Create issues and pull requests
- Tell other people about this library
- The library is licensed under MIT
Copyright (c) 2021- Khoi Hoang