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 the 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 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 does not address this failing. Therefore, under these circumstances, the WYSIWYG part of RTM_TimerCalc is lying to you.
For example, if I ask for a 4-MHz frequency in Fast-PWM, it seems to generate working code. But the duty cycle is no longer predictable using the interactive WYSIWYG display! Mathematically, we must realize a counter divide-N value of 4-1=3. Therefore we can only apply output-compare register values of 1, 2 and 3. This corresponds to duty cycles of 25%, 50% and 75% -- that's it! You can't realize any other possible useful duty-cycle! The hardware has limits and if you push hard, you'll run into them.
As of this edit (19-Feb, 2020), no one has called me on this. Nonetheless, be warned that using divider values less than 100 will limit your duty-cycle resolution. In an upcoming release, I'll add some better warnings to help folks avoid this problem.
I may even modify the WYSIWYG control to make this problem more obvious.
Even if you're brand new to Arduino, you can quickly generate -correct- Arduino PWM (for N-dividers above 99) and Interrupt source code for your IDE. The Interactive Display shows output waveforms and duty-cycle choices with summary Frequency and Interval info at bottom. This is an efficient way to develop a PWM plan that works for your 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!