322f70f1bcb3e276f745f799113dc299ea8d731f
[owSlave2.git] / DS18B20_Thermocouble / OWDS18B20.S
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 _CHANGEABLE_ID_
35
36 #include "../common/OWConfig.s"
37 #include "../common/OWCRC8.s"
38
39 .extern pack,8
40
41 //.extern  am2302_temp,2
42
43
44
45 .macro CHIP_INIT        
46 Init_EEPROM_read:
47         sbic _SFR_IO_ADDR(EECR), EEPE
48         rjmp Init_EEPROM_read
49         ldi r_temp,0
50         out _SFR_IO_ADDR(EEARH), r_temp
51         ldi r_temp,2
52         out _SFR_IO_ADDR(EEARL), r_temp
53         sbi _SFR_IO_ADDR(EECR), EERE
54         in r_temp,_SFR_IO_ADDR(EEDR)
55         sbrs r_temp,7
56         rcall hrc_recall_eeprom_func
57 .endm
58
59 .macro COMMAND_TABLE
60                 rjmp h_readscratchpad
61                 rjmp h_writescratchpad
62 .endm
63
64 #include "../common/OWRomFunctions.s"
65 #include "../common/OWTimerInterrupt.s"
66
67
68
69 ; Ab hier Geraeteabhaenging
70 #define OW_READ_SCRATCHPAD OW_FIRST_COMMAND+0
71 #define OW_WRITE_SCRATCHPAD OW_FIRST_COMMAND+1
72
73
74 ;---------------------------------------------------
75 ;       READ COMMAND and start operation
76 ;---------------------------------------------------
77
78
79 h_readcommand:
80         clr r_bytep
81 #ifndef _DIS_FLASH_
82         FLASH_COMMANDS ; muss zu erst sein....
83 #endif
84         cjmp 0xBE,hrc_set_readscratchpad
85         cjmp 0x4E,hrc_set_writescratchpad
86         cjmp 0x44,hrc_set_convertT
87         cjmp 0x48,hrc_copy_scratchpad
88         cjmp 0xB8,hrc_recall_eeprom
89 #ifdef _CHANGEABLE_ID_
90         CHANGE_ID_COMMANDS
91 #endif
92         ldi r_mode,OW_SLEEP
93         rjmp handle_end
94
95 hrc_set_readscratchpad:
96         ldi r_mode,OW_READ_SCRATCHPAD
97         ldi r_sendflag,1
98         CRCInit2
99         rjmp h_readscratchpad
100
101 hrc_set_writescratchpad:
102         ldi r_mode,OW_WRITE_SCRATCHPAD
103         ldi r_bytep,2 ;start to write in 2
104         rjmp handle_end
105
106 hrc_recall_eeprom:
107         rcall hrc_recall_eeprom_func
108         rjmp handle_end
109
110
111
112 hrc_set_convertT:
113         ldi r_temp,1
114         sts gcontrol,r_temp
115         ;lds temp,am2302_temp
116         //sts pack+1,temp
117         ;lds temp,am2302_temp+1
118         //sts pack+2,temp
119 ;       ldi r_rwbyte,0
120 ;       lds r_temp,pack
121 ;       swap r_temp
122 ;       andi r_temp,0x0F
123 ;       lds r_temp2,pack+1
124 ;       swap r_temp2
125 ;       andi r_temp2,0xF0
126 ;       or r_temp,r_temp2
127 ;       lds r_temp2,pack+2
128 ;       cp r_temp,r_temp2  ;higher than or equal to TH
129 ;       brge hrc_set_convertT_set_arlarm
130 ;       lds r_temp2,pack+3 
131 ;       cp r_temp,r_temp2 ;lower than or equal to TL
132 ;       brlt hrc_set_convertT_set_arlarm
133 ;       breq hrc_set_convertT_set_arlarm
134 ;       rjmp hrc_set_convertT_end
135 ;hrc_set_convertT_set_arlarm:
136 ;       ldi r_rwbyte,1
137 ;hrc_set_convertT_end:
138 ;       sts alarmflag,r_rwbyte
139         rjmp handle_end_sleep
140
141
142 hrc_copy_scratchpad:
143         ldi r_bytep,2
144         configZ pack,r_bytep
145         clr r_bytep
146 hrc_copy_scratchpad_EEPROM_write:
147         sbic _SFR_IO_ADDR(EECR), EEPE   
148         rjmp hrc_copy_scratchpad_EEPROM_write
149         ldi r_temp, (0<<EEPM1)|(0<<EEPM0)
150         out _SFR_IO_ADDR(EECR), r_temp
151         ldi r_temp,0
152         out _SFR_IO_ADDR(EEARH),r_temp
153         out _SFR_IO_ADDR(EEARL), r_bytep
154         ld  r_rwbyte,Z+
155         out _SFR_IO_ADDR(EEDR), r_rwbyte
156         sbi _SFR_IO_ADDR(EECR), EEMPE
157         sbi _SFR_IO_ADDR(EECR), EEPE
158         inc r_bytep
159         cpi r_bytep,3
160         brne hrc_copy_scratchpad_EEPROM_write
161         rjmp handle_end
162
163
164 hrc_recall_eeprom_func:
165         ldi r_bytep,2
166         configZ pack,r_bytep
167         clr r_bytep
168         clr r_temp
169 hrc_recall_eeprom_EEPROM_read:
170         sbic _SFR_IO_ADDR(EECR), EEPE
171         rjmp hrc_recall_eeprom_EEPROM_read
172         out _SFR_IO_ADDR(EEARH), r_temp
173         out _SFR_IO_ADDR(EEARL), r_bytep
174         sbi _SFR_IO_ADDR(EECR), EERE
175         in r_rwbyte,_SFR_IO_ADDR(EEDR)
176         st Z+,r_rwbyte
177         inc r_bytep
178         cpi r_bytep,3
179         brne hrc_recall_eeprom_EEPROM_read
180         ret
181
182
183
184
185
186
187 ;---------------------------------------------------
188 ;   READ SCRATCHPAD
189 ;---------------------------------------------------
190
191 h_readscratchpad:
192         cpi  r_bytep,8
193         breq h_readscratchpad_crc
194         cpi  r_bytep,9
195         breq h_readscratchpad_all
196         configZ pack,r_bytep
197         ld   r_rwbyte,Z
198         rjmp h_readscratchpad_endc
199 h_readscratchpad_crc:
200         lds  r_rwbyte,crc
201 h_readscratchpad_endc:
202         inc  r_bytep
203         ldi  r_bcount,1 
204         rjmp handle_end
205 h_readscratchpad_all:
206         rjmp handle_end_sleep
207
208
209
210
211
212 ;---------------------------------------------------
213 ;   WRITE SCRATCHPAD
214 ;---------------------------------------------------
215
216 h_writescratchpad:
217         configZ pack,r_bytep
218         inc  r_bytep
219         cpi  r_bytep,5
220         breq h_writescratchpad_all
221         st   Z,r_rwbyte
222         rjmp handle_end
223 h_writescratchpad_all:
224         ori r_rwbyte,0x1F ; Alle unteren Bits sind immer 1
225         st   Z,r_rwbyte
226         rjmp handle_end_sleep
227
228
229
230
231
232
233 #include "../common/OWPinInterrupt.s"
234 .end