WYSIWYG Arduino PWM Code Generator
If you've ever struggled with Arduino PWM or Timer coding, I've got good news. Runtime Micro is offering a free Windows Desktop app. that lets you generate C++ code for Arduino Timers 1, 2, 3, 4 and 5. This works for MCU card types of Nano, Uno and Mega2560!
Using selections you make (Clock, Timer and Mode) along with your desired Frequency, the application calculates exact Timer values and needed code.
It even shows a depiction of what your Arduino PWM will look like! View your chosen waveforms in the built-in display. Those same waveforms will appear on an oscilloscope hooked to your MCU output port pins (after you upload the code of course).
Yes, I'm claiming its WYSIWYG. What shows on the display will also show on your O-Scope.
Immediate PWM Feedback
Notice those vertical slider controls on the above image's upper right side? They adjust PWM Duty-Cycle percent. At lower right the horizontal sliders select Output-Compare modes. As you make changes, the display updates to show what you ordered-up.
In just moments, you can dial up a desired pattern and watch as Arduino compatible code is generated. You then copy and paste the code into your Sketch, Upload and your Arduino PWM is ready to run!
Place your scope probes on the port pins to see your desired waveforms humming along at the Frequency, Period and Duty cycle you dialed up. Hardware PWM is easy this way!
Sometimes you don't care about waveforms. Instead you need a precisely timed Interrupt to synchronize your code. No problem -- just check the Add-Interrupt checkbox and hit Calculate. A line of Interrupt code is created for any of 5 Timer choices. An Overflow-Interrupt happens each time your Timer reaches its TOP count (or 1 clock after).
ISR Prototype with Vector
Hidden as a comment to the right of your Interrupt code-line, is a C++ Prototype of the vectored ISR your interrupt will call. If you don't already have that call coded in your Sketch, it's a handy reference for copy and paste.
You'll likely place this ISR code after any #includes but ahead of Setup(). All 5 Timers are handled. Only the Overflow type Interrupt is included in RTM_TimerCalc output.
Arduino PWM Modes
RTM_TimerCalc uses a small subset of the many Timer Modes available. You'll get adjustable frequency Phase and Fast PWM for both 8 and 16 bit Timers. And you get one CTC mode for the 16 bit Timers. That adds up to 2 (8-bit) and 3 (16-bit) Modes -- across a selection of 5 possible Timers.
Pins, Pinmodes and Comments
RTM_TimerCalc generates PinMode code for each output waveform you choose. It adds comment-info identifying what compare-output (a, b or c) is associated with which pin. You won't need to dive into a datasheet to work out what pin# needs to be set up as OUTPUT. This is true for pins on Nano, Uno and Maga2560 -- across Timers 1 thru 5.
Prompts And Warnings
To ensure a smooth experience, the App. prompts you quickly thru the easy steps of choosing Clock, Timer and Waveform mode. Click the Calculate button and you'll see the resulting code and an info message. Edit a new Frequency or Time-Interval and press Calculate -- you're quickly ready to copy-paste to your IDE.
If you made a mistake, you'll hear a bleeping sound. Be sure you check the Message-Window for hints about what's going wrong. You may need to change to a different Timer or use a different frequency -- it happens.
Sometimes your inputs can NOT be made to work. And sometimes your desired output is far away from what you requested. Read the Message Window and consider accordingly. There are limits on what the hardware can do.
Frequency or Interval
You start off with a default frequency of 1000Hz. Its easy to click a dropdown to change from Hz to mSec (frequency or time interval). And its up to you to edit that 1000 Hz value to whatever you need. Then hit Calculate.
If you hear no bleeps and see no warnings, you're good. Check the Frequency and Interval outputs at the bottom of the Waveform Display. They should match what you expected. If not, go back and check your inputs.
Yes, its likely there are bugs in RTM_TimerCalc. No code is error free and I'm too long in the tooth to think otherwise. Click the blue link at bottom of the App to navigate to Runtime Micro's Forum for RTM_TimerCalc. Log-in and let us know what you encountered. Support may be slow but its free.
Limitations You Should Pay Attention To
If you ask for frequencies greater than 1 MHz, you may encounter warning messages about small counter N values being not recommended. But there's more to know.
One not-so-obvious problem is when a counter divide-N value is below 100, you no longer have a Duty-Cycle resolution of 1 percent or better! The first release did not address this failing. Version 1.20 and later gives you fair warning about low PWM resolution and low Phase resolution in mode-12 CTC.
For example, if I ask for a 4-MHz frequency in Fast-PWM, I lose a lot of PWM resolution. In the first version, Duty-Cycle was not predictable using the interactive WYSIWYG display! But in version 1.20, the WYSIWYG sliders adapt to the limited step-size for PWM or Phase Shifted square waves. And -- you're given an audible and text warning message.
Mathematically speaking, we must realize a counter divide-N value below 100 will result in Duty-Cycle step sizes greater than 1%. It the divide-N value is less that 180, we'll get Phase-Shift step sizes greater than 1 degree. The good news is you are warned and forced to use only legal slider settings (in version 1.20).
As of this edit (6-May, 2020), no one called me out on the failings of v1.01. Hopefully, these issues were resolved in the latest release!
Even if you're brand new to Arduino (Nano, Uno or Mega2560), you can quickly generate working PWM or Interrupt C++ code. The Interactive Display shows output waveforms and Duty-Cycle choices with summary Frequency and Interval info at bottom. This is an easy way to develop a viable PWM plan for your Arduino project.
INTRO VIDEO of the program here: https://youtu.be/Slhe3Ud6YBo
Head on over to the DOWNLOAD section of this site and grab your copy, today!