- Why do we need this ATmega_Slow_PWM library
- Changelog
- Prerequisites
- Installation
- HOWTO Fix
Multiple Definitions
Linker Error - More useful Information
- Usage
- Examples
- Example ISR_8_PWMs_Array_Complex
- Debug
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
Why do we need this ATmega_Slow_PWM library
This library enables you to use ISR-based PWM channels on ATmega164, ATmega324, ATmega644, ATmega1284 using MCUdude MightyCore to create and output PWM any GPIO pin. Because this library doesn't use the powerful purely hardware-controlled PWM with many limitations, the maximum PWM frequency is currently limited at 500Hz, which is still suitable for many real-life applications. Now you can also modify PWM settings on-the-fly.
This library enables you to use Interrupt from Hardware Timers on AVR-based 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 (uint64_t microsecs / 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 PWM channels.
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 ISR_8_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)
PWM channels 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 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 channels using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.
Functions using normal software PWM channels, 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:
- ATmega164(A/P), ATmega324(A/P/PA/PB), ATmega644(A/P), ATmega1284(P)
- ATmega8535, ATmega16 and ATmega32
-
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 IDE 1.8.19+
for ArduinoMCUdude MightyCore v2.1.3+
for ATmega164, ATmega324, ATmega644, ATmega1284. Use Arduino Board Manager to install.- 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 ATmega_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 ATmega_Slow_PWM page.
- Download the latest release
ATmega_Slow_PWM-main.zip
. - Extract the zip file to
ATmega_Slow_PWM-main
directory - Copy whole
ATmega_Slow_PWM-main
folder to Arduino libraries' directory such as~/Arduino/libraries/
.
- Install VS Code
- Install PlatformIO
- Install ATmega_Slow_PWM library by using Library Manager. Search for ATmega_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
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 "ATmega_Slow_PWM.hpp" //https://github.com/khoih-prog/ATmega_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 "ATmega_Slow_PWM.h" //https://github.com/khoih-prog/ATmega_Slow_PWM
Check the new multiFileProject example for a HOWTO
demo.
Have a look at the discussion in Different behaviour using the src_cpp or src_h lib #80
From Arduino 101: Timers and Interrupts
Timer0 is a 8-bit timer.
In the Arduino world, Timer0 is been used for the timer functions, like delay(), millis() and micros(). If you change Timer0 registers, this may influence the Arduino timer function. So you should know what you are doing.
Timer1 is a 16-bit timer. In the Arduino world, the Servo library uses Timer1
Timer2 is a 8-bit timer like Timer0
In the Arduino world, the tone() function uses Timer2.
Timer3 is only available on Arduino ATMEGA_1284(P), ATMEGA_324PB boards
Timer4 is only available on Arduino ATMEGA_324PB boards
Before using any Timer, you have to make sure the Timer has not been used by any other purpose
-
Timer1 and Timer2 are supported for ATmega164(A/P), ATmega324(A/P/PA), ATmega644(A/P)
-
Timer1, Timer2 and Timer3 are supported for ATmega1284(P)
-
Timer1, Timer2, Timer3 and Timer4 are supported for ATmega324PB, which is not yet supported by MightyCore v2.1.3
Before using any Timer, you have to make sure the Timer has not been used by any other purpose.
// Select the timers you're using, here ITimer1
#define USE_TIMER_1 true
#define USE_TIMER_2 false
#define USE_TIMER_3 false
#define USE_TIMER_4 false
// Init ATmega_Slow_PWM, each can service 16 different ISR-based PWM channels
ATmega_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_8_PWMs_Array
- ISR_8_PWMs_Array_Complex
- ISR_8_PWMs_Array_Simple
- ISR_Changing_PWM
- ISR_Modify_PWM
- multiFileProject New
Example ISR_8_PWMs_Array_Complex
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: ATmega_Slow_PWM issues
- Search for bug and improvement.
- Similar features for remaining Arduino boards
- Basic hardware multi-channel PWM for ATmega164(A/P), ATmega324(A/P/PA/PB), ATmega644(A/P), ATmega1284(P) using MightyCore
- Add Table of Contents
- Add functions to modify PWM settings on-the-fly
- Fix
multiple-definitions
linker error. Dropsrc_cpp
andsrc_h
directories - Add example multiFileProject to demo for multiple-file project
- Improve accuracy by using
float
, instead ofuint32_t
fordutycycle
- Optimize library code by using
reference-passing
instead ofvalue-passing
- DutyCycle to be optionally updated at the end current PWM period instead of immediately.
- 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.
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) 2022- Khoi Hoang