1 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de
\r
2 // All rights reserved.
\r
4 // Redistribution and use in source and binary forms, with or without
\r
5 // modification, are permitted provided that the following conditions are
\r
8 // * Redistributions of source code must retain the above copyright
\r
9 // notice, this list of conditions and the following disclaimer.
\r
10 // * Redistributions in binary form must reproduce the above copyright
\r
11 // notice, this list of conditions and the following disclaimer in the
\r
12 // documentation and/or other materials provided with the
\r
14 // * All advertising materials mentioning features or use of this
\r
15 // software must display the following acknowledgement: This product
\r
16 // includes software developed by tm3d.de and its contributors.
\r
17 // * Neither the name of tm3d.de nor the names of its contributors may
\r
18 // be used to endorse or promote products derived from this software
\r
19 // without specific prior written permission.
\r
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\r
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
\r
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
\r
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
\r
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
33 //!!!!!Max Program size 7551 Byte
\r
35 #define F_CPU 8000000UL
\r
37 #include <avr/interrupt.h>
\r
38 #include <util/delay.h>
\r
39 #include <avr/wdt.h>
\r
40 #include <avr/sleep.h>
\r
41 #include <avr/pgmspace.h>
\r
43 extern void OWINIT(void);
\r
44 extern void EXTERN_SLEEP(void);
\r
45 //#define FHEM_PLATINE
\r
49 volatile uint8_t owid1[8]={0x1D, 0x1D, 0xDA, 0x84, 0x00, 0x00, 0x05, 0xD9};/**/
\r
50 volatile uint8_t owid2[8]={0x3A, 0x3A, 0xDA, 0x84, 0x00, 0x00, 0x05, 0xB6};/**/
\r
51 uint8_t config_info1[26]={9,13,9,13,9,13,9,13,0x02,19,19,19,19,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //+2 for CRC
\r
52 uint8_t config_info2[26]={0,0,0,0,0,0,0,0,0x02,0,0,0,0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //+2 for CRC
\r
55 #error "Variable not correct"
\r
58 extern uint8_t mode;
\r
59 extern uint8_t gcontrol;
\r
60 extern uint8_t reset_indicator;
\r
68 volatile uint8_t bytes[45];
\r
72 uint8_t scratch[32];//3
\r
73 uint32_t counter; //35
\r
78 counterpack_t pack1;
\r
82 volatile uint8_t lastcps;
\r
88 volatile counters_t counters1;
\r
91 volatile uint8_t istat;
\r
92 volatile uint8_t changefromeeprom;
\r
94 #if defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
\r
95 #define PCINT_VECTOR PCINT0_vect
\r
96 #define PIN_REG PINA
\r
97 #define PIN_DDR DDRA
\r
100 #define PIN_CH3 (1<<PINA2)
\r
101 #define PIN_CH2 (1<<PINA1)
\r
102 #define PIN_PIOA (1<<PINA3)
\r
103 #define PIN_PIOB (1<<PINA4)
\r
104 //LEDS
\r#define LPIN_CH2 (1<<PINB0)
\r#define LDD_CH2 DDRB
\r#define LPORT_CH2 PORTB
\r#define LPIN_CH3 (1<<PINA5)
\r#define LDD_CH3 DDRA
\r#define LPORT_CH3 PORTA
\r#define LPIN_CH0 (1<<PINA7)
\r#define LDD_CH0 DDRA
\r#define LPORT_CH0 PORTA
\r#define LPIN_CH1 (1<<PINB1)
\r#define LDD_CH1 DDRB
\r#define LPORT_CH1 PORTB
\r
106 #define LED2_ON LPORT_CH2|=LPIN_CH2;
\r
112 #define PIN_CH2 (1<<PINA4)
\r
113 #define PIN_CH3 (1<<PINA5)
\r
114 #define PIN_CH0 (1<<PINA6)
\r
115 #define PIN_CH1 (1<<PINA7)
\r
116 #define PIN_PIOA PIN_CH0
\r
117 #define PIN_PIOB PIN_CH1
\r
121 #define PIN_CH3 (1<<PINA1)
\r
122 #define PIN_CH2 (1<<PINA0)
\r
123 #define PIN_CH1 (1<<PINA7)
\r
124 #define PIN_CH0 (1<<PINA3)
\r
125 #define PIN_PIOA PIN_CH0
\r
126 #define PIN_PIOB PIN_CH1
\r
127 //LEDS
\r#define LPIN_CH0 (1<<PINB1)
\r#define LDD_CH0 DDRB
\r#define LPORT_CH0 PORTB
\r#define LPIN_CH1 (1<<PINB1)
\r#define LDD_CH1 DDRB
\r#define LPORT_CH1 PORTB
\r#define LPIN_CH2 (1<<PINB1)
\r#define LDD_CH2 DDRB
\r#define LPORT_CH2 PORTB
\r#define LPIN_CH3 (1<<PINB1)
\r#define LDD_CH3 DDRB
\r#define LPORT_CH3 PORTB
\r\r
128 #define LED2_ON LPORT_CH2&=~LPIN_CH2;
\r
131 #define TEST_TIMER ((TIMSK0 & (1<<TOIE0))==0)
\r
138 if (((PIN_REG&PIN_CH2)==0)&&((istat&PIN_CH2)==PIN_CH2)) { counters1.c32[2]++; LED2_ON}
\r
139 if (((PIN_REG&PIN_CH3)==0)&&((istat&PIN_CH3)==PIN_CH3)) { counters1.c32[3]++;LED2_ON }
\r
140 if ((PIN_REG&PIN_PIOA)==0) pin_state&=~0x1; else pin_state|=0x01;
\r
141 if ((PIN_REG&PIN_PIOB)==0) pin_state&=~0x4; else pin_state|=0x04;
\r
142 //Reset Switch on the FHEM_BOARD
\r
143 #ifdef FHEM_PLATINE //clear counter
\r
144 if ((((PINA&(1<<PINA6)))==0)&&((istat&(1<<PINA6))==(1<<PINA6))) {
\r _delay_ms(100);
\r if (((PINA&(1<<PINA6)))==0) {
\r LPORT_CH3|=LPIN_CH3;
\r _delay_ms(100);
\r counters1.c32[2]=0;
\r counters1.c32[3]=0;
\r //count Resets
\r counters1.c32[0]++;
\r LPORT_CH3&=~LPIN_CH3;
\r }
\r GIFR|=(1<<PCIF0);
\r }
\r #endif
\r\r
146 changefromeeprom=1;
\r
150 ISR(ANA_COMP_vect) {
\r
151 if (changefromeeprom==0) return;
\r
152 if ((ACSR&(1<<ACO))!=0) {
\r
154 if ((ACSR&(1<<ACO))!=0) {
\r
155 counters1.c32[1]++;
\r CLKPR=0x80;//Switch to 4 MHz
\r
160 for(uint8_t i=0;i<16;i++) {
\r
162 while(EECR & (1<<EEPE));
\r
163 EECR = (0<<EEPM1)|(0<<EEPM0);
\r
165 EEDR = counters1.c8[addr];
\r
166 EECR |= (1<<EEMPE);
\r
169 changefromeeprom=0;
\r
170 PORTB&=~(1<<PINB1);
\r
173 #ifdef FHEM_PLATINE
\r
174 LPORT_CH1|=LPIN_CH1;
\r
184 #ifdef FHEM_PLATINE
\r
185 PRR|=(1<<PRUSI); //Switch off usi, dont switch of ADC cause Multiplexer is used for the correct AIN1 pin
\r
187 PRR|=(1<<PRUSI)|(1<<PRADC); //Switch off usi and adc for save Power
\r
192 counters1.c32[0]=0;
\r
193 counters1.c32[2]=0;
\r
194 counters1.c32[1]=0;
\r
195 counters1.c32[3]=0;
\r
197 changefromeeprom=1;
\r
198 #if defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
\r
200 PORTB|=0xFF-(1<<PINB2); //Make PullUp an all Pins but not OW_PIN
\r
203 #ifndef _CPULLUP_ // pullup
\r
204 PORTA&=~(PIN_PIOA|PIN_PIOB);
\r
205 PORTA&=~(PIN_CH2|PIN_CH3);
\r
208 #if defined(FHEM_PLATINE) || defined(W1DAQ) //LEDs
\r
209 LDD_CH0|=LPIN_CH0;
\r LPORT_CH0&=~LPIN_CH0;
\r LDD_CH1|=LPIN_CH1;
\r LPORT_CH1&=~LPIN_CH1;
\r LDD_CH2|=LPIN_CH2;
\r LPORT_CH2&=~LPIN_CH2;
\r LDD_CH3|=LPIN_CH3;
\r LPORT_CH3&=~LPIN_CH3;
\r
213 PCMSK0=(PIN_PIOA|PIN_PIOB|PIN_CH2|PIN_CH3); //Nicht ganz korrekt aber die Bits liegen gleich
\r
222 for(uint8_t i=0;i<16;i++) {
\r
224 while(EECR & (1<<EEPE));
\r
227 counters1.c8[addr]=EEDR;
\r
229 changefromeeprom=0; //Daten neu eingelesen
\r
230 for (uint8_t i=0;i<4;i++) {
\r
231 if (counters1.c32[i]==0xFFFFFFFF) {
\r
232 counters1.c32[i]=0;
\r
233 changefromeeprom=1; //Daten geaendert
\r
235 //counters.c32[i]=0;
\r
238 //ACSR|=(1<<ACD); //Disable Comparator
\r
239 ADCSRB|=(1<<ACME); //Disable Analog multiplexer
\r
240 MCUCR &=~(1<<PUD); //All Pins Pullup...
\r
241 #ifdef FHEM_PLATINE
\r
243 PORTA&=~(1<<PINA0);//Disable Pullup
\r
245 DIDR0|=(1<<ADC2D)|(1<<ADC1D); // Disable Digital input on Analog AIN0/AIN1 (PINA1 / PINA2)
\r
246 PORTA&=~(1<<PINA2); //AIN1
\r
249 ACSR|=(1<<ACIE)|(1<<ACIS1)|(1<<ACIS0)|(1<<ACBG); //Enabble comperator interrupt Rising edge....(1<<ACIS0) -> minus of Comperator falls down -> output of Comperator rises
\r
250 #ifdef FHEM_PLATINE
\r
251 //Switch std AIN1 to A0
\r
252 ADCSRA&=~(1<<ADEN);
\r
256 DDRA&=~(1<<PINA6);
\r PCMSK0|=(1<<PCINT6);
\r PORTA|=(1<<PINA6); //Pullup
\r
259 LPORT_CH0|=LPIN_CH0;
\r _delay_ms(500);
\r LPORT_CH0&=~LPIN_CH0;
\r
262 LPORT_CH0&=~LPIN_CH0;
\r
263 _delay_ms(500);
\rLPORT_CH0|=LPIN_CH0;
\r#endif
\r
270 counters1.c32[3]=0;
\r
272 changefromeeprom=1;
\r
275 counters1.c32[2]=0;
\r
277 changefromeeprom=1;
\r
279 /*if (counters1.c32[2]==0) {
\r
281 } else pin_state&=~2;
\r
282 if (counters1.c32[3]==0) {
\r
284 } else pin_state&=~8;*/
\r
286 #ifdef FHEM_PLATINE
\r
287 if (LPORT_CH2&LPIN_CH2) {
\r _delay_ms(50);
\r LPORT_CH2&=~LPIN_CH2;
\r }
\r
288 if (LPORT_CH1&LPIN_CH1) {
\r _delay_ms(50);
\r LPORT_CH1&=~LPIN_CH1;
\r }
\r
291 if ((LPORT_CH2&LPIN_CH2)==0) {
\r _delay_ms(50);
\r LPORT_CH2|=LPIN_CH2;
\r}
\r
293 #ifndef FHEM_PLATINE
\r
294 if ((PINB&(1<<PORTB0))==0) { //Jumper gesetzt ->Ruecksetzen
\r
295 for (uint8_t i=2;i<4;i++) {
\r
296 counters1.c32[i]=0;
\r
298 //count Resets
\r counters1.c32[0]++;
\r changefromeeprom=1;
\r