/* * ScopeSignalGenerator.ino * 2BitOrNot2Bit — Arduino as a known-frequency source for testing a cheap USB scope. * * Board: Arduino Uno or Nano, 16 MHz ATmega328P (F_CPU = 16000000UL). * Output: pin D9 (OC1A). A square wave at 50 % duty, generated by Timer1 in CTC * mode, toggling OC1A on every compare match. * * This is a cleaned rewrite of my 2020 bench sketch (LED_PWM.ino). That sketch * flipped Timer0 and Timer2 depending on the frequency, which also breaks * millis() and delay() whenever Timer0 is used. Here every frequency uses * Timer1 only, so millis() and delay() keep working, and you can switch * frequencies from the serial monitor without re-flashing. * * -------------------------------------------------------------------------- * Timer math (16 MHz ATmega328P) * -------------------------------------------------------------------------- * * Timer1 is a 16-bit counter. In CTC mode (WGM13:0 = 0b0100) it counts from 0 * to OCR1A inclusive, then starts again. With COM1A1:0 = 0b01, pin D9 (OC1A) * toggles on every compare match (that is, once per timer period). * * Two toggles make one output cycle, so: * * f_out = F_CPU / (2 * N * (1 + OCR1A)) * * N is the prescaler. This sketch always uses N = 1 (CS12:0 = 0b001), so: * * f_out = 16 000 000 / (2 * (1 + OCR1A)) * = 8 000 000 / (1 + OCR1A) * * Solving for the register: OCR1A = (8 000 000 / f_out) - 1 * * The six frequencies below are the ones I actually captured in May 2020 * with OpenHantek6022 3.1.0 on a Hantek 6022BE. They are not rounded * marketing names: "7 kHz" in the filename is exactly 7812.5 Hz, and * "62 kHz" is exactly 62.5 kHz. Both come from the familiar 8-bit Fast PWM * formula F_CPU / (N * 256), which is what the 2020 sketch used on Timer0. * They also fall out of the Timer1 CTC formula with the OCR1A values here. * * Target OCR1A Exact f_out * ------------ ----- --------------------- * 7.8 kHz 1023 16e6 / (2 * 1024) = 7812.5 Hz * 62.5 kHz 127 16e6 / (2 * 128) = 62500 Hz * 100 kHz 79 16e6 / (2 * 80) = 100000 Hz * 727 kHz 10 16e6 / (2 * 11) = 727272.72... Hz (16e6/22) * 1 MHz 7 16e6 / (2 * 8) = 1000000 Hz * 4 MHz 1 16e6 / (2 * 2) = 4000000 Hz * * -------------------------------------------------------------------------- * How the 2020 captures were generated (for the record) * -------------------------------------------------------------------------- * * The OpenHantek screenshots were taken with LED_PWM.ino, not with this file. * The frequencies match because the register math is the same; the pins and * the timers were different: * * 7.8 kHz / 62.5 kHz — Timer0, Fast PWM, TOP = 0xFF (mode 3), pin D6 (OC0A), * analogWrite(6, 127), prescaler 8 or 1: * f = F_CPU / (N * 256) * N = 8 -> 7812.5 Hz (filename 7khz.PNG) * N = 1 -> 62500 Hz (filename 62khz.PNG, 62khz_with_code.PNG) * Changing Timer0 breaks millis() and delay(). The side-by-side * screenshot 62khz_with_code.PNG shows this exact Timer0 /1 path * running on an Arduino Nano (COM7), Arduino IDE 1.8.12. * * 100 kHz — Timer2, Fast PWM mode 7 (TOP = OCR2A), pin D3 (OC2B): * OCR2A = 159, prescaler 1 * f = F_CPU / (N * (1 + OCR2A)) = 16e6 / 160 = 100000 Hz * The 2020 capture is 100 kHz, but the duty-cycle badge reads 32 % * (3.21 µs and 6.79 µs), not 50 %. This sketch outputs 50 % on D9. * * 727 kHz / 1 MHz — Timer2, Fast PWM mode 7, toggle OC2A (pin D11): * f = F_CPU / (2 * N * (1 + OCR2A)) * OCR2A = 10 -> 16e6 / 22 = 727272.72... Hz * OCR2A = 7 -> 16e6 / 16 = 1000000 Hz * * 4 MHz — Timer0, CTC mode 2, toggle OC0A (pin D6), OCR0A = 1: * f = F_CPU / (2 * N * (1 + OCR0A)) = 16e6 / 4 = 4000000 Hz * Again, this touches Timer0. The equivalent here is OCR1A = 1 on D9. * * Connect a scope probe to D9 and GND, open the serial monitor at 115200 baud, * and type 1..6 (or 0 to stop). */ #include const uint8_t kOutPin = 9; // OC1A on Uno / Nano const unsigned long kBaud = 115200; struct Tone { const char *label; uint16_t ocr1a; // Timer1 CTC TOP, prescaler 1, toggle OC1A const char *exact; // exact frequency as text, for the serial printout const char *filename; // 2020 OpenHantek capture this frequency matches }; // f = 16000000 / (2 * (1 + ocr1a)) const Tone kTones[] = { {"7.8 kHz", 1023, "7812.5 Hz", "7khz.PNG"}, {"62.5 kHz", 127, "62500 Hz", "62khz.PNG"}, {"100 kHz", 79, "100000 Hz", "100khz.PNG"}, {"727 kHz", 10, "727272.73 Hz (16e6/22)","727khz.PNG"}, {"1 MHz", 7, "1000000 Hz", "1mhz.PNG"}, {"4 MHz", 1, "4000000 Hz", "4mhz.PNG"}, }; const uint8_t kToneCount = sizeof(kTones) / sizeof(kTones[0]); int8_t gCurrent = -1; // -1 = output stopped, pin held low static void stopTimer1() { TCCR1A = 0; TCCR1B = 0; TIMSK1 = 0; TCNT1 = 0; OCR1A = 0; digitalWrite(kOutPin, LOW); pinMode(kOutPin, OUTPUT); } // CTC, toggle OC1A (D9) on compare match, prescaler = 1, no Timer1 interrupts. static void startTimer1Toggle(uint16_t ocr1a) { stopTimer1(); pinMode(kOutPin, OUTPUT); TCNT1 = 0; OCR1A = ocr1a; // COM1A1:0 = 01 -> toggle OC1A on compare match // WGM13:0 = 0100 -> CTC, TOP = OCR1A // CS12:0 = 001 -> clk/1 TCCR1A = _BV(COM1A0); TCCR1B = _BV(WGM12) | _BV(CS10); } static void printMenu() { Serial.println(); Serial.println(F("Scope signal generator | pin D9 (OC1A) | 16 MHz ATmega328P")); Serial.println(F("f = 16000000 / (2 * (1 + OCR1A)) Timer1 CTC, toggle, 50% duty")); Serial.println(F("--------------------------------------------------------------")); for (uint8_t i = 0; i < kToneCount; i++) { Serial.print(F(" ")); Serial.print(i + 1); Serial.print(F(" ")); Serial.print(kTones[i].label); Serial.print(F(" OCR1A=")); Serial.print(kTones[i].ocr1a); Serial.print(F(" exact ")); Serial.print(kTones[i].exact); Serial.println(); } Serial.println(F(" 0 stop (pin D9 low)")); Serial.println(F(" m print this menu")); Serial.println(F("Type a key and press Enter.")); } static void selectTone(int8_t index) { if (index < 0) { stopTimer1(); gCurrent = -1; Serial.println(F("Stopped. D9 is low.")); return; } if (index >= (int8_t)kToneCount) { return; } const Tone &t = kTones[index]; startTimer1Toggle(t.ocr1a); gCurrent = index; Serial.print(F("Now outputting ")); Serial.print(t.label); Serial.print(F(" (exact ")); Serial.print(t.exact); Serial.print(F(", OCR1A=")); Serial.print(t.ocr1a); Serial.print(F(") on D9. 2020 capture: ")); Serial.println(t.filename); } void setup() { stopTimer1(); Serial.begin(kBaud); while (!Serial) { ; // wait for the USB serial port on a native-USB board; Uno/Nano continue } printMenu(); } void loop() { if (!Serial.available()) { return; } const int c = Serial.read(); if (c == '\r' || c == '\n' || c == ' ') { return; } if (c == 'm' || c == 'M' || c == '?') { printMenu(); return; } if (c == '0') { selectTone(-1); return; } if (c >= '1' && c < '1' + (int)kToneCount) { selectTone((int8_t)(c - '1')); return; } Serial.println(F("Unknown key. Type m for the menu.")); }