comparison simple_ds18b20.c @ 115:90d1ebb941ab

untested fridge control
author Matt Johnston <matt@ucc.asn.au>
date Wed, 03 Oct 2012 23:40:03 +0800
parents ca08442635ca
children 295a4b875677
comparison
equal deleted inserted replaced
114:b0ec7b783c0b 115:90d1ebb941ab
45 } 45 }
46 46
47 return ret; 47 return ret;
48 } 48 }
49 49
50 static int16_t 50 int16_t
51 ds18b20_raw_to_decicelsius( uint8_t sp[] ) 51 ds18b20_raw16_to_decicelsius(uint16_t measure)
52 { 52 {
53 uint16_t measure;
54 uint8_t negative; 53 uint8_t negative;
55 int16_t decicelsius; 54 int16_t decicelsius;
56 uint16_t fract; 55 uint16_t fract;
57
58 measure = sp[0] | (sp[1] << 8);
59 //measure = 0xFF5E; // test -10.125
60 //measure = 0xFE6F; // test -25.0625
61 56
62 // check for negative 57 // check for negative
63 if ( measure & 0x8000 ) { 58 if ( measure & 0x8000 ) {
64 negative = 1; // mark negative 59 negative = 1; // mark negative
65 measure ^= 0xffff; // convert to positive => (twos complement)++ 60 measure ^= 0xffff; // convert to positive => (twos complement)++
66 measure++; 61 measure++;
67 } 62 }
68 else { 63 else {
69 negative = 0; 64 negative = 0;
70 }
71
72 // clear undefined bits for DS18B20 != 12bit resolution
73 switch( sp[DS18B20_CONF_REG] & DS18B20_RES_MASK ) {
74 case DS18B20_9_BIT:
75 measure &= ~(DS18B20_9_BIT_UNDF);
76 break;
77 case DS18B20_10_BIT:
78 measure &= ~(DS18B20_10_BIT_UNDF);
79 break;
80 case DS18B20_11_BIT:
81 measure &= ~(DS18B20_11_BIT_UNDF);
82 break;
83 default:
84 // 12 bit - all bits valid
85 break;
86 } 65 }
87 66
88 decicelsius = (measure >> 4); 67 decicelsius = (measure >> 4);
89 decicelsius *= 10; 68 decicelsius *= 10;
90 69
109 } 88 }
110 89
111 uint8_t 90 uint8_t
112 simple_ds18b20_read_decicelsius( uint8_t id[], int16_t *decicelsius ) 91 simple_ds18b20_read_decicelsius( uint8_t id[], int16_t *decicelsius )
113 { 92 {
114 uint8_t sp[DS18X20_SP_SIZE]; 93 uint16_t reading;
115 uint8_t ret; 94 uint8_t ret;
116 95
117 if (id) 96 ret = simple_ds18b20_read_raw(id, &reading);
118 { 97 if (ret == DS18X20_OK)
119 ow_reset(); 98 {
120 } 99 *decicelsius = ds18b20_raw16_to_decicelsius(reading);
121 ret = read_scratchpad( id, sp, DS18X20_SP_SIZE ); 100 }
122 if ( ret == DS18X20_OK ) { 101 return ret;
123 *decicelsius = ds18b20_raw_to_decicelsius( sp );
124 }
125 return ret;
126 } 102 }
127 103
128 uint8_t 104 uint8_t
129 simple_ds18b20_read_raw( uint8_t id[], uint16_t *reading ) 105 simple_ds18b20_read_raw( uint8_t id[], uint16_t *reading )
130 { 106 {