comparison simple_ds18b20.c @ 326:f6b5941b4c34

Untested - calculate crc in uart_putchar
author Matt Johnston <matt@ucc.asn.au>
date Tue, 22 May 2012 23:23:38 +0800
parents 9621436cfa07
children ca08442635ca
comparison
equal deleted inserted replaced
325:b05c9fd7c098 326:f6b5941b4c34
124 } 124 }
125 return ret; 125 return ret;
126 } 126 }
127 127
128 static void 128 static void
129 printhex_nibble(const unsigned char b) 129 printhex_nibble(const unsigned char b, FILE *stream)
130 { 130 {
131 unsigned char c = b & 0x0f; 131 unsigned char c = b & 0x0f;
132 if ( c > 9 ) { 132 if ( c > 9 ) {
133 c += 'A'-10; 133 c += 'A'-10;
134 } 134 }
135 else { 135 else {
136 c += '0'; 136 c += '0';
137 } 137 }
138 putchar(c); 138 fputc(c, stream);
139 } 139 }
140 140
141 void 141 void
142 printhex_byte( const unsigned char b ) 142 printhex_byte(const unsigned char b, FILE *stream)
143 { 143 {
144 printhex_nibble( b >> 4 ); 144 printhex_nibble( b >> 4, stream);
145 printhex_nibble( b ); 145 printhex_nibble( b, stream);
146 } 146 }
147 147
148 void 148 void
149 printhex(uint8_t *id, uint8_t n) 149 printhex(uint8_t *id, uint8_t n, FILE *stream)
150 { 150 {
151 for (uint8_t i = 0; i < n; i++) 151 for (uint8_t i = 0; i < n; i++)
152 { 152 {
153 if (i > 0) 153 if (i > 0)
154 { 154 {
155 putchar(' '); 155 fputc(' ', stream);
156 } 156 }
157 printhex_byte(id[i]); 157 printhex_byte(id[i], stream);
158 } 158 }
159 } 159 }
160 160
161 161
162 uint8_t 162 uint8_t
188 printf_P(PSTR("DS18B20 %d: "), diff); 188 printf_P(PSTR("DS18B20 %d: "), diff);
189 if (crc8(id, OW_ROMCODE_SIZE)) 189 if (crc8(id, OW_ROMCODE_SIZE))
190 { 190 {
191 printf_P(PSTR("CRC fail")); 191 printf_P(PSTR("CRC fail"));
192 } 192 }
193 printhex(id, OW_ROMCODE_SIZE); 193 printhex(id, OW_ROMCODE_SIZE, stdout);
194 printf_P(PSTR(" %d.%d ºC\n"), decicelsius/10, decicelsius % 10); 194 printf_P(PSTR(" %d.%d ºC\n"), decicelsius/10, decicelsius % 10);
195 } 195 }
196 printf_P(PSTR("Done sensors\n")); 196 printf_P(PSTR("Done sensors\n"));
197 return DS18X20_OK; 197 return DS18X20_OK;
198 } 198 }