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