First Commit
[owSlave2.git] / DS2406 / DS2406.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
35 #define F_CPU 8000000UL
36 #include <avr/io.h>
37 #include <avr/interrupt.h>
38 #include <util/delay.h>
39 #include <avr/wdt.h>
40 #include <avr/sleep.h>
41
42
43 extern void OWINIT();
44
45 uint8_t owid[8]={0x12, 0xA2, 0xD9, 0x84, 0x00, 0x26, 0x02, 0x1E};/**/
46
47         
48
49 extern uint8_t mode;
50 extern uint8_t gcontrol;
51 extern uint8_t reset_indicator;
52
53
54
55 typedef union {
56         volatile uint8_t bytes[1];
57         struct {
58                 uint8_t status;
59         };
60 } pack_t;
61 pack_t pack;
62
63
64
65
66 int main(void){
67     PRR|=(1<<PRUSI)|(1<<PRADC);  //Switch off usi and adc for save Power
68         OWINIT();
69         
70         ACSR|=(1<<ACD);  //Disable Comparator
71         ADCSRB|=(1<<ACME); //Disable Analog multiplexer
72         MCUCR &=~(1<<PUD); //All Pins Pullup...
73         
74                 
75         
76
77
78
79 #ifdef _EEPROM_SAVE_
80                 EEARH=0;
81                 
82                 uint8_t addr;
83                 for(uint8_t i=0;i<16;i++) {
84                         addr=i^0x0C;
85                         while(EECR & (1<<EEPE));   
86                         EEARL=i;
87                         EECR |= (1<<EERE);
88                         counters.c8[addr]=EEDR;
89                 }
90                 for (uint8_t i=0;i<4;i++) {
91                         if (counters.c32[i]==0xFFFFFFFF) counters.c32[i]=0;
92                         //counters.c32[i]=0;
93                 }
94
95                 /*for(uint8_t i=0;i<16;i++) {
96                         while(EECR & (1<<EEPE));
97                         EECR = (1<<EEPM0);
98                         EEARL = i;
99                         EECR |= (1<<EEMPE);
100                         EECR |= (1<<EEPE);
101                 }*/
102         
103    
104 #endif
105         sei();
106     while(1)   {
107                 
108                 //Test if timer active and no sleep then Idle else Power Down
109                 if (TEST_TIMER&&(mode==0)) {
110                         MCUCR|=(1<<SE)|(1<<SM1); //Power Down, only low level on 1-Wire and pin change on PCINT wakes up
111                         MCUCR&=~(1<<ISC01);
112                 } else {
113                         MCUCR|=(1<<SE);
114                         MCUCR&=~(1<<SM1); 
115                 }
116                 asm("SLEEP");
117    }
118 }