Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
/ ATmega_Slow_PWM Public archive

This library enables you to use ISR-based PWM channels on AVR ATmega164, ATmega324, ATmega644, ATmega1284 with MCUdude MightyCore, to create and output PWM any GPIO pin. It now supports 16 ISR-based PWM channels, while consuming only 1 Hardware Timer. PWM channel interval can be very long (ulong microsecs / millisecs). The most important feature…

License

Notifications You must be signed in to change notification settings

khoih-prog/ATmega_Slow_PWM

Repository files navigation

ATmega_Slow_PWM Library

arduino-library-badge GitHub release GitHub contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents



Why do we need this ATmega_Slow_PWM library

Features

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


Why using ISR-based PWM is better

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:

HOWTO Attach Interrupt


Currently supported Boards

  • ATmega164(A/P), ATmega324(A/P/PA/PB), ATmega644(A/P), ATmega1284(P)

Not yet supported Boards

  • ATmega8535, ATmega16 and ATmega32

Important Notes about ISR

  1. 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.

  2. 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.



Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino
  2. MCUdude MightyCore v2.1.3+ for ATmega164, ATmega324, ATmega644, ATmega1284. Use Arduino Board Manager to install. Latest release
  3. To use with certain example


Installation

Use Arduino Library Manager

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 arduino-library-badge for more detailed instructions.

Manual Install

Another way to install is to:

  1. Navigate to ATmega_Slow_PWM page.
  2. Download the latest release ATmega_Slow_PWM-main.zip.
  3. Extract the zip file to ATmega_Slow_PWM-main directory
  4. Copy whole ATmega_Slow_PWM-main folder to Arduino libraries' directory such as ~/Arduino/libraries/.

VS Code & PlatformIO

  1. Install VS Code
  2. Install PlatformIO
  3. Install ATmega_Slow_PWM library by using Library Manager. Search for ATmega_Slow_PWM in Platform.io Author's Libraries
  4. 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


HOWTO Fix Multiple Definitions Linker Error

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



More useful Information

From Arduino 101: Timers and Interrupts

1. Timer0:

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.

2. Timer1:

Timer1 is a 16-bit timer. In the Arduino world, the Servo library uses Timer1

3. Timer2:

Timer2 is a 8-bit timer like Timer0

In the Arduino world, the tone() function uses Timer2.

4. Timer3 and Timer4:

Timer3 is only available on Arduino ATMEGA_1284(P), ATMEGA_324PB boards

Timer4 is only available on Arduino ATMEGA_324PB boards

5. Important Notes

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



Usage

Before using any Timer, you have to make sure the Timer has not been used by any other purpose.

1. Init Hardware Timer

// 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;

2. Set PWM Frequency, dutycycle, attach irqCallbackStartFunc and irqCallbackStopFunc functions

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);
                   
  ....                 
}  


Examples:

  1. ISR_8_PWMs_Array
  2. ISR_8_PWMs_Array_Complex
  3. ISR_8_PWMs_Array_Simple
  4. ISR_Changing_PWM
  5. ISR_Modify_PWM
  6. multiFileProject New


// Select just 1 TIMER to be true
#define USE_TIMER_1 true
#define USE_TIMER_2 false
// TIMER_3 Only valid for ATmega1284 and ATmega324PB (not ready in core yet)
#define USE_TIMER_3 false
// TIMER_4 Only valid for ATmega324PB, not ready in core yet
#define USE_TIMER_4 false
// These define's must be placed at the beginning before #include "ATmega_Slow_PWM.h"
// _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_ 3
#if (_PWM_LOGLEVEL_ > 3)
#if USE_TIMER_1
#warning Using Timer1
#elif USE_TIMER_1
#warning Using Timer3
#endif
#endif
#define USING_MICROS_RESOLUTION true //false
// Default is true, uncomment to false
//#define CHANGING_PWM_END_OF_CYCLE false
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "ATmega_Slow_PWM.h"
#include <SimpleTimer.h> // https://github.com/jfturcot/SimpleTimer
#define LED_OFF HIGH
#define LED_ON LOW
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
#ifndef LED_BLUE
#define LED_BLUE 10
#endif
#ifndef LED_RED
#define LED_RED 11
#endif
#define USING_HW_TIMER_INTERVAL_MS false //true
// Don't change these numbers to make higher Timer freq. System can hang
#define HW_TIMER_INTERVAL_MS 0.1f
#define HW_TIMER_INTERVAL_FREQ 10000L
volatile uint32_t startMicros = 0;
// Init ATmega_Slow_PWM, each can service 16 different ISR-based PWM channels
ATmega_Slow_PWM ISR_PWM;
//////////////////////////////////////////////////////
void TimerHandler()
{
ISR_PWM.run();
}
/////////////////////////////////////////////////
#define PIN_D0 0
#define PIN_D1 1
#define PIN_D2 2
#define PIN_D3 3
#define PIN_D4 4
#define PIN_D5 5
#define PIN_D6 6
// You can assign pins here. Be careful to select good pin to use or crash, e.g pin 6-11
uint32_t PWM_Pin[] =
{
LED_BUILTIN, PIN_D0, PIN_D1, PIN_D2, PIN_D3, PIN_D4, PIN_D5, PIN_D6
};
#define NUMBER_ISR_PWMS ( sizeof(PWM_Pin) / sizeof(uint32_t) )
typedef void (*irqCallback) ();
//////////////////////////////////////////////////////
#define USE_COMPLEX_STRUCT true
//////////////////////////////////////////////////////
#if USE_COMPLEX_STRUCT
typedef struct
{
uint32_t PWM_Pin;
irqCallback irqCallbackStartFunc;
irqCallback irqCallbackStopFunc;
float PWM_Freq;
float PWM_DutyCycle;
uint32_t deltaMicrosStart;
uint32_t previousMicrosStart;
uint32_t deltaMicrosStop;
uint32_t previousMicrosStop;
} ISR_PWM_Data;
// In nRF52, avoid doing something fancy in ISR, for example Serial.print()
// The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
// Or you can get this run-time error / crash
void doingSomethingStart(int index);
void doingSomethingStop(int index);
#else // #if USE_COMPLEX_STRUCT
volatile unsigned long deltaMicrosStart [] = { 0, 0, 0, 0, 0, 0, 0, 0 };
volatile unsigned long previousMicrosStart [] = { 0, 0, 0, 0, 0, 0, 0, 0 };
volatile unsigned long deltaMicrosStop [] = { 0, 0, 0, 0, 0, 0, 0, 0 };
volatile unsigned long previousMicrosStop [] = { 0, 0, 0, 0, 0, 0, 0, 0 };
// You can assign any interval for any timer here, in Microseconds
uint32_t PWM_Period[] =
{
1000, 500, 333, 250, 200, 167, 143, 125
};
// You can assign any interval for any timer here, in Hz
float PWM_Freq[] =
{
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
};
// You can assign any interval for any timer here, in Microseconds
float PWM_DutyCycle[] =
{
5.0, 10.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0
};
void doingSomethingStart(int index)
{
unsigned long currentMicros = micros();
deltaMicrosStart[index] = currentMicros - previousMicrosStart[index];
previousMicrosStart[index] = currentMicros;
}
void doingSomethingStop(int index)
{
unsigned long currentMicros = micros();
// Count from start to stop PWM pulse
deltaMicrosStop[index] = currentMicros - previousMicrosStart[index];
previousMicrosStop[index] = currentMicros;
}
#endif // #if USE_COMPLEX_STRUCT
////////////////////////////////////
// Shared
////////////////////////////////////
void doingSomethingStart0()
{
doingSomethingStart(0);
}
void doingSomethingStart1()
{
doingSomethingStart(1);
}
void doingSomethingStart2()
{
doingSomethingStart(2);
}
void doingSomethingStart3()
{
doingSomethingStart(3);
}
void doingSomethingStart4()
{
doingSomethingStart(4);
}
void doingSomethingStart5()
{
doingSomethingStart(5);
}
void doingSomethingStart6()
{
doingSomethingStart(6);
}
void doingSomethingStart7()
{
doingSomethingStart(7);
}
//////////////////////////////////////////////////////
void doingSomethingStop0()
{
doingSomethingStop(0);
}
void doingSomethingStop1()
{
doingSomethingStop(1);
}
void doingSomethingStop2()
{
doingSomethingStop(2);
}
void doingSomethingStop3()
{
doingSomethingStop(3);
}
void doingSomethingStop4()
{
doingSomethingStop(4);
}
void doingSomethingStop5()
{
doingSomethingStop(5);
}
void doingSomethingStop6()
{
doingSomethingStop(6);
}
void doingSomethingStop7()
{
doingSomethingStop(7);
}
//////////////////////////////////////////////////////
#if USE_COMPLEX_STRUCT
ISR_PWM_Data curISR_PWM_Data[] =
{
// pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Freq, PWM_DutyCycle, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
{ LED_BUILTIN, doingSomethingStart0, doingSomethingStop0, 1.0, 5.0, 0, 0, 0, 0 },
{ PIN_D0, doingSomethingStart1, doingSomethingStop1, 2.0, 10.0, 0, 0, 0, 0 },
{ PIN_D1, doingSomethingStart2, doingSomethingStop2, 3.0, 20.0, 0, 0, 0, 0 },
{ PIN_D2, doingSomethingStart3, doingSomethingStop3, 4.0, 25.0, 0, 0, 0, 0 },
{ PIN_D3, doingSomethingStart4, doingSomethingStop4, 5.0, 30.0, 0, 0, 0, 0 },
{ PIN_D4, doingSomethingStart5, doingSomethingStop5, 6.0, 35.0, 0, 0, 0, 0 },
{ PIN_D5, doingSomethingStart6, doingSomethingStop6, 7.0, 40.0, 0, 0, 0, 0 },
{ PIN_D6, doingSomethingStart7, doingSomethingStop7, 8.0, 45.0, 0, 0, 0, 0 },
};
void doingSomethingStart(int index)
{
unsigned long currentMicros = micros();
curISR_PWM_Data[index].deltaMicrosStart = currentMicros - curISR_PWM_Data[index].previousMicrosStart;
curISR_PWM_Data[index].previousMicrosStart = currentMicros;
}
void doingSomethingStop(int index)
{
unsigned long currentMicros = micros();
//curISR_PWM_Data[index].deltaMicrosStop = currentMicros - curISR_PWM_Data[index].previousMicrosStop;
// Count from start to stop PWM pulse
curISR_PWM_Data[index].deltaMicrosStop = currentMicros - curISR_PWM_Data[index].previousMicrosStart;
curISR_PWM_Data[index].previousMicrosStop = currentMicros;
}
#else // #if USE_COMPLEX_STRUCT
irqCallback irqCallbackStartFunc[] =
{
doingSomethingStart0, doingSomethingStart1, doingSomethingStart2, doingSomethingStart3,
doingSomethingStart4, doingSomethingStart5, doingSomethingStart6, doingSomethingStart7
};
irqCallback irqCallbackStopFunc[] =
{
doingSomethingStop0, doingSomethingStop1, doingSomethingStop2, doingSomethingStop3,
doingSomethingStop4, doingSomethingStop5, doingSomethingStop6, doingSomethingStop7
};
#endif // #if USE_COMPLEX_STRUCT
//////////////////////////////////////////////////////
#define SIMPLE_TIMER_MS 2000L
// Init SimpleTimer
SimpleTimer simpleTimer;
// Here is software Timer, you can do somewhat fancy stuffs without many issues.
// But always avoid
// 1. Long delay() it just doing nothing and pain-without-gain wasting CPU power.Plan and design your code / strategy ahead
// 2. Very long "do", "while", "for" loops without predetermined exit time.
void simpleTimerDoingSomething2s()
{
static unsigned long previousMicrosStart = startMicros;
unsigned long currMicros = micros();
Serial.print(F("SimpleTimer (us): ")); Serial.print(SIMPLE_TIMER_MS);
Serial.print(F(", us : ")); Serial.print(currMicros);
Serial.print(F(", Dus : ")); Serial.println(currMicros - previousMicrosStart);
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
{
#if USE_COMPLEX_STRUCT
Serial.print(F("PWM Channel : ")); Serial.print(i);
Serial.print(F(", prog Period (ms): "));
Serial.print(1000.0f / curISR_PWM_Data[i].PWM_Freq);
Serial.print(F(", actual (uS) : ")); Serial.print(curISR_PWM_Data[i].deltaMicrosStart);
Serial.print(F(", prog DutyCycle : "));
Serial.print(curISR_PWM_Data[i].PWM_DutyCycle);
Serial.print(F(", actual : ")); Serial.println((float) curISR_PWM_Data[i].deltaMicrosStop * 100.0f / curISR_PWM_Data[i].deltaMicrosStart);
//Serial.print(F(", actual deltaMicrosStop : ")); Serial.println(curISR_PWM_Data[i].deltaMicrosStop);
//Serial.print(F(", actual deltaMicrosStart : ")); Serial.println(curISR_PWM_Data[i].deltaMicrosStart);
#else
Serial.print(F("PWM Channel : ")); Serial.print(i);
Serial.print(1000.0f / PWM_Freq[i]);
Serial.print(F(", prog. Period (us): ")); Serial.print(PWM_Period[i]);
Serial.print(F(", actual : ")); Serial.print(deltaMicrosStart[i]);
Serial.print(F(", prog DutyCycle : "));
Serial.print(PWM_DutyCycle[i]);
Serial.print(F(", actual : ")); Serial.println( (float) deltaMicrosStop[i] * 100.0f / deltaMicrosStart[i]);
//Serial.print(F(", actual deltaMicrosStop : ")); Serial.println(deltaMicrosStop[i]);
//Serial.print(F(", actual deltaMicrosStart : ")); Serial.println(deltaMicrosStart[i]);
#endif
}
previousMicrosStart = currMicros;
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
Serial.print(F("\nStarting ISR_8_PWMs_Array_Complex on ")); Serial.println(BOARD_NAME);
Serial.println(ATMEGA_SLOW_PWM_VERSION);
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
// Timer0 is already used for micros(), millis(), delay(), etc and can't be used
// Select Timer 1-2
// Timer 2 is 8-bit timer, only for higher frequency
#if USING_HW_TIMER_INTERVAL_MS
/////////////////////////////////////////
#if USE_TIMER_1
ITimer1.init();
// Using ATmega324 with 16MHz CPU clock ,
// For 16-bit timer 1, set frequency from 0.2385 to some KHz
// For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz
if (ITimer1.attachInterruptInterval(HW_TIMER_INTERVAL_MS, TimerHandler))
{
Serial.print(F("Starting ITimer1 OK, micros() = ")); Serial.println(micros());
}
else
Serial.println(F("Can't set ITimer1. Select another freq. or timer"));
#elif USE_TIMER_2
ITimer2.init();
if (ITimer2.attachInterruptInterval(HW_TIMER_INTERVAL_MS, TimerHandler))
{
Serial.print(F("Starting ITimer2 OK, micros() = ")); Serial.println(micros());
}
else
Serial.println(F("Can't set ITimer2. Select another freq. or timer"));
#elif USE_TIMER_3
ITimer3.init();
if (ITimer3.attachInterruptInterval(HW_TIMER_INTERVAL_MS, TimerHandler))
{
Serial.print(F("Starting ITimer3 OK, micros() = ")); Serial.println(micros());
}
else
Serial.println(F("Can't set ITimer3. Select another freq. or timer"));
#endif
/////////////////////////////////////////
#else
/////////////////////////////////////////
#if USE_TIMER_1
ITimer1.init();
// Using ATmega324 with 16MHz CPU clock ,
// For 16-bit timer 1, set frequency from 0.2385 to some KHz
// For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz
if (ITimer1.attachInterrupt(HW_TIMER_INTERVAL_FREQ, TimerHandler))
{
Serial.print(F("Starting ITimer1 OK, micros() = ")); Serial.println(micros());
}
else
Serial.println(F("Can't set ITimer1. Select another freq. or timer"));
#elif USE_TIMER_2
ITimer2.init();
if (ITimer2.attachInterrupt(HW_TIMER_INTERVAL_FREQ, TimerHandler))
{
Serial.print(F("Starting ITimer2 OK, micros() = ")); Serial.println(micros());
}
else
Serial.println(F("Can't set ITimer2. Select another freq. or timer"));
#elif USE_TIMER_3
ITimer3.init();
if (ITimer3.attachInterrupt(HW_TIMER_INTERVAL_FREQ, TimerHandler))
{
Serial.print(F("Starting ITimer3 OK, micros() = ")); Serial.println(micros());
}
else
Serial.println(F("Can't set ITimer3. Select another freq. or timer"));
#endif
#endif
startMicros = micros();
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
// You can use up to 16 timer for each ISR_PWM
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
{
#if USE_COMPLEX_STRUCT
curISR_PWM_Data[i].previousMicrosStart = startMicros;
//ISR_PWM.setInterval(curISR_PWM_Data[i].PWM_Period, curISR_PWM_Data[i].irqCallbackStartFunc);
//void setPWM(uint32_t pin, float frequency, float dutycycle
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
// You can use this with PWM_Freq in Hz
ISR_PWM.setPWM(curISR_PWM_Data[i].PWM_Pin, curISR_PWM_Data[i].PWM_Freq, curISR_PWM_Data[i].PWM_DutyCycle,
curISR_PWM_Data[i].irqCallbackStartFunc, curISR_PWM_Data[i].irqCallbackStopFunc);
#else
previousMicrosStart[i] = micros();
// You can use this with PWM_Freq in Hz
ISR_PWM.setPWM(PWM_Pin[i], PWM_Freq[i], PWM_DutyCycle[i], irqCallbackStartFunc[i], irqCallbackStopFunc[i]);
#endif
}
// You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary.
simpleTimer.setInterval(SIMPLE_TIMER_MS, simpleTimerDoingSomething2s);
}
#define BLOCKING_TIME_MS 10000L
void loop()
{
// This unadvised blocking task is used to demonstrate the blocking effects onto the execution and accuracy to Software timer
// You see the time elapse of ISR_PWM still accurate, whereas very unaccurate for Software Timer
// The time elapse for 2000ms software timer now becomes 3000ms (BLOCKING_TIME_MS)
// While that of ISR_PWM is still prefect.
delay(BLOCKING_TIME_MS);
// You need this Software timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary
// You don't need to and never call ISR_PWM.run() here in the loop(). It's already handled by ISR timer.
simpleTimer.run();
}



Debug

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

Troubleshooting

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.



Issues

Submit issues to: ATmega_Slow_PWM issues


TO DO

  1. Search for bug and improvement.
  2. Similar features for remaining Arduino boards

DONE

  1. Basic hardware multi-channel PWM for ATmega164(A/P), ATmega324(A/P/PA/PB), ATmega644(A/P), ATmega1284(P) using MightyCore
  2. Add Table of Contents
  3. Add functions to modify PWM settings on-the-fly
  4. Fix multiple-definitions linker error. Drop src_cpp and src_h directories
  5. Add example multiFileProject to demo for multiple-file project
  6. Improve accuracy by using float, instead of uint32_t for dutycycle
  7. Optimize library code by using reference-passing instead of value-passing
  8. DutyCycle to be optionally updated at the end current PWM period instead of immediately.
  9. Display informational warning only when _PWM_LOGLEVEL_ > 3


Contributions and Thanks

Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.


Contributing

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

License

  • The library is licensed under MIT

Copyright

Copyright (c) 2022- Khoi Hoang

About

This library enables you to use ISR-based PWM channels on AVR ATmega164, ATmega324, ATmega644, ATmega1284 with MCUdude MightyCore, to create and output PWM any GPIO pin. It now supports 16 ISR-based PWM channels, while consuming only 1 Hardware Timer. PWM channel interval can be very long (ulong microsecs / millisecs). The most important feature…

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published