annotate 1wire.c @ 41:5898fba6593c

Add temperature control. - Split out console stuff to cons.[ch]. Set up stdio so we can use printf etc. - Use \r\n as the line terminator consistently. - Add OWGetTemp to get temperatures from a device. - Load/save settings in EEPROM, defaults loaded from flash. Nearly feature complete except you can't edit ROM IDs without a programming tool :) (To be fixed) Needs more testing.
author darius@inchoate.localdomain
date Sun, 06 Jul 2008 22:19:53 +0930
parents 0aa6bf4b98ae
children efd44dc40934
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
1 /*
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
2 * Various 1 wire routines
12
4b141cc7cbd4 - Reduce type widths where possible.
darius
parents: 10
diff changeset
3 * Search routine is copied from the Dallas owpd library with mods
4b141cc7cbd4 - Reduce type widths where possible.
darius
parents: 10
diff changeset
4 * available from here http://www.ibutton.com/software/1wire/wirekit.html
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
5 *
8
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
6 * $Id$
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
7 *
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
8 * Copyright (c) 2004
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
9 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
10 *
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
11 * Redistribution and use in source and binary forms, with or without
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
12 * modification, are permitted provided that the following conditions
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
13 * are met:
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
14 * 1. Redistributions of source code must retain the above copyright
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
15 * notice, this list of conditions and the following disclaimer.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
16 * 2. Redistributions in binary form must reproduce the above copyright
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
17 * notice, this list of conditions and the following disclaimer in the
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
18 * documentation and/or other materials provided with the distribution.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
19 *
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
20 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
23 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
30 * SUCH DAMAGE.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
31 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
32
32
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
33 /*
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
34 * No user servicable parts inside
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
35 *
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
36 * Modify 1wire-config.h
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
37 */
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
38
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
39 #include <stdio.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
40 #include <avr/io.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
41 #include <avr/pgmspace.h>
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
42 #include <util/delay.h>
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
43 #include "1wire.h"
32
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
44 #include "1wire-config.h"
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
45 #include "cons.h"
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
46
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
47 static uint8_t OW_LastDevice = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
48 static uint8_t OW_LastDiscrepancy = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
49 static uint8_t OW_LastFamilyDiscrepancy = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
50
32
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
51 /*-----------------------------------------------------------------------------
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
52 * Configure the IO port as we need
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
53 */
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
54 void
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
55 OWInit(void) {
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
56 OWBUSINIT();
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
57 OWSETBUSHIGH();
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
58 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
59
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
60 /*-----------------------------------------------------------------------------
22
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
61 * Generate a 1-Wire reset, return 0 if presence pulse was found, 1 if it
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
62 * wasn't, or 2 if the line appears to be being held low.
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
63 *
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
64 * (NOTE: Does not handle alarm presence from DS2404/DS1994)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
65 */
12
4b141cc7cbd4 - Reduce type widths where possible.
darius
parents: 10
diff changeset
66 uint8_t
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
67 OWTouchReset(void) {
32
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
68 OWDELAY_G;
22
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
69
32
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
70 /* Check the bus isn't being held low (ie it's broken) Do it after
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
71 * the delay so we guarantee we don't see a slave from a previous
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
72 * comms attempt
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
73 */
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
74 if(OWREADBUS() == 0)
22
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
75 return 2;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
76
32
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
77 OWSETBUSLOW();
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
78 OWDELAY_H;
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
79 OWSETBUSHIGH();
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
80 OWDELAY_I;
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
81
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
82 return(OWREADBUS());
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
83 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
84
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
85 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
86 * Send a 1-wire write bit.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
87 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
88 void
12
4b141cc7cbd4 - Reduce type widths where possible.
darius
parents: 10
diff changeset
89 OWWriteBit(uint8_t bit) {
32
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
90 OWDELAY_I;
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
91
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
92 if (bit) {
32
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
93 OWSETBUSLOW();
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
94 OWDELAY_A;
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
95 OWSETBUSHIGH();
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
96 OWDELAY_B;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
97 } else {
32
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
98 OWSETBUSLOW();
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
99 OWDELAY_C;
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
100 OWSETBUSHIGH();
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
101 OWDELAY_D;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
102 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
103 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
104
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
105 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
106 * Read a bit from the 1-wire bus and return it.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
107 */
12
4b141cc7cbd4 - Reduce type widths where possible.
darius
parents: 10
diff changeset
108 uint8_t
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
109 OWReadBit(void) {
32
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
110 OWDELAY_I;
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
111
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
112 OWSETBUSLOW();
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
113 OWDELAY_A;
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
114 OWSETBUSHIGH();
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
115 OWDELAY_E;
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
116 return(OWREADBUS());
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
117 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
118
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
119 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
120 * Write a byte to the 1-wire bus
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
121 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
122 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
123 OWWriteByte(uint8_t data) {
12
4b141cc7cbd4 - Reduce type widths where possible.
darius
parents: 10
diff changeset
124 uint8_t i;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
125
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
126 /* Send LSB first */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
127 for (i = 0; i < 8; i++) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
128 OWWriteBit(data & 0x01);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
129 data >>= 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
130 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
131 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
132
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
133 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
134 * Read a byte from the 1-wire bus
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
135 */
12
4b141cc7cbd4 - Reduce type widths where possible.
darius
parents: 10
diff changeset
136 uint8_t
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
137 OWReadByte(void) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
138 int i, result = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
139
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
140 for (i = 0; i < 8; i++) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
141 result >>= 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
142 if (OWReadBit())
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
143 result |= 0x80;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
144 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
145 return(result);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
146 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
147
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
148 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
149 * Write a 1-wire data byte and return the sampled result.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
150 */
12
4b141cc7cbd4 - Reduce type widths where possible.
darius
parents: 10
diff changeset
151 uint8_t
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
152 OWTouchByte(uint8_t data) {
12
4b141cc7cbd4 - Reduce type widths where possible.
darius
parents: 10
diff changeset
153 uint8_t i, result = 0;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
154
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
155 for (i = 0; i < 8; i++) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
156 result >>= 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
157
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
158 /* If sending a 1 then read a bit, otherwise write a 0 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
159 if (data & 0x01) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
160 if (OWReadBit())
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
161 result |= 0x80;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
162 } else
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
163 OWWriteBit(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
164
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
165 data >>= 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
166 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
167
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
168 return(result);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
169 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
170
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
171 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
172 * Write a block of bytes to the 1-wire bus and return the sampled result in
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
173 * the same buffer
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
174 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
175 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
176 OWBlock(uint8_t *data, int len) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
177 int i;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
178
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
179 for (i = 0; i < len; i++)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
180 data[i] = OWTouchByte(data[i]);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
181 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
182
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
183
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
184 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
185 * Send a 1 wire command to a device, or all if no ROM ID provided
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
186 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
187 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
188 OWSendCmd(uint8_t *ROM, uint8_t cmd) {
12
4b141cc7cbd4 - Reduce type widths where possible.
darius
parents: 10
diff changeset
189 uint8_t i;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
190
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
191 OWTouchReset();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
192
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
193 if (ROM == NULL)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
194 OWWriteByte(OW_SKIP_ROM_CMD);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
195 else {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
196 OWWriteByte(OW_MATCH_ROM_CMD);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
197 for (i = 0; i < 8; i++)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
198 OWWriteByte(ROM[i]);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
199 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
200 OWWriteByte(cmd);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
201 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
202
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
203 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
204 * Search algorithm from App note 187 (and 162)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
205 *
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
206 * OWFirst/OWNext return..
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
207 * 1 when something is found,
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
208 * 0 no more modules
16
026dc24d85e0 Spell presence correctly.
darius
parents: 12
diff changeset
209 * -1 if no presence pulse,
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
210 * -2 if bad CRC,
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
211 * -3 if bad wiring.
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
212 */
12
4b141cc7cbd4 - Reduce type widths where possible.
darius
parents: 10
diff changeset
213 uint8_t
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
214 OWFirst(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
215 /* Reset state */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
216 OW_LastDiscrepancy = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
217 OW_LastDevice = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
218 OW_LastFamilyDiscrepancy = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
219
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
220 /* Go looking */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
221 return (OWNext(ROM, do_reset, alarm_only));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
222 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
223
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
224 /* Returns 1 when something is found, 0 if nothing left */
12
4b141cc7cbd4 - Reduce type widths where possible.
darius
parents: 10
diff changeset
225 uint8_t
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
226 OWNext(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
227 uint8_t bit_test, search_direction, bit_number;
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
228 uint8_t last_zero, rom_byte_number, rom_byte_mask;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
229 uint8_t lastcrc8, crcaccum;
12
4b141cc7cbd4 - Reduce type widths where possible.
darius
parents: 10
diff changeset
230 int8_t next_result;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
231
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
232 /* Init for search */
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
233 bit_number = 1;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
234 last_zero = 0;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
235 rom_byte_number = 0;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
236 rom_byte_mask = 1;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
237 next_result = OW_NOMODULES;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
238 lastcrc8 = 0;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
239 crcaccum = 0;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
240
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
241 /* if the last call was not the last one */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
242 if (!OW_LastDevice) {
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
243 /* check if reset first is requested */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
244 if (do_reset) {
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
245 /* reset the 1-wire
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
246 * if there are no parts on 1-wire, return 0 */
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
247 OWPUTSP(PSTR("Resetting\r\n"));
22
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
248 switch (OWTouchReset()) {
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
249 case 0:
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
250 break;
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
251
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
252 case 1:
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
253 /* reset the search */
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
254 OW_LastDiscrepancy = 0;
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
255 OW_LastFamilyDiscrepancy = 0;
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
256 OWPUTSP(PSTR("No devices on bus\r\n"));
22
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
257 return OW_NOPRESENCE;
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
258 break;
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
259
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
260 case 2:
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
261 /* reset the search */
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
262 OW_LastDiscrepancy = 0;
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
263 OW_LastFamilyDiscrepancy = 0;
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
264 OWPUTSP(PSTR("Bus appears to be being held low\r\n"));
22
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
265 return OW_BADWIRE;
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
266 break;
bd792ebf813d Report the bus being held low back to the caller from OWFirst/Next.
darius
parents: 21
diff changeset
267
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
268 }
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
269 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
270
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
271 /* If finding alarming devices issue a different command */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
272 if (alarm_only)
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
273 OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
274 else
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
275 OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
276
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
277 /* pause before beginning the search */
32
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
278 OWDELAY_I;
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
279 OWDELAY_I;
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
280 OWDELAY_I;
b0cb873c0206 Isolate the bus frobbing parts and the delays into a separate header.
darius
parents: 29
diff changeset
281
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
282 /* loop to do the search */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
283 do {
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
284 /* read a bit and its compliment */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
285 bit_test = OWReadBit() << 1;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
286 bit_test |= OWReadBit();
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
287
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
288 OWPRINTFP(PSTR("bit_test = %d\r\n"), bit_test);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
289
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
290 /* check for no devices on 1-wire */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
291 if (bit_test == 3) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
292 OWPRINTFP(PSTR("bit_test = %d\r\n"), bit_test);
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
293 return(OW_BADWIRE);
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
294 }
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
295 else {
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
296 /* all devices coupled have 0 or 1 */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
297 if (bit_test > 0)
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
298 search_direction = !(bit_test & 0x01); /* bit write value for search */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
299 else {
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
300 /* if this discrepancy is before the Last Discrepancy
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
301 * on a previous OWNext then pick the same as last time */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
302 if (bit_number < OW_LastDiscrepancy)
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
303 search_direction = ((ROM[rom_byte_number] & rom_byte_mask) > 0);
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
304 else
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
305 /* if equal to last pick 1, if not then pick 0 */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
306 search_direction = (bit_number == OW_LastDiscrepancy);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
307
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
308 /* if 0 was picked then record its position in LastZero */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
309 if (search_direction == 0) {
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
310 last_zero = bit_number;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
311
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
312 /* check for Last discrepancy in family */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
313 if (last_zero < 9)
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
314 OW_LastFamilyDiscrepancy = last_zero;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
315 }
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
316 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
317
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
318 /* set or clear the bit in the ROM byte rom_byte_number
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
319 * with mask rom_byte_mask */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
320 if (search_direction == 1)
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
321 ROM[rom_byte_number] |= rom_byte_mask;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
322 else
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
323 ROM[rom_byte_number] &= ~rom_byte_mask;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
324
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
325 /* serial number search direction write bit */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
326 OWWriteBit(search_direction);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
327
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
328 /* increment the byte counter bit_number
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
329 * and shift the mask rom_byte_mask */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
330 bit_number++;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
331 rom_byte_mask <<= 1;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
332
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
333 /* if the mask is 0 then go to new ROM byte rom_byte_number
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
334 * and reset mask */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
335 if (rom_byte_mask == 0) {
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
336 OWCRC(ROM[rom_byte_number], &crcaccum); /* accumulate the CRC */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
337 lastcrc8 = crcaccum;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
338
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
339 rom_byte_number++;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
340 rom_byte_mask = 1;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
341 }
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
342 }
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
343 } while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7 */
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
344
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
345 /* if the search was successful then */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
346 if (!(bit_number < 65) || lastcrc8) {
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
347 if (lastcrc8) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
348 OWPRINTFP(PSTR("Bad CRC (%d)\r\n"), lastcrc8);
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
349 next_result = OW_BADCRC;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
350 } else {
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
351 /* search successful so set LastDiscrepancy,LastDevice,next_result */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
352 OW_LastDiscrepancy = last_zero;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
353 OW_LastDevice = (OW_LastDiscrepancy == 0);
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
354 OWPRINTFP(PSTR("Last device = %d\r\n"), OW_LastDevice);
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
355 next_result = OW_FOUND;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
356 }
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
357 }
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
358 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
359
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
360 /* if no device found then reset counters so next 'next' will be
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
361 * like a first */
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
362 if (next_result != OW_FOUND || ROM[0] == 0) {
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
363 OW_LastDiscrepancy = 0;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
364 OW_LastDevice = 0;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
365 OW_LastFamilyDiscrepancy = 0;
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
366 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
367
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
368 if (next_result == OW_FOUND && ROM[0] == 0x00)
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
369 next_result = OW_BADWIRE;
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
370
21
e82d15fa9a1a Re-format the code.
darius
parents: 16
diff changeset
371 return next_result;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
372
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
373 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
374
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
375 uint8_t PROGMEM dscrc_table[] = {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
376 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
377 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
378 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
379 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
380 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
381 219, 133,103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
382 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
383 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
384 140,210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113,147, 205,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
385 17, 79, 173, 243, 112, 46, 204, 146, 211,141, 111, 49, 178, 236, 14, 80,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
386 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82,176, 238,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
387 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
388 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
389 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
390 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
391 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
392 };
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
393
33
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
394 /*-----------------------------------------------------------------------------
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
395 * Update *crc based on the value of x
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
396 */
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
397 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
398 OWCRC(uint8_t x, uint8_t *crc) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
399 *crc = pgm_read_byte(&dscrc_table[(*crc) ^ x]);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
400 }
33
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
401
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
402 /*-----------------------------------------------------------------------------
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
403 * Program a DS2502's memory
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
404 *
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
405 * Arguments
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
406 * ROM - ROM ID (or NULL to send SKIP_ROM)
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
407 * start - Start address (bytes)
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
408 * len - Length of data to write
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
409 * data - Data to write
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
410 * exact - If true, only accept exact matches for programming,
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
411 * otherwise only ensure the bits we requested were
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
412 * programmed [to 0]
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
413 * status - If true program status rather than memory
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
414 *
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
415 * Returns..
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
416 * 0 if all is OK
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
417 * 1 if the programming was unsuccessful
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
418 * 2 if the parameters were invalid
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
419 * 3 if the DS2502 didn't respond appropriately (also happens if the
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
420 * module doesn't exist)
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
421 */
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
422 uint8_t
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
423 OWProgROM(uint8_t *ROM, uint8_t start, uint8_t len, uint8_t *data, uint8_t exact, uint8_t status) {
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
424 #if defined(OWSETVPPON) && defined(OWSETVPPOFF)
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
425 uint8_t crc, i, tmp;
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
426
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
427 /* Stupid programmer detection */
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
428 if (status) {
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
429 if (start + len > 3)
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
430 return(2);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
431 } else {
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
432 if (start + len > 127)
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
433 return(2);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
434 }
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
435
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
436 if (len < 1)
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
437 return(2);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
438
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
439 OWDELAY_I;
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
440 if (OWTouchReset() != 0) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
441 cons_putsP(PSTR("No presence pulse\r\n"));
33
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
442 return(3);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
443 }
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
444
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
445 crc = 0;
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
446
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
447 /* Send the command */
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
448 if (status) {
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
449 OWSendCmd(ROM, OW_WRITE_STATUS);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
450 OWCRC(OW_WRITE_STATUS, &crc);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
451 } else {
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
452 OWSendCmd(ROM, OW_WRITE_MEMORY);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
453 OWCRC(OW_WRITE_MEMORY, &crc);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
454 }
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
455
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
456 /* And the start address
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
457 * (2 bytes even though one would do)
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
458 */
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
459 OWWriteByte(start);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
460 OWCRC(start, &crc);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
461
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
462 OWWriteByte(0x00);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
463 OWCRC(0x00, &crc);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
464
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
465 for (i = 0; i < len; i++) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
466 cons_putsP(PSTR("Programming "));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
467 cons_puts_hex(data[i]);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
468 cons_putsP(PSTR(" to "));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
469 cons_puts_hex(start + i);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
470 cons_putsP(PSTR("\r\n"));
33
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
471
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
472 OWWriteByte(data[i]);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
473 OWCRC(data[i], &crc);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
474
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
475 tmp = OWReadByte();
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
476
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
477 if (crc != tmp) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
478 cons_putsP(PSTR("CRC mismatch "));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
479 cons_puts_hex(crc);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
480 cons_putsP(PSTR(" vs "));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
481 cons_puts_hex(tmp);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
482 cons_putsP(PSTR("\r\n"));
33
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
483
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
484 OWTouchReset();
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
485 return(3);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
486 }
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
487
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
488 OWSETVPPON();
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
489 OWDELAY_H;
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
490 OWSETVPPOFF();
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
491
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
492 tmp = OWReadByte();
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
493
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
494 /* Check the bits we turned off are off */
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
495 /*
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
496 for (i = 0; i < 8; i++)
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
497 if (!(data[i] & 1 << i) && (tmp & 1 << i))
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
498 return(-3);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
499 */
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
500 if ((!data[i] & tmp) != 0) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
501 cons_putsP(PSTR("Readback mismatch "));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
502 cons_puts_hex(data[i]);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
503 cons_putsP(PSTR(" vs "));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
504 cons_puts_hex(data[i]);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
505 cons_putsP(PSTR("\r\n"));
33
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
506
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
507 OWTouchReset();
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
508 return(3);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
509 }
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
510
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
511 /* The DS2502 loads it's CRC register with the address of the
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
512 * next byte */
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
513 crc = 0;
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
514 OWCRC(start + i + 1, &crc);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
515 }
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
516
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
517 return(0);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
518 #else
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
519 return(1);
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
520 #endif
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
521 }
0aa6bf4b98ae - Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents: 32
diff changeset
522
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
523 /*
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
524 * OWGetTemp
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
525 *
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
526 * Get the temperature from a 1wire bus module
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
527 *
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
528 * Returns temperature in hundredths of a degree or OW_TEMP_xxx on
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
529 * error.
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
530 */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
531 int16_t
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
532 OWGetTemp(uint8_t *ROM) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
533 int8_t i;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
534 uint8_t crc, buf[9];
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
535 int16_t temp;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
536 int16_t tfrac;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
537
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
538 if (ROM[0] != OW_FAMILY_TEMP)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
539 return OW_TEMP_WRONG_FAM;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
540
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
541 OWSendCmd(ROM, OW_CONVERTT_CMD);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
542
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
543 i = 0;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
544
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
545 /* Wait for the conversion */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
546 while (OWReadBit() == 0)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
547 i = 1;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
548
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
549 /* Check that we talked to a module and it did something */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
550 if (i == 0) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
551 return OW_TEMP_NO_ROM;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
552 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
553
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
554 OWSendCmd(ROM, OW_RD_SCR_CMD);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
555 crc = 0;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
556 for (i = 0; i < 8; i++) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
557 buf[i] = OWReadByte();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
558 OWCRC(buf[i], &crc);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
559 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
560 buf[i] = OWReadByte();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
561 if (crc != buf[8])
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
562 return OW_TEMP_CRC_ERR;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
563 temp = buf[0];
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
564 if (buf[1] & 0x80)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
565 temp -= 256;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
566
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
567 /* Chop off 0.5 degree bit */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
568 temp >>= 1;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
569
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
570 /* Calulate the fractional remainder */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
571 tfrac = buf[7] - buf[6];
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
572
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
573 /* Work in 100'th of degreess to save on floats */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
574 tfrac *= (int16_t)100;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
575
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
576 /* Divide by count */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
577 tfrac /= buf[7];
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
578
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
579 /* Subtract 0.25 deg from temp */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
580 tfrac += 75;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
581 if (tfrac < 100)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
582 temp--;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
583 else
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
584 tfrac -= 100;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
585
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
586 i = temp;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
587 temp *= 100;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
588 temp += tfrac;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
589
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
590 return(temp);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 33
diff changeset
591 }