Programming for Robotics with Arduino C++ 8, Challenge 1 solution (which frequencies can a timer make?) Idea: plan_timer() from timer_calc.h is called for every whole frequency from 1 to 20 000 Hz on Timer1 (prescalers 1, 8, 64, 256, 1024; OCR up to 65 535) and on Timer2 (prescalers 1, 8, 32, 64, 128, 256, 1024; OCR up to 255). The program counts how many it can reach, how many within 1000 ppm and 100 ppm, and records the lowest reachable frequency and the worst error. CODE: // x1.cpp -- Challenge 1: which frequencies can each timer hit? (host build, 16 MHz, CTC mode) // For every whole frequency from 1 Hz to 20 000 Hz, plan_timer() finds the prescaler and OCR with the smallest error on Timer1 (16 bit) and on Timer2 (8 bit). // Reported: how many of the 20 000 frequencies each timer can produce within 100 ppm, within 1000 ppm, and at all; the lowest frequency each can reach; the worst error among those it can reach. #include #include "timer_calc.h" int main() { const uint16_t P1[] = {1, 8, 64, 256, 1024}; const uint16_t P2[] = {1, 8, 32, 64, 128, 256, 1024}; int ok100[2] = {0, 0}, ok1000[2] = {0, 0}, any[2] = {0, 0}; double worst[2] = {0, 0}; int lowest[2] = {0, 0}, first_bad[2] = {0, 0}; for (int f = 1; f <= 20000; ++f) { TimerPlan p[2] = {plan_timer(f, P1, 5, 16), plan_timer(f, P2, 7, 8)}; for (int t = 0; t < 2; ++t) { if (!p[t].ok) continue; ++any[t]; if (!lowest[t]) lowest[t] = f; if (p[t].error_ppm <= 100) ++ok100[t]; else if (!first_bad[t]) first_bad[t] = f; if (p[t].error_ppm <= 1000) ++ok1000[t]; if (p[t].error_ppm > worst[t]) worst[t] = p[t].error_ppm; } } std::printf("whole frequencies 1..20000 Hz that each timer can produce (CTC, 16 MHz)\n"); std::printf(" timer reachable within 1000 ppm within 100 ppm lowest whole Hz reachable worst error (ppm) first Hz that misses 100 ppm\n"); const char* names[2] = {"Timer1 (16 bit)", "Timer2 (8 bit) "}; for (int t = 0; t < 2; ++t) std::printf(" %s %9d %15d %14d %25d %17.0f %28d\n", names[t], any[t], ok1000[t], ok100[t], lowest[t], worst[t], first_bad[t]); std::printf("lowest frequency in theory: Timer1 %.3f Hz (prescaler 1024, OCR 65535), Timer2 %.3f Hz (prescaler 1024, OCR 255)\n", 16e6 / (1024.0 * 65536), 16e6 / (1024.0 * 256)); return 0; } END CODE OUTPUT: whole frequencies 1..20000 Hz that each timer can produce (CTC, 16 MHz) timer reachable within 1000 ppm within 100 ppm lowest whole Hz reachable worst error (ppm) first Hz that misses 100 ppm Timer1 (16 bit) 20000 20000 9068 1 617 3232 Timer2 (8 bit) 19940 5793 595 61 7804 61 lowest frequency in theory: Timer1 0.238 Hz (prescaler 1024, OCR 65535), Timer2 61.035 Hz (prescaler 1024, OCR 255) END OUTPUT Reading the output. Timer1 reaches all 20 000 frequencies and every one within 1000 ppm (worst case 617 ppm). Timer2 reaches 19 940: nothing below 61 Hz, because its slowest setting is a prescaler of 1024 and an OCR of 255, 16 MHz / (1024 x 256) = 61.04 Hz. Within 100 ppm Timer1 reaches 9068 frequencies and Timer2 only 595. The rule behind these numbers is rounding. The compare value is a whole number of timer counts, so the period is wrong by up to half a count out of N counts, an error of about 1 / (2 N) in the frequency. At 20 000 Hz and a prescaler of 1, N is 800 and the worst error is 625 ppm (the 617 printed). For an error under 100 ppm, N must be at least 5000, which on Timer1 means a frequency of at most 16 MHz / 5000 = 3200 Hz: the first frequency that misses 100 ppm is 3232 Hz, as printed. Timer2 can count only up to 256, so its best case is 1 / 512 = 1950 ppm unless the frequency happens to divide evenly; its 595 frequencies within 100 ppm are the exact divisors, and its worst error is 7804 ppm. Practical consequences: use Timer1 for anything slow or exact; a Timer2 tick is fine only for frequencies that divide 16 MHz evenly (1 kHz does: prescaler 64, OCR 249). A frequency that matters (a 38 kHz carrier, a servo-like pulse) is checked with the calculator, not assumed. Check on method: this is arithmetic on the datasheet's formula for CTC mode; the real error also contains the board's clock error, which I did not measure, and for phase-correct PWM modes the formula is different.