Timer/Counter Wins

I performed surgery on the Arduino Library. Yes, it is a risky task. I had to modify five source files, but the changes are conditionally compiled.

I had previously added another “board” that is the Arduino Nano Every with a 20MHz clock so I could easily select it.

Now I’ve gotten rid of the ATMEGA328 emulation and have choice for “Better Operation”. I will describe that here.

I realized that I didn’t want that /64 timer prescaler, so Better Operation uses the /8 prescaler. But then I had to make changes in the Arduino Library to compensate. changes were made to timers.h, variant.c, wiring.c, tone.cpp and Server.cpp all for the megaavr or Arduino Nano Every.

Faster is better for PWM (AKA analogWrite function). And dual slope is nicer because it eliminates the phase shift when changing duty cycles. The original Nano/Uno 4 channels running at 490Hz with dual slope and two running at 977 Hz with single slope. The unmodified Nano Every has five channels, all running at 977 Hz with single slope. They did fix the library so that the argument for analogWrite now has the range 0 through 256, with the duty cycle being (N/256)%.

The Better Operation Nano Every has three channels at 3.91 kHz with dual slope and two channels at 7.81 kHz with single slope.

The original Nano/Uno tone function went down to 31 Hz. The Better Operation Nano Every goes down to 16 Hz.

Unlike the original Nano Every, Nano, or Uno, the micros function now has a 1µs resolution instead of a 4µs resolution. The operation of the Servo library has been restored to having a 0.5µs pulse width resolution from the original Nano Every 4µs pulse width resolution.

And as a final touch, the Analog to Digital Converter (analogRead function) runs with a 1MHz clock instead of a 125 kHz clock, or 8 times faster!

Using the 20 MHz system clock gives a 25% boost to all the frequencies and pulse widths and periods mentioned above.

It’s been a great day!