Mercurial > ~darius > hgwebdir.cgi > tempctrl
comparison 1wire.c @ 32:b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
This means the user only has to edit a single file to suit their
situation and allows the code to work with active drive systems as
well as passive pullups (ie 1 vs 2 IO pins)
author | darius |
---|---|
date | Sun, 23 Apr 2006 22:57:16 +0930 |
parents | 02b67e09ca12 |
children | 0aa6bf4b98ae |
comparison
equal
deleted
inserted
replaced
31:4e417d84365e | 32:b0cb873c0206 |
---|---|
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 * SUCH DAMAGE. | 30 * SUCH DAMAGE. |
31 */ | 31 */ |
32 | 32 |
33 /* | |
34 * No user servicable parts inside | |
35 * | |
36 * Modify 1wire-config.h | |
37 */ | |
38 | |
33 #include <stdio.h> | 39 #include <stdio.h> |
34 #include <avr/io.h> | 40 #include <avr/io.h> |
35 #include <avr/pgmspace.h> | 41 #include <avr/pgmspace.h> |
36 #include <util/delay.h> | 42 #include <util/delay.h> |
37 #include "1wire.h" | 43 #include "1wire.h" |
38 #include "1wire-delay.h" | 44 #include "1wire-config.h" |
39 | 45 |
40 #if OW_DEBUG | 46 #if OW_DEBUG |
41 void uart_putsP(const char *addr); | 47 void uart_putsP(const char *addr); |
42 void uart_puts(const char *addr); | 48 void uart_puts(const char *addr); |
43 void uart_getc(); | 49 void uart_getc(); |
46 | 52 |
47 static uint8_t OW_LastDevice = 0; | 53 static uint8_t OW_LastDevice = 0; |
48 static uint8_t OW_LastDiscrepancy = 0; | 54 static uint8_t OW_LastDiscrepancy = 0; |
49 static uint8_t OW_LastFamilyDiscrepancy = 0; | 55 static uint8_t OW_LastFamilyDiscrepancy = 0; |
50 | 56 |
51 static void | 57 /*----------------------------------------------------------------------------- |
52 OWdelay(void) { | 58 * Configure the IO port as we need |
53 DELAY_I; | 59 */ |
60 void | |
61 OWInit(void) { | |
62 OWBUSINIT(); | |
63 OWSETBUSHIGH(); | |
54 } | 64 } |
55 | 65 |
56 /*----------------------------------------------------------------------------- | 66 /*----------------------------------------------------------------------------- |
57 * Generate a 1-Wire reset, return 0 if presence pulse was found, 1 if it | 67 * Generate a 1-Wire reset, return 0 if presence pulse was found, 1 if it |
58 * wasn't, or 2 if the line appears to be being held low. | 68 * wasn't, or 2 if the line appears to be being held low. |
59 * | 69 * |
60 * (NOTE: Does not handle alarm presence from DS2404/DS1994) | 70 * (NOTE: Does not handle alarm presence from DS2404/DS1994) |
61 */ | 71 */ |
62 uint8_t | 72 uint8_t |
63 OWTouchReset(void) { | 73 OWTouchReset(void) { |
64 DELAY_G; | 74 OWDELAY_G; |
65 | 75 |
66 if (!(OWIREINPORT & _BV(OWIREINPIN))) | 76 /* Check the bus isn't being held low (ie it's broken) Do it after |
77 * the delay so we guarantee we don't see a slave from a previous | |
78 * comms attempt | |
79 */ | |
80 if(OWREADBUS() == 0) | |
67 return 2; | 81 return 2; |
68 | 82 |
69 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); | 83 OWSETBUSLOW(); |
70 OWIREDDR |= _BV(OWIREOUTPIN); | 84 OWDELAY_H; |
71 DELAY_H; | 85 OWSETBUSHIGH(); |
72 OWIREDDR &= ~(_BV(OWIREOUTPIN)); | 86 OWDELAY_I; |
73 DELAY_I; | 87 |
74 | 88 return(OWREADBUS()); |
75 return(OWIREINPORT & _BV(OWIREINPIN) ? 1 : 0); | |
76 } | 89 } |
77 | 90 |
78 /*----------------------------------------------------------------------------- | 91 /*----------------------------------------------------------------------------- |
79 * Send a 1-wire write bit. | 92 * Send a 1-wire write bit. |
80 */ | 93 */ |
81 void | 94 void |
82 OWWriteBit(uint8_t bit) { | 95 OWWriteBit(uint8_t bit) { |
83 OWdelay(); | 96 OWDELAY_I; |
97 | |
84 if (bit) { | 98 if (bit) { |
85 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); | 99 OWSETBUSLOW(); |
86 OWIREDDR |= _BV(OWIREOUTPIN); | 100 OWDELAY_A; |
87 DELAY_A; | 101 OWSETBUSHIGH(); |
88 OWIREDDR &= ~(_BV(OWIREOUTPIN)); | 102 OWDELAY_B; |
89 DELAY_B; | |
90 } else { | 103 } else { |
91 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); | 104 OWSETBUSLOW(); |
92 OWIREDDR |= _BV(OWIREOUTPIN); | 105 OWDELAY_C; |
93 DELAY_C; | 106 OWSETBUSHIGH(); |
94 OWIREDDR &= ~(_BV(OWIREOUTPIN)); | 107 OWDELAY_D; |
95 DELAY_D; | |
96 } | 108 } |
97 } | 109 } |
98 | 110 |
99 /*----------------------------------------------------------------------------- | 111 /*----------------------------------------------------------------------------- |
100 * Read a bit from the 1-wire bus and return it. | 112 * Read a bit from the 1-wire bus and return it. |
101 */ | 113 */ |
102 uint8_t | 114 uint8_t |
103 OWReadBit(void) { | 115 OWReadBit(void) { |
104 OWdelay(); | 116 OWDELAY_I; |
105 | 117 |
106 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); | 118 OWSETBUSLOW(); |
107 OWIREDDR |= _BV(OWIREOUTPIN); | 119 OWDELAY_A; |
108 DELAY_A; | 120 OWSETBUSHIGH(); |
109 OWIREDDR &= ~(_BV(OWIREOUTPIN)); | 121 OWDELAY_E; |
110 DELAY_E; | 122 return(OWREADBUS()); |
111 return(OWIREINPORT & _BV(OWIREINPIN) ? 1 : 0); | |
112 } | 123 } |
113 | 124 |
114 /*----------------------------------------------------------------------------- | 125 /*----------------------------------------------------------------------------- |
115 * Write a byte to the 1-wire bus | 126 * Write a byte to the 1-wire bus |
116 */ | 127 */ |
190 else { | 201 else { |
191 OWWriteByte(OW_MATCH_ROM_CMD); | 202 OWWriteByte(OW_MATCH_ROM_CMD); |
192 for (i = 0; i < 8; i++) | 203 for (i = 0; i < 8; i++) |
193 OWWriteByte(ROM[i]); | 204 OWWriteByte(ROM[i]); |
194 } | 205 } |
195 | |
196 OWWriteByte(cmd); | 206 OWWriteByte(cmd); |
197 } | 207 } |
198 | 208 |
199 /*----------------------------------------------------------------------------- | 209 /*----------------------------------------------------------------------------- |
200 * Search algorithm from App note 187 (and 162) | 210 * Search algorithm from App note 187 (and 162) |
275 OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */ | 285 OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */ |
276 else | 286 else |
277 OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */ | 287 OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */ |
278 | 288 |
279 /* pause before beginning the search */ | 289 /* pause before beginning the search */ |
280 OWdelay(); | 290 OWDELAY_I; |
281 OWdelay(); | 291 OWDELAY_I; |
282 OWdelay(); | 292 OWDELAY_I; |
283 | 293 |
284 /* loop to do the search */ | 294 /* loop to do the search */ |
285 do { | 295 do { |
286 /* read a bit and its compliment */ | 296 /* read a bit and its compliment */ |
287 bit_test = OWReadBit() << 1; | 297 bit_test = OWReadBit() << 1; |
288 bit_test |= OWReadBit(); | 298 bit_test |= OWReadBit(); |