スイッチサイエンスで販売されている静電容量式のレインセンサー基板を使って省電力なレインセンサーを作ってみた。
以前に公開した光センサーのLEDの代わりにレインセンサー基板を接続しただけだ。
AVR0/1シリーズで省電力な光センサーを作ってみた。(改良版)
レインセンサーの静電容量(乾燥時約170pF程度)に充電した後に放電を開始し閾値(1.1V)まで電圧が下がるまでの時間を計測する。センサー表面が乾燥時より長い時間のパルスが出力されたら雨が降ってきたと判断することができる。出力は安定しているので誤検出の心配もなさそうだ。
ATtiny202のAC入力インピーダンスはかなり高そう(数十GΩ以上)なので数pF程度まで図れるかもしれない。
【ファームウェア(Microchip Studio)】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
/* main.cpp - Low Power LED Optical Sensor Firmware for Microchip AVR0/1 Series Copyright (c) 2023 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ //#define F_CPU 20000000 // 20MHz (OSC20M) //#define F_CPU 1250000 // 1.25MHz (OSC20M) #define F_CPU 32768 // 32.786KHz (OSCULP32K) #include <avr/io.h> #include <avr/cpufunc.h> #include <avr/interrupt.h> #include <avr/sleep.h> #include <util/delay.h> #include <string.h> #define INTERVAL_TIME 1000 // ms #define AC_OUT_MIN_WIDTH 1000 // us #if defined(__AVR_ATtiny202__) #define PIN_LED_CATHODE PIN7_bp // PA7 #define PIN_LED_ANODE PIN6_bp // PA6 #define PIN_OUTPUT PIN2_bp // PA2 #define PIN_AC_OUT PIN3_bp // PA3 #define PIN_AC_OUT_CTRL PORTA.PIN3CTRL #elif defined(__AVR_ATtiny1614__) #define PIN_LED_CATHODE PIN7_bp // PA7 // #define PIN_LED_ANODE PIN6_bp // PA6 #define PIN_LED_ANODE PIN4_bp // PA4 (for test board) #define PIN_OUTPUT PIN2_bp // PA2 #define PIN_AC_OUT PIN5_bp // PA5 #define PIN_AC_OUT_CTRL PORTA.PIN5CTRL #endif #if !defined(AC_LPMODE_bm) #define AC_LPMODE_bm 0 #endif #define ATOMIC(...) \ do { \ uint8_t sreg_save = SREG; \ cli(); \ __VA_ARGS__; \ SREG = sreg_save; \ } while (0) #define SBI(port, bit) \ do { \ if (_SFR_IO_REG_P(port)) \ asm volatile ("sbi %0, %1" :: "I" (_SFR_IO_ADDR(port)), "I" (bit)); \ else \ ATOMIC(port |= _BV(bit)); \ } while (0) #define CBI(port, bit) \ do { \ if (_SFR_IO_REG_P(port)) \ asm volatile ("cbi %0, %1" :: "I" (_SFR_IO_ADDR(port)), "I" (bit)); \ else \ ATOMIC(port &= ~_BV(bit)); \ } while (0) ISR(RTC_CNT_vect) { RTC.INTFLAGS = RTC_OVF_bm; } ISR(PORTA_PORT_vect) { SBI(VPORTA_INTFLAGS, PIN_AC_OUT); // Output LOW CBI(VPORTA_OUT, PIN_OUTPUT); } void clock_select(void) { #if F_CPU == 32768 _PROTECTED_WRITE(CLKCTRL_MCLKCTRLB, 0); _PROTECTED_WRITE(CLKCTRL_MCLKCTRLA, CLKCTRL_CLKSEL_OSCULP32K_gc); #elif F_CPU == 1250000 _PROTECTED_WRITE(CLKCTRL_MCLKCTRLB, CLKCTRL_PDIV_16X_gc | CLKCTRL_PEN_bm); #elif F_CPU == 20000000 _PROTECTED_WRITE(CLKCTRL_MCLKCTRLB, 0); #endif } void disable_inputs(void) { #if defined(PORTA) memset((void *)&PORTA.PIN0CTRL, PORT_ISC_INPUT_DISABLE_gc, 8); #endif #if defined(PORTB) memset((void *)&PORTB.PIN0CTRL, PORT_ISC_INPUT_DISABLE_gc, 8); #endif #if defined(PORTC) memset((void *)&PORTC.PIN0CTRL, PORT_ISC_INPUT_DISABLE_gc, 8); #endif #if defined(PORTD) memset((void *)&PORTD.PIN0CTRL, PORT_ISC_INPUT_DISABLE_gc, 8); #endif } int main(void) { // Disable all GPIO inputs disable_inputs(); // PORT Configuration SBI(VPORTA_OUT, PIN_LED_CATHODE); SBI(VPORTA_DIR, PIN_LED_ANODE); SBI(VPORTA_DIR, PIN_OUTPUT); // VREF Configuration VREF.CTRLA = VREF_DAC0REFSEL_1V1_gc; // AC0 Configuration AC0.MUXCTRLA = AC_MUXPOS_PIN0_gc | AC_MUXNEG_VREF_gc; AC0.CTRLA = AC_RUNSTDBY_bm | AC_OUTEN_bm | AC_LPMODE_bm | AC_HYSMODE_OFF_gc | AC_ENABLE_bm; // RTC Configuration while (RTC.STATUS) continue; RTC.PER = (INTERVAL_TIME * 32768UL / 1000) - 1; RTC.INTCTRL = RTC_OVF_bm; RTC.CLKSEL = RTC_CLKSEL_INT32K_gc; RTC.CTRLA = RTC_RUNSTDBY_bm | RTC_PRESCALER_DIV1_gc | RTC_RTCEN_bm; // Sleep Mode Configuration set_sleep_mode(SLEEP_MODE_STANDBY); sleep_enable(); // Clock Select clock_select(); // Main Loop while (1) { sei(); // Minimum LOW Pulse Width _delay_us(AC_OUT_MIN_WIDTH); // Interval Sleep sleep_cpu(); // Output HIGH SBI(VPORTA_OUT, PIN_OUTPUT); // Charge to LED Parasitic Capacitance SBI(VPORTA_DIR, PIN_LED_CATHODE); // Minimum HIGH Pulse Width _delay_us(AC_OUT_MIN_WIDTH); // AC_OUT Pin Interrupt Enable PIN_AC_OUT_CTRL = PORT_ISC_BOTHEDGES_gc; cli(); // Discharge from LED Parasitic Capacitance CBI(VPORTA_DIR, PIN_LED_CATHODE); // Wait AC_OUT Pin Interrupt do { sei(); sleep_cpu(); cli(); } while (VPORTA_OUT & _BV(PIN_OUTPUT)); // AC_OUT Pin Interrupt Disable PIN_AC_OUT_CTRL = PORT_ISC_INTDISABLE_gc; SBI(VPORTA_INTFLAGS, PIN_AC_OUT); } } |