2 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the
15 // * All advertising materials mentioning features or use of this
16 // software must display the following acknowledgement: This product
17 // includes software developed by tm3d.de and its contributors.
18 // * Neither the name of tm3d.de nor the names of its contributors may
19 // be used to endorse or promote products derived from this software
20 // without specific prior written permission.
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //#define _EEPROM_SAVE_
38 #define F_CPU 8000000UL
40 #include <avr/interrupt.h>
41 #include <util/delay.h>
43 #include <avr/sleep.h>
46 #if defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
48 #if defined(_4_COUNTERS_)&&defined(_EEPROM_SAVE_)
49 #error on ATtinyX5 you can not enable four counters and EEPROM save
56 uint8_t owid[8]={0x1D, 0xA2, 0xD9, 0x84, 0x00, 0x26, 0x02, 0x5C};/**/
57 uint8_t config_info[16]={0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
62 extern uint8_t gcontrol;
63 extern uint8_t reset_indicator;
66 volatile uint8_t wdcounter;
72 volatile uint8_t bytes[45];
76 uint8_t scratch[32];//3
77 uint32_t counter; //35
84 volatile uint8_t lastcps;
90 volatile counters_t counters;
92 volatile uint8_t istat;
94 #if defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
95 #define PCINT_VECTOR PCINT0_vect
98 #define PIN_CH2 (1<<PINB3)
99 #define PIN_CH3 (1<<PINB4)
100 #define PIN_CH0 (1<<PINB0)
101 #define PIN_CH1 (1<<PINB1)
103 #define PIN_EE (1<<PINB0)
104 #define TEST_TIMER ((TIMSK & (1<<TOIE0))==0)
108 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
109 #define PCINT_VECTOR PCINT0_vect
112 #define PIN_CH2 (1<<PINA4)
113 #define PIN_CH3 (1<<PINA5)
114 #define PIN_CH0 (1<<PINA6)
115 #define PIN_CH1 (1<<PINA7)
116 #define PORT_EE PINA //WARNING have to be the same PORT like PINREG because of istat
117 #define PIN_EE (1<<PINA0)
118 #define TEST_TIMER ((TIMSK0 & (1<<TOIE0))==0)
124 if (((PIN_REG&PIN_CH2)==0)&&((istat&PIN_CH2)==PIN_CH2)) { counters.c32[2]++; }
125 if (((PIN_REG&PIN_CH3)==0)&&((istat&PIN_CH3)==PIN_CH3)) { counters.c32[3]++; }
127 if (((PIN_REG&PIN_CH0)==0)&&((istat&PIN_CH0)==PIN_CH0)) { counters.c32[0]++; }
128 if (((PIN_REG&PIN_CH1)==0)&&((istat&PIN_CH1)==PIN_CH1)) { counters.c32[1]++; }
132 if (((PORT_EE&PIN_EE)==0)&&((istat&PIN_EE)==PIN_EE)) {
133 //PORTB&=~(1<<PINB1);
136 counters.c32[0]++;//Count the save cycles
138 for(uint8_t i=0;i<16;i++) {
140 while(EECR & (1<<EEPE));
141 EECR = (0<<EEPM1)|(0<<EEPM0);
143 EEDR = counters.c8[addr];
154 PRR|=(1<<PRUSI)|(1<<PRADC); //Switch off usi and adc for save Power
162 ACSR|=(1<<ACD); //Disable Comparator
163 ADCSRB|=(1<<ACME); //Disable Analog multiplexer
164 MCUCR &=~(1<<PUD); //All Pins Pullup...
167 #if defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
168 PORTB|=(1<<PINB5) //Reset
179 PCMSK=(1<<PCINT3)|(1<<PCINT4)
181 |(1<<PCINT0)|(1<<PCINT1)
187 PORTB&=~PIN_EE; //Spannungsueberpruefung
189 DDRB &=~((1<<PINB3)|(1<<PINB4)
191 |(1<<PINB0)|(1<<PINB1)
198 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
200 PORTB|=0xFF-(1<<PINB2); //Make PullUp an all Pins but not OW_PIN
202 if (PINB&(1<<PINB0)) {
203 } else {//SW set, PINB0 =0 no pullup, clear PORT of ios
204 PORTA&=~((1<<PINA4)|(1<<PINA5));
206 PORTA&=~((1<<PINA6)|(1<<PINA7));
208 PORTB&=~(1<<PINB0); //Disable Pullup io switch to save Power
213 PCMSK0=(1<<PCINT4)|(1<<PCINT5)
215 |(1<<PCINT6)|(1<<PCINT7)
223 PORTA&=~(1<<PINA0); //Spannungsueberpruefung
232 for(uint8_t i=0;i<16;i++) {
234 while(EECR & (1<<EEPE));
237 counters.c8[addr]=EEDR;
239 for (uint8_t i=0;i<4;i++) {
240 if (counters.c32[i]==0xFFFFFFFF) counters.c32[i]=0;
244 /*for(uint8_t i=0;i<16;i++) {
245 while(EECR & (1<<EEPE));
257 //Test if timer active and no sleep then Idle else Power Down
258 if (TEST_TIMER&&(mode==0)) {
259 MCUCR|=(1<<SE)|(1<<SM1); //Power Down, only low level on 1-Wire and pin change on PCINT wakes up