New Dual with BMP280 and SHT
[owSlave2.git] / DS2450 / DS2450.c
1
2 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
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
14 //    distribution.
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.
21 //
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.
33
34 #define F_CPU 8000000UL
35 #include <avr/io.h>
36 #include <avr/interrupt.h>
37 #include <util/delay.h>
38 #include <avr/wdt.h>
39 #include <avr/sleep.h>
40 #include <avr/pgmspace.h>
41
42 extern void OWINIT();
43
44 uint8_t owid[8]={0x20, 0xA2, 0xD9, 0x84, 0x00, 0x16, 0x02, 0x5D};/**/
45 uint8_t config_info[16]={0x06,0x09,0x06,0x09,0x06,0x09,0x06,0x09,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
46
47 #if (owid>128) 
48 #error "Variable not correct"
49 #endif
50
51 extern uint8_t mode;
52 extern uint8_t gcontrol;
53 extern uint8_t reset_indicator;
54 extern uint8_t alarmflag;
55
56
57 typedef union {
58         volatile uint8_t bytes[0x20];
59         struct {
60                 //Page0
61                 uint16_t A;  //0
62                 uint16_t B;  //2
63                 uint16_t C;  //4
64                 uint16_t D;  //6
65                 //Page1
66                 uint8_t CSA1;
67                 uint8_t CSA2;
68                 uint8_t CSB1;
69                 uint8_t CSB2;
70                 uint8_t CSC1;
71                 uint8_t CSC2;
72                 uint8_t CSD1;
73                 uint8_t CSD2;
74                 //Page2
75                 uint8_t LA;
76                 uint8_t HA;
77                 uint8_t LB;
78                 uint8_t HB;
79                 uint8_t LC;
80                 uint8_t HC;
81                 uint8_t LD;
82                 uint8_t HD;
83                 //Page3
84                 uint8_t FC1;
85                 uint8_t FC2;
86                 uint8_t FC3;
87                 uint8_t FC4;
88                 uint8_t VCCP;
89                 uint8_t FC5;
90                 uint8_t FC6;
91                 uint8_t FC7;
92                 uint8_t convc1;
93                 uint8_t convc2;
94                 
95                 
96         };
97 } pack_t;
98 volatile pack_t pack;
99
100
101
102
103
104
105 int main(void){
106         pack.A=0;
107         pack.B=0;
108         pack.C=0;
109         pack.D=0;
110         pack.CSA1=0x08;
111         pack.CSA2=0x8C;
112         pack.CSB1=0x08;
113         pack.CSB2=0x8C;
114         pack.CSC1=0x08;
115         pack.CSC2=0x8C;
116         pack.CSD1=0x08;
117         pack.CSD2=0x8C;
118         pack.HA=0xFF;
119         pack.LA=0x00;
120         pack.HB=0xFF;
121         pack.LB=0x00;
122         pack.HC=0xFF;
123         pack.LC=0x00;
124         pack.HD=0xFF;
125         pack.LD=0x00;
126         pack.VCCP=0;
127         OWINIT();
128
129         MCUCR &=~(1<<PUD); //All Pins Pullup...
130         MCUCR |=(1<<BODS);
131         PORTA&=~((1<<PINA0)|(1<<PINA1)|(1<<PINA2)|(1<<PINA3));
132         ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
133
134         
135         
136         gcontrol=1;
137         ADCSRB|=(1<<ADLAR); 
138         sei();
139
140     while(1)   {
141
142
143                 if (gcontrol) {
144                         uint8_t bb=1;
145                         uint8_t bb1=1;
146                         for(uint8_t i=0;i<4;i++){
147                                 if (pack.convc1&bb1) {
148                                         if (pack.convc2&(bb)) {pack.bytes[i*2]=0;pack.bytes[i*2+1]=0;}
149                                         bb=bb<<1;
150                                         if (pack.convc2&(bb)) {pack.bytes[i*2]=0xFF;pack.bytes[i*2+1]=0xFF;}
151                                         bb=bb<<1;
152                                 } else bb=bb<<2;
153                                 bb1=bb1<<1;                             
154                         }
155                         //CHanel A
156                         if (pack.convc1&1) {
157                                 if (pack.CSA2&0x01)     ADMUX=0; else ADMUX=0x80;
158                                 _delay_ms(10);
159                                 ADCSRA|=(1<<ADSC);
160                                 while ((ADCSRA&(1<<ADSC)));
161                                 cli();pack.A=ADC;sei();
162                                 alarmflag=0;
163                                 if (pack.CSA2&0x08)  //AEH
164                                         if (pack.bytes[1]>pack.HA) {alarmflag=1;pack.CSA2|=0x20;}
165                                 if (pack.CSA2&0x04)  //AEL
166                                         if (pack.bytes[1]<pack.LA) {alarmflag=1;pack.CSA2|=0x10;}
167                         }
168
169                         if (pack.convc1&2) {
170                                 if (pack.CSB2&0x01)     ADMUX=1; else ADMUX=0x81;
171                                 _delay_ms(10);
172                                 ADCSRA|=(1<<ADSC);
173                                 while ((ADCSRA&(1<<ADSC)));
174                                 cli();pack.B=ADC;sei();
175                                 if (pack.CSB2&0x08)  //AEH
176                                         if (pack.bytes[1]>pack.HB) {alarmflag=1;pack.CSB2|=0x20;}
177                                 if (pack.CSB2&0x04)  //AEL
178                                         if (pack.bytes[1]<pack.LB) {alarmflag=1;pack.CSB2|=0x10;}
179                         }
180
181                         if (pack.convc1&4) {
182                                 if (pack.CSC2&0x01)     ADMUX=2; else ADMUX=0x82;
183                                 _delay_ms(10);
184                                 ADCSRA|=(1<<ADSC);
185                                 while ((ADCSRA&(1<<ADSC)));
186                                 cli();pack.C=ADC;sei();
187                                 if (pack.CSC2&0x08)  //AEH
188                                         if (pack.bytes[1]>pack.HC) {alarmflag=1;pack.CSC2|=0x20;}
189                                 if (pack.CSC2&0x04)  //AEL
190                                         if (pack.bytes[1]<pack.LC) {alarmflag=1;pack.CSC2|=0x10;}
191                         } 
192                         if (pack.convc1&8) {
193                                 if (pack.CSD2&0x01)     ADMUX=3; else ADMUX=0x83;
194                                 _delay_ms(10);
195                                 ADCSRA|=(1<<ADSC);
196                                 while ((ADCSRA&(1<<ADSC)));
197                                 cli();pack.D=ADC;sei();
198                                 if (pack.CSD2&0x08)  //AEH
199                                         if (pack.bytes[1]>pack.HD) {alarmflag=1;pack.CSD2|=0x20;}
200                                 if (pack.CSD2&0x04)  //AEL
201                                         if (pack.bytes[1]<pack.LD) {alarmflag=1;pack.CSD2|=0x10;}
202                         }
203                         
204                         gcontrol=0;
205                 }
206
207                 uint8_t bb=1;
208                 for(uint8_t i=0;i<4;i++) {
209                         if (pack.bytes[8+i*2]&0x80) {  //Chanel as output
210                                 if (pack.bytes[8+i*2]&0x40) {
211                                         DDRA|=bb;
212                                 } else {
213                                         DDRA&=~bb;
214                                 }
215                         } else {
216                                 DDRA&=~bb;
217                         }
218                         bb=bb*2;
219                 }
220                 
221 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)
222                         if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))
223 #endif                  
224 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
225                         if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))
226 #endif
227                           {
228
229                         MCUCR|=(1<<SE)|(1<<SM1);
230                         MCUCR&=~(1<<ISC01);
231                 } else {
232                         MCUCR|=(1<<SE);
233                         MCUCR&=~(1<<SM1);
234                 }
235         //      asm("SLEEP");
236    }
237
238
239 }