/* Tiny Continuity Tester - see http://www.technoblogy.com/show?5DEV David Johnson-Davies - www.technoblogy.com - 27th October 2025 ATtiny202 @ 1 MHz (internal oscillator; BOD disabled) CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ #include #include const int LED = PIN_PA1; const int Reference = PIN_PA7; // AINP0 const int Probe = PIN_PA6; // AINN0 const int Speaker = PIN_PA3; const int Speaker2 = PIN_PA2; const unsigned long Timeout = (unsigned long)60*1000; // One minute to sleep volatile unsigned long Time; // Pin change interrupt service routine - resets sleep timer ISR (PORTA_PORT_vect) { Time = millis(); PORTA.INTFLAGS = PIN6_bm; // Clear interrupt flag } void SetupBeep () { PORTMUX.CTRLC = 0; PORTA.DIRSET = PIN3_bm; // Make WO0 an output TCA0.SINGLE.CTRLD = 0; // Normal mode TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc; // Clock divided by 1 TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | TCA_SINGLE_WGMODE_FRQ_gc; TCA0.SINGLE.CMP0 = 479; // Gives 1042Hz (C6) } void SetupEvents () { PORTA.DIRSET = PIN2_bm; // Make PA2 an output PORTMUX.CTRLA = PORTMUX_EVOUT0_bm; // Enable EVOUT0 EVSYS.ASYNCCH0 = EVSYS_ASYNCCH0_PORTA_PIN3_gc; // PA3 to Channel0 EVSYS.ASYNCUSER8 = EVSYS_ASYNCUSER8_ASYNCCH0_gc; // Channel0 to EVOUT0 PORTA.PIN2CTRL = PORT_INVEN_bm; // Invert PA2 } void Beep () { TCA0.SINGLE.CTRLA = TCA0.SINGLE.CTRLA | TCA_SINGLE_ENABLE_bm; PORTA.OUTSET = PIN1_bm; // LED on } void NoBeep () { TCA0.SINGLE.CTRLA = TCA0.SINGLE.CTRLA & ~TCA_SINGLE_ENABLE_bm; PORTA.OUTCLR = PIN1_bm; // LED off } void setup () { PORTA.DIRSET = PIN3_bm | PIN2_bm | PIN1_bm; // LED, Speaker, Speaker2 // Probe and reference PORTA.PIN7CTRL = PORT_PULLUPEN_bm; // Reference input PORTA.PIN6CTRL = PORT_PULLUPEN_bm | PORT_ISC_FALLING_gc; // Interrupt falling edge // Analogue Comparator AC0.CTRLA = AC_ENABLE_bm; // Enable Analogue Comparator // Power saving set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Start running Time = millis(); SetupBeep (); // Start beep oscillator SetupEvents(); } void loop() { bool Continuity = AC0.STATUS & AC_STATE_bm; if (Continuity) Beep(); else NoBeep(); // Go to sleep? if (millis() - Time > Timeout) { PORTA.PIN7CTRL = 0; // Pullup off saves power NoBeep(); // LED and beep off sleep_enable(); sleep_cpu(); // Carry on here when we wake up PORTA.PIN7CTRL = PORT_PULLUPEN_bm; // Reference pullup on } }