diff 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
line wrap: on
line diff
--- a/1wire.c	Thu Nov 22 16:02:40 2007 +0000
+++ b/1wire.c	Sun Jul 06 22:19:53 2008 +0930
@@ -42,6 +42,7 @@
 #include <util/delay.h>
 #include "1wire.h"
 #include "1wire-config.h"
+#include "cons.h"
 
 static uint8_t OW_LastDevice = 0;
 static uint8_t OW_LastDiscrepancy = 0;
@@ -243,7 +244,7 @@
 	if (do_reset) {
 	    /* reset the 1-wire
 	     * if there are no parts on 1-wire, return 0 */
-	    OWPUTSP(PSTR("Resetting\n\r"));
+	    OWPUTSP(PSTR("Resetting\r\n"));
 	    switch (OWTouchReset()) {
 		case 0:
 		    break;
@@ -252,7 +253,7 @@
 		    /* reset the search */
 		    OW_LastDiscrepancy = 0;
 		    OW_LastFamilyDiscrepancy = 0;
-		    OWPUTSP(PSTR("No devices on bus\n\r"));
+		    OWPUTSP(PSTR("No devices on bus\r\n"));
 		    return OW_NOPRESENCE;
 		    break;
 		
@@ -260,7 +261,7 @@
 		    /* reset the search */
 		    OW_LastDiscrepancy = 0;
 		    OW_LastFamilyDiscrepancy = 0;
-		    OWPUTSP(PSTR("Bus appears to be being held low\n\r"));
+		    OWPUTSP(PSTR("Bus appears to be being held low\r\n"));
 		    return OW_BADWIRE;
 		break;
 		    
@@ -284,17 +285,11 @@
 	    bit_test = OWReadBit() << 1;
 	    bit_test |= OWReadBit();
 
-#if OW_DEBUG
-	    sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test);
-	    OWPUTSP(errstr);
-#endif
+	    OWPRINTFP(PSTR("bit_test = %d\r\n"), bit_test);
 
 	    /* check for no devices on 1-wire */
 	    if (bit_test == 3) {
-#if OW_DEBUG
-		sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test);
-		OWPUTSP(errstr);
-#endif
+		OWPRINTFP(PSTR("bit_test = %d\r\n"), bit_test);
 		return(OW_BADWIRE);
 	    }
 	    else {
@@ -350,19 +345,13 @@
 	/* if the search was successful then */
 	if (!(bit_number < 65) || lastcrc8) {
 	    if (lastcrc8) {
-#if OW_DEBUG
-		sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8);
-		OWPUTSP(errstr);
-#endif
+		OWPRINTFP(PSTR("Bad CRC (%d)\r\n"), lastcrc8);
 		next_result = OW_BADCRC;
 	    } else {
 		/*  search successful so set LastDiscrepancy,LastDevice,next_result */
 		OW_LastDiscrepancy = last_zero;
 		OW_LastDevice = (OW_LastDiscrepancy == 0);
-#if OW_DEBUG
-		sprintf_P(errstr, PSTR("Last device = %d\n\r"), OW_LastDevice);
-		OWPUTSP(errstr);
-#endif
+		OWPRINTFP(PSTR("Last device = %d\r\n"), OW_LastDevice);
 		next_result = OW_FOUND;
 	    }
 	}
@@ -449,7 +438,7 @@
     
     OWDELAY_I;
     if (OWTouchReset() != 0) {
-	uart_putsP(PSTR("No presence pulse\n\r"));
+	cons_putsP(PSTR("No presence pulse\r\n"));
 	return(3);
     }
     
@@ -474,11 +463,11 @@
     OWCRC(0x00, &crc);
 
     for (i = 0; i < len; i++) {
-	uart_putsP(PSTR("Programming "));
-	uart_puts_hex(data[i]);
-	uart_putsP(PSTR(" to "));
-	uart_puts_hex(start + i);
-	uart_putsP(PSTR("\n\r"));
+	cons_putsP(PSTR("Programming "));
+	cons_puts_hex(data[i]);
+	cons_putsP(PSTR(" to "));
+	cons_puts_hex(start + i);
+	cons_putsP(PSTR("\r\n"));
 
 	OWWriteByte(data[i]);
 	OWCRC(data[i], &crc);
@@ -486,11 +475,11 @@
 	tmp = OWReadByte();
 	
 	if (crc != tmp) {
-	    uart_putsP(PSTR("CRC mismatch "));
-	    uart_puts_hex(crc);
-	    uart_putsP(PSTR(" vs "));
-	    uart_puts_hex(tmp);
-	    uart_putsP(PSTR("\n\r"));
+	    cons_putsP(PSTR("CRC mismatch "));
+	    cons_puts_hex(crc);
+	    cons_putsP(PSTR(" vs "));
+	    cons_puts_hex(tmp);
+	    cons_putsP(PSTR("\r\n"));
 
 	    OWTouchReset();
 	    return(3);
@@ -509,11 +498,11 @@
 	        return(-3);
 */
 	if ((!data[i] & tmp) != 0) {
-	    uart_putsP(PSTR("Readback mismatch "));
-	    uart_puts_hex(data[i]);
-	    uart_putsP(PSTR(" vs "));
-	    uart_puts_hex(data[i]);
-	    uart_putsP(PSTR("\n\r"));
+	    cons_putsP(PSTR("Readback mismatch "));
+	    cons_puts_hex(data[i]);
+	    cons_putsP(PSTR(" vs "));
+	    cons_puts_hex(data[i]);
+	    cons_putsP(PSTR("\r\n"));
 	    
 	    OWTouchReset();
 	    return(3);
@@ -531,3 +520,72 @@
 #endif
 }
 
+/* 
+ * OWGetTemp
+ *
+ * Get the temperature from a 1wire bus module
+ *
+ * Returns temperature in hundredths of a degree or OW_TEMP_xxx on
+ * error.
+ */
+int16_t
+OWGetTemp(uint8_t *ROM) {
+    int8_t	i;
+    uint8_t	crc, buf[9];
+    int16_t	temp;
+    int16_t	tfrac;
+    
+    if (ROM[0] != OW_FAMILY_TEMP)
+	return OW_TEMP_WRONG_FAM;
+    
+    OWSendCmd(ROM, OW_CONVERTT_CMD);
+    
+    i = 0;
+
+    /* Wait for the conversion */
+    while (OWReadBit() == 0)
+	i = 1;
+
+    /* Check that we talked to a module and it did something  */
+    if (i == 0) {
+	return OW_TEMP_NO_ROM;
+    }
+    
+    OWSendCmd(ROM, OW_RD_SCR_CMD);
+    crc = 0;
+    for (i = 0; i < 8; i++) {
+	buf[i] = OWReadByte();
+	OWCRC(buf[i], &crc);
+    }
+    buf[i] = OWReadByte();
+    if (crc != buf[8])
+	return OW_TEMP_CRC_ERR;
+    temp = buf[0];
+    if (buf[1] & 0x80)
+	temp -= 256;
+
+    /* Chop off 0.5 degree bit */
+    temp >>= 1;
+
+    /* Calulate the fractional remainder */
+    tfrac = buf[7] - buf[6];
+
+    /* Work in 100'th of degreess to save on floats */
+    tfrac *= (int16_t)100;
+
+    /* Divide by count */
+    tfrac /= buf[7];
+
+    /* Subtract 0.25 deg from temp */
+    tfrac += 75;
+    if (tfrac < 100)
+	temp--;
+    else
+	tfrac -= 100;
+
+    i = temp;
+    temp *= 100;
+    temp += tfrac;
+
+    return(temp);
+}