Realtime
Fork of DS1820 by
DS1820.cpp@5:f77b9f69df9e, 2016-10-09 (annotated)
- Committer:
- GlimwormBeacons
- Date:
- Sun Oct 09 09:45:34 2016 +0000
- Revision:
- 5:f77b9f69df9e
- Parent:
- 4:29264b0a2c9f
made it real time for nrf51822 using nrf_delay_us instead of timer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pairmand | 3:8f2b7f4940b5 | 1 | #include "DS1820.h" |
pairmand | 3:8f2b7f4940b5 | 2 | #include "mbed.h" |
pairmand | 3:8f2b7f4940b5 | 3 | |
pairmand | 3:8f2b7f4940b5 | 4 | // Global variables shared between all DS1820 objects |
pairmand | 3:8f2b7f4940b5 | 5 | bool DS1820_done_flag; |
pairmand | 3:8f2b7f4940b5 | 6 | int DS1820_last_descrepancy; |
pairmand | 3:8f2b7f4940b5 | 7 | char DS1820_search_ROM[8]; |
pairmand | 3:8f2b7f4940b5 | 8 | |
GlimwormBeacons | 5:f77b9f69df9e | 9 | |
GlimwormBeacons | 5:f77b9f69df9e | 10 | |
GlimwormBeacons | 5:f77b9f69df9e | 11 | |
GlimwormBeacons | 5:f77b9f69df9e | 12 | #if defined ( __CC_ARM ) |
GlimwormBeacons | 5:f77b9f69df9e | 13 | static __ASM void __INLINE nrf_delay_us(uint32_t volatile number_of_us) |
GlimwormBeacons | 5:f77b9f69df9e | 14 | { |
GlimwormBeacons | 5:f77b9f69df9e | 15 | loop |
GlimwormBeacons | 5:f77b9f69df9e | 16 | SUBS R0, R0, #1 |
GlimwormBeacons | 5:f77b9f69df9e | 17 | NOP |
GlimwormBeacons | 5:f77b9f69df9e | 18 | NOP |
GlimwormBeacons | 5:f77b9f69df9e | 19 | NOP |
GlimwormBeacons | 5:f77b9f69df9e | 20 | NOP |
GlimwormBeacons | 5:f77b9f69df9e | 21 | NOP |
GlimwormBeacons | 5:f77b9f69df9e | 22 | NOP |
GlimwormBeacons | 5:f77b9f69df9e | 23 | NOP |
GlimwormBeacons | 5:f77b9f69df9e | 24 | NOP |
GlimwormBeacons | 5:f77b9f69df9e | 25 | NOP |
GlimwormBeacons | 5:f77b9f69df9e | 26 | NOP |
GlimwormBeacons | 5:f77b9f69df9e | 27 | NOP |
GlimwormBeacons | 5:f77b9f69df9e | 28 | NOP |
GlimwormBeacons | 5:f77b9f69df9e | 29 | BNE loop |
GlimwormBeacons | 5:f77b9f69df9e | 30 | BX LR |
GlimwormBeacons | 5:f77b9f69df9e | 31 | } |
GlimwormBeacons | 5:f77b9f69df9e | 32 | #elif defined ( __ICCARM__ ) |
GlimwormBeacons | 5:f77b9f69df9e | 33 | static void __INLINE nrf_delay_us(uint32_t volatile number_of_us) |
GlimwormBeacons | 5:f77b9f69df9e | 34 | { |
GlimwormBeacons | 5:f77b9f69df9e | 35 | __ASM ( |
GlimwormBeacons | 5:f77b9f69df9e | 36 | "loop:\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 37 | " SUBS R0, R0, #1\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 38 | " NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 39 | " NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 40 | " NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 41 | " NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 42 | " NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 43 | " NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 44 | " NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 45 | " NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 46 | " NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 47 | " NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 48 | " NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 49 | " NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 50 | " BNE loop\n\t"); |
GlimwormBeacons | 5:f77b9f69df9e | 51 | } |
GlimwormBeacons | 5:f77b9f69df9e | 52 | #elif defined ( __GNUC__ ) |
GlimwormBeacons | 5:f77b9f69df9e | 53 | __INLINE static void nrf_delay_us(uint32_t volatile number_of_us) |
GlimwormBeacons | 5:f77b9f69df9e | 54 | { |
GlimwormBeacons | 5:f77b9f69df9e | 55 | do |
GlimwormBeacons | 5:f77b9f69df9e | 56 | { |
GlimwormBeacons | 5:f77b9f69df9e | 57 | __ASM volatile ( |
GlimwormBeacons | 5:f77b9f69df9e | 58 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 59 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 60 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 61 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 62 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 63 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 64 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 65 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 66 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 67 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 68 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 69 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 70 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 71 | "NOP\n\t" |
GlimwormBeacons | 5:f77b9f69df9e | 72 | ); |
GlimwormBeacons | 5:f77b9f69df9e | 73 | } while (--number_of_us); |
GlimwormBeacons | 5:f77b9f69df9e | 74 | } |
GlimwormBeacons | 5:f77b9f69df9e | 75 | #endif |
pairmand | 3:8f2b7f4940b5 | 76 | |
pairmand | 3:8f2b7f4940b5 | 77 | DS1820::DS1820 (PinName data_pin, PinName power_pin) : _datapin(data_pin), _parasitepin(power_pin) { |
pairmand | 3:8f2b7f4940b5 | 78 | int byte_counter; |
pairmand | 3:8f2b7f4940b5 | 79 | _parasite_power = true; |
pairmand | 3:8f2b7f4940b5 | 80 | for(byte_counter=0;byte_counter<8;byte_counter++) |
pairmand | 3:8f2b7f4940b5 | 81 | ROM[byte_counter] = 0xFF; |
pairmand | 3:8f2b7f4940b5 | 82 | for(byte_counter=0;byte_counter<9;byte_counter++) |
pairmand | 3:8f2b7f4940b5 | 83 | RAM[byte_counter] = 0x00; |
pairmand | 3:8f2b7f4940b5 | 84 | } |
pairmand | 3:8f2b7f4940b5 | 85 | DS1820::DS1820 (PinName data_pin) : _datapin(data_pin), _parasitepin(NC) { |
pairmand | 3:8f2b7f4940b5 | 86 | int byte_counter; |
pairmand | 3:8f2b7f4940b5 | 87 | _parasite_power = false; |
pairmand | 3:8f2b7f4940b5 | 88 | for(byte_counter=0;byte_counter<8;byte_counter++) |
pairmand | 3:8f2b7f4940b5 | 89 | ROM[byte_counter] = 0xFF; |
pairmand | 3:8f2b7f4940b5 | 90 | for(byte_counter=0;byte_counter<9;byte_counter++) |
pairmand | 3:8f2b7f4940b5 | 91 | RAM[byte_counter] = 0x00; |
pairmand | 3:8f2b7f4940b5 | 92 | } |
pairmand | 3:8f2b7f4940b5 | 93 | |
pairmand | 3:8f2b7f4940b5 | 94 | bool DS1820::onewire_reset() { |
pairmand | 3:8f2b7f4940b5 | 95 | // This will return false if no devices are present on the data bus |
pairmand | 3:8f2b7f4940b5 | 96 | bool presence=false; |
pairmand | 3:8f2b7f4940b5 | 97 | _datapin.output(); |
pairmand | 3:8f2b7f4940b5 | 98 | _datapin = 0; // bring low for 500 us |
GlimwormBeacons | 5:f77b9f69df9e | 99 | nrf_delay_us(500); |
pairmand | 3:8f2b7f4940b5 | 100 | _datapin.input(); // let the data line float high |
GlimwormBeacons | 5:f77b9f69df9e | 101 | nrf_delay_us(90); // wait 90us |
pairmand | 3:8f2b7f4940b5 | 102 | if (_datapin.read()==0) // see if any devices are pulling the data line low |
pairmand | 3:8f2b7f4940b5 | 103 | presence=true; |
GlimwormBeacons | 5:f77b9f69df9e | 104 | nrf_delay_us(410); |
pairmand | 3:8f2b7f4940b5 | 105 | return presence; |
pairmand | 3:8f2b7f4940b5 | 106 | } |
pairmand | 3:8f2b7f4940b5 | 107 | |
pairmand | 3:8f2b7f4940b5 | 108 | void DS1820::onewire_bit_out (bool bit_data) { |
pairmand | 3:8f2b7f4940b5 | 109 | _datapin.output(); |
pairmand | 3:8f2b7f4940b5 | 110 | _datapin = 0; |
GlimwormBeacons | 5:f77b9f69df9e | 111 | nrf_delay_us(3); // DXP modified from 5 |
pairmand | 3:8f2b7f4940b5 | 112 | if (bit_data) { |
pairmand | 3:8f2b7f4940b5 | 113 | _datapin.input(); // bring data line high |
GlimwormBeacons | 5:f77b9f69df9e | 114 | nrf_delay_us(55); |
pairmand | 3:8f2b7f4940b5 | 115 | } else { |
GlimwormBeacons | 5:f77b9f69df9e | 116 | nrf_delay_us(55); // keep data line low |
pairmand | 3:8f2b7f4940b5 | 117 | _datapin.input(); |
GlimwormBeacons | 5:f77b9f69df9e | 118 | nrf_delay_us(10); // DXP added to allow bus to float high before next bit_out |
pairmand | 3:8f2b7f4940b5 | 119 | } |
pairmand | 3:8f2b7f4940b5 | 120 | } |
pairmand | 3:8f2b7f4940b5 | 121 | |
pairmand | 3:8f2b7f4940b5 | 122 | void DS1820::onewire_byte_out(char data) { // output data character (least sig bit first). |
pairmand | 3:8f2b7f4940b5 | 123 | int n; |
pairmand | 3:8f2b7f4940b5 | 124 | for (n=0; n<8; n++) { |
pairmand | 3:8f2b7f4940b5 | 125 | onewire_bit_out(data & 0x01); |
pairmand | 3:8f2b7f4940b5 | 126 | data = data >> 1; // now the next bit is in the least sig bit position. |
pairmand | 3:8f2b7f4940b5 | 127 | } |
pairmand | 3:8f2b7f4940b5 | 128 | } |
pairmand | 3:8f2b7f4940b5 | 129 | |
pairmand | 3:8f2b7f4940b5 | 130 | bool DS1820::onewire_bit_in() { |
pairmand | 3:8f2b7f4940b5 | 131 | bool answer; |
pairmand | 3:8f2b7f4940b5 | 132 | _datapin.output(); |
pairmand | 3:8f2b7f4940b5 | 133 | _datapin = 0; |
GlimwormBeacons | 5:f77b9f69df9e | 134 | nrf_delay_us(3); // DXP modofied from 5 |
pairmand | 3:8f2b7f4940b5 | 135 | _datapin.input(); |
GlimwormBeacons | 5:f77b9f69df9e | 136 | nrf_delay_us(10); // DXP modified from 5 |
pairmand | 3:8f2b7f4940b5 | 137 | answer = _datapin.read(); |
GlimwormBeacons | 5:f77b9f69df9e | 138 | nrf_delay_us(45); // DXP modified from 50 |
pairmand | 3:8f2b7f4940b5 | 139 | return answer; |
pairmand | 3:8f2b7f4940b5 | 140 | } |
pairmand | 3:8f2b7f4940b5 | 141 | |
pairmand | 3:8f2b7f4940b5 | 142 | char DS1820::onewire_byte_in() { // read byte, least sig byte first |
pairmand | 3:8f2b7f4940b5 | 143 | char answer = 0x00; |
pairmand | 3:8f2b7f4940b5 | 144 | int i; |
pairmand | 3:8f2b7f4940b5 | 145 | for (i=0; i<8; i++) { |
pairmand | 3:8f2b7f4940b5 | 146 | answer = answer >> 1; // shift over to make room for the next bit |
pairmand | 3:8f2b7f4940b5 | 147 | if (onewire_bit_in()) |
pairmand | 3:8f2b7f4940b5 | 148 | answer = answer | 0x80; // if the data port is high, make this bit a 1 |
pairmand | 3:8f2b7f4940b5 | 149 | } |
pairmand | 3:8f2b7f4940b5 | 150 | return answer; |
pairmand | 3:8f2b7f4940b5 | 151 | } |
pairmand | 3:8f2b7f4940b5 | 152 | |
pairmand | 3:8f2b7f4940b5 | 153 | bool DS1820::search_ROM() { |
pairmand | 3:8f2b7f4940b5 | 154 | return search_ROM_routine(0xF0); // Search ROM command |
pairmand | 3:8f2b7f4940b5 | 155 | } |
pairmand | 3:8f2b7f4940b5 | 156 | |
pairmand | 3:8f2b7f4940b5 | 157 | bool DS1820::search_alarm() { |
pairmand | 3:8f2b7f4940b5 | 158 | return search_ROM_routine(0xEC); // Search Alarm command |
pairmand | 3:8f2b7f4940b5 | 159 | } |
pairmand | 3:8f2b7f4940b5 | 160 | |
pairmand | 3:8f2b7f4940b5 | 161 | bool DS1820::search_ROM_routine(char command) { |
pairmand | 3:8f2b7f4940b5 | 162 | extern bool DS1820_done_flag; |
pairmand | 3:8f2b7f4940b5 | 163 | extern int DS1820_last_descrepancy; |
pairmand | 3:8f2b7f4940b5 | 164 | extern char DS1820_search_ROM[8]; |
pairmand | 3:8f2b7f4940b5 | 165 | int descrepancy_marker, ROM_bit_index; |
pairmand | 3:8f2b7f4940b5 | 166 | bool return_value, Bit_A, Bit_B; |
pairmand | 3:8f2b7f4940b5 | 167 | char byte_counter, bit_mask; |
pairmand | 3:8f2b7f4940b5 | 168 | |
pairmand | 3:8f2b7f4940b5 | 169 | return_value=false; |
pairmand | 3:8f2b7f4940b5 | 170 | if (!DS1820_done_flag) { |
pairmand | 3:8f2b7f4940b5 | 171 | if (!onewire_reset()) { |
pairmand | 3:8f2b7f4940b5 | 172 | DS1820_last_descrepancy = 0; // no devices present |
pairmand | 3:8f2b7f4940b5 | 173 | } else { |
pairmand | 3:8f2b7f4940b5 | 174 | ROM_bit_index=1; |
pairmand | 3:8f2b7f4940b5 | 175 | descrepancy_marker=0; |
pairmand | 3:8f2b7f4940b5 | 176 | onewire_byte_out(command); // Search ROM command or Search Alarm command |
pairmand | 3:8f2b7f4940b5 | 177 | byte_counter = 0; |
pairmand | 3:8f2b7f4940b5 | 178 | bit_mask = 0x01; |
pairmand | 3:8f2b7f4940b5 | 179 | while (ROM_bit_index<=64) { |
pairmand | 3:8f2b7f4940b5 | 180 | Bit_A = onewire_bit_in(); |
pairmand | 3:8f2b7f4940b5 | 181 | Bit_B = onewire_bit_in(); |
pairmand | 3:8f2b7f4940b5 | 182 | if (Bit_A & Bit_B) { |
pairmand | 3:8f2b7f4940b5 | 183 | descrepancy_marker = 0; // data read error, this should never happen |
pairmand | 3:8f2b7f4940b5 | 184 | ROM_bit_index = 0xFF; |
pairmand | 3:8f2b7f4940b5 | 185 | } else { |
pairmand | 3:8f2b7f4940b5 | 186 | if (Bit_A | Bit_B) { |
pairmand | 3:8f2b7f4940b5 | 187 | // Set ROM bit to Bit_A |
pairmand | 3:8f2b7f4940b5 | 188 | if (Bit_A) { |
pairmand | 3:8f2b7f4940b5 | 189 | DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one |
pairmand | 3:8f2b7f4940b5 | 190 | } else { |
pairmand | 3:8f2b7f4940b5 | 191 | DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero |
pairmand | 3:8f2b7f4940b5 | 192 | } |
pairmand | 3:8f2b7f4940b5 | 193 | } else { |
pairmand | 3:8f2b7f4940b5 | 194 | // both bits A and B are low, so there are two or more devices present |
pairmand | 3:8f2b7f4940b5 | 195 | if ( ROM_bit_index == DS1820_last_descrepancy ) { |
pairmand | 3:8f2b7f4940b5 | 196 | DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one |
pairmand | 3:8f2b7f4940b5 | 197 | } else { |
pairmand | 3:8f2b7f4940b5 | 198 | if ( ROM_bit_index > DS1820_last_descrepancy ) { |
pairmand | 3:8f2b7f4940b5 | 199 | DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero |
pairmand | 3:8f2b7f4940b5 | 200 | descrepancy_marker = ROM_bit_index; |
pairmand | 3:8f2b7f4940b5 | 201 | } else { |
pairmand | 3:8f2b7f4940b5 | 202 | if (( DS1820_search_ROM[byte_counter] & bit_mask) == 0x00 ) |
pairmand | 3:8f2b7f4940b5 | 203 | descrepancy_marker = ROM_bit_index; |
pairmand | 3:8f2b7f4940b5 | 204 | } |
pairmand | 3:8f2b7f4940b5 | 205 | } |
pairmand | 3:8f2b7f4940b5 | 206 | } |
pairmand | 3:8f2b7f4940b5 | 207 | onewire_bit_out (DS1820_search_ROM[byte_counter] & bit_mask); |
pairmand | 3:8f2b7f4940b5 | 208 | ROM_bit_index++; |
pairmand | 3:8f2b7f4940b5 | 209 | if (bit_mask & 0x80) { |
pairmand | 3:8f2b7f4940b5 | 210 | byte_counter++; |
pairmand | 3:8f2b7f4940b5 | 211 | bit_mask = 0x01; |
pairmand | 3:8f2b7f4940b5 | 212 | } else { |
pairmand | 3:8f2b7f4940b5 | 213 | bit_mask = bit_mask << 1; |
pairmand | 3:8f2b7f4940b5 | 214 | } |
pairmand | 3:8f2b7f4940b5 | 215 | } |
pairmand | 3:8f2b7f4940b5 | 216 | } |
pairmand | 3:8f2b7f4940b5 | 217 | DS1820_last_descrepancy = descrepancy_marker; |
pairmand | 3:8f2b7f4940b5 | 218 | if (ROM_bit_index != 0xFF) { |
pairmand | 3:8f2b7f4940b5 | 219 | for(byte_counter=0;byte_counter<8;byte_counter++) |
pairmand | 3:8f2b7f4940b5 | 220 | ROM[byte_counter] = DS1820_search_ROM[byte_counter]; |
pairmand | 4:29264b0a2c9f | 221 | if (ROM_checksum_error()) // Check the CRC |
pairmand | 4:29264b0a2c9f | 222 | DS1820_last_descrepancy = 0; // Abort any more search |
pairmand | 4:29264b0a2c9f | 223 | else |
pairmand | 4:29264b0a2c9f | 224 | return_value = true; |
pairmand | 3:8f2b7f4940b5 | 225 | } |
pairmand | 3:8f2b7f4940b5 | 226 | } |
pairmand | 3:8f2b7f4940b5 | 227 | if (DS1820_last_descrepancy == 0) |
pairmand | 3:8f2b7f4940b5 | 228 | DS1820_done_flag = true; |
pairmand | 3:8f2b7f4940b5 | 229 | } |
pairmand | 3:8f2b7f4940b5 | 230 | return return_value; |
pairmand | 3:8f2b7f4940b5 | 231 | } |
pairmand | 3:8f2b7f4940b5 | 232 | |
pairmand | 3:8f2b7f4940b5 | 233 | void DS1820::search_ROM_setup() { |
pairmand | 3:8f2b7f4940b5 | 234 | extern bool DS1820_done_flag; |
pairmand | 3:8f2b7f4940b5 | 235 | extern int DS1820_last_descrepancy; |
pairmand | 3:8f2b7f4940b5 | 236 | extern char DS1820_search_ROM[8]; |
pairmand | 3:8f2b7f4940b5 | 237 | DS1820_done_flag = false; |
pairmand | 3:8f2b7f4940b5 | 238 | DS1820_last_descrepancy = 0; |
pairmand | 3:8f2b7f4940b5 | 239 | int i; |
pairmand | 3:8f2b7f4940b5 | 240 | for (i=0; i<8; i++) |
pairmand | 3:8f2b7f4940b5 | 241 | DS1820_search_ROM[i]=0x00; |
pairmand | 3:8f2b7f4940b5 | 242 | } |
pairmand | 3:8f2b7f4940b5 | 243 | |
pairmand | 3:8f2b7f4940b5 | 244 | void DS1820::read_ROM() { |
pairmand | 3:8f2b7f4940b5 | 245 | // NOTE: This command can only be used when there is one DS1820 on the bus. If this command |
pairmand | 3:8f2b7f4940b5 | 246 | // is used when there is more than one slave present on the bus, a data collision will occur |
pairmand | 3:8f2b7f4940b5 | 247 | // when all the DS1820s attempt to respond at the same time. |
pairmand | 3:8f2b7f4940b5 | 248 | int i; |
pairmand | 3:8f2b7f4940b5 | 249 | onewire_reset(); |
pairmand | 3:8f2b7f4940b5 | 250 | onewire_byte_out(0x33); // Read ROM id |
pairmand | 3:8f2b7f4940b5 | 251 | for (i=0; i<8; i++) |
pairmand | 3:8f2b7f4940b5 | 252 | ROM[i]=onewire_byte_in(); |
pairmand | 3:8f2b7f4940b5 | 253 | } |
pairmand | 3:8f2b7f4940b5 | 254 | |
pairmand | 3:8f2b7f4940b5 | 255 | void DS1820::match_ROM() { |
pairmand | 3:8f2b7f4940b5 | 256 | // Used to select a specific device |
pairmand | 3:8f2b7f4940b5 | 257 | int i; |
pairmand | 3:8f2b7f4940b5 | 258 | onewire_reset(); |
pairmand | 3:8f2b7f4940b5 | 259 | onewire_byte_out( 0x55); //Match ROM command |
pairmand | 3:8f2b7f4940b5 | 260 | for (i=0;i<8;i++) |
pairmand | 3:8f2b7f4940b5 | 261 | onewire_byte_out(ROM[i]); |
pairmand | 3:8f2b7f4940b5 | 262 | } |
pairmand | 3:8f2b7f4940b5 | 263 | |
pairmand | 3:8f2b7f4940b5 | 264 | void DS1820::skip_ROM() { |
pairmand | 3:8f2b7f4940b5 | 265 | onewire_reset(); |
pairmand | 3:8f2b7f4940b5 | 266 | onewire_byte_out(0xCC); // Skip ROM command |
pairmand | 3:8f2b7f4940b5 | 267 | } |
pairmand | 3:8f2b7f4940b5 | 268 | |
pairmand | 3:8f2b7f4940b5 | 269 | bool DS1820::ROM_checksum_error() { |
pairmand | 3:8f2b7f4940b5 | 270 | char CRC=0x00; |
pairmand | 3:8f2b7f4940b5 | 271 | int i; |
pairmand | 3:8f2b7f4940b5 | 272 | for(i=0;i<7;i++) // Only going to shift the lower 7 bytes |
pairmand | 3:8f2b7f4940b5 | 273 | CRC = CRC_byte(CRC, ROM[i]); |
pairmand | 3:8f2b7f4940b5 | 274 | // After 7 bytes CRC should equal the 8th byte (ROM CRC) |
pairmand | 3:8f2b7f4940b5 | 275 | return (CRC!=ROM[7]); // will return true if there is a CRC checksum mis-match |
pairmand | 3:8f2b7f4940b5 | 276 | } |
pairmand | 3:8f2b7f4940b5 | 277 | |
pairmand | 3:8f2b7f4940b5 | 278 | bool DS1820::RAM_checksum_error() { |
pairmand | 3:8f2b7f4940b5 | 279 | char CRC=0x00; |
pairmand | 3:8f2b7f4940b5 | 280 | int i; |
pairmand | 3:8f2b7f4940b5 | 281 | for(i=0;i<8;i++) // Only going to shift the lower 8 bytes |
pairmand | 3:8f2b7f4940b5 | 282 | CRC = CRC_byte(CRC, RAM[i]); |
pairmand | 3:8f2b7f4940b5 | 283 | // After 8 bytes CRC should equal the 9th byte (RAM CRC) |
pairmand | 3:8f2b7f4940b5 | 284 | return (CRC!=RAM[8]); // will return true if there is a CRC checksum mis-match |
pairmand | 3:8f2b7f4940b5 | 285 | } |
pairmand | 3:8f2b7f4940b5 | 286 | |
pairmand | 3:8f2b7f4940b5 | 287 | char DS1820::CRC_byte (char CRC, char byte ) { |
pairmand | 3:8f2b7f4940b5 | 288 | int j; |
pairmand | 3:8f2b7f4940b5 | 289 | for(j=0;j<8;j++) { |
pairmand | 3:8f2b7f4940b5 | 290 | if ((byte & 0x01 ) ^ (CRC & 0x01)) { |
pairmand | 3:8f2b7f4940b5 | 291 | // DATA ^ LSB CRC = 1 |
pairmand | 3:8f2b7f4940b5 | 292 | CRC = CRC>>1; |
pairmand | 3:8f2b7f4940b5 | 293 | // Set the MSB to 1 |
pairmand | 3:8f2b7f4940b5 | 294 | CRC = CRC | 0x80; |
pairmand | 3:8f2b7f4940b5 | 295 | // Check bit 3 |
pairmand | 3:8f2b7f4940b5 | 296 | if (CRC & 0x04) { |
pairmand | 3:8f2b7f4940b5 | 297 | CRC = CRC & 0xFB; // Bit 3 is set, so clear it |
pairmand | 3:8f2b7f4940b5 | 298 | } else { |
pairmand | 3:8f2b7f4940b5 | 299 | CRC = CRC | 0x04; // Bit 3 is clear, so set it |
pairmand | 3:8f2b7f4940b5 | 300 | } |
pairmand | 3:8f2b7f4940b5 | 301 | // Check bit 4 |
pairmand | 3:8f2b7f4940b5 | 302 | if (CRC & 0x08) { |
pairmand | 3:8f2b7f4940b5 | 303 | CRC = CRC & 0xF7; // Bit 4 is set, so clear it |
pairmand | 3:8f2b7f4940b5 | 304 | } else { |
pairmand | 3:8f2b7f4940b5 | 305 | CRC = CRC | 0x08; // Bit 4 is clear, so set it |
pairmand | 3:8f2b7f4940b5 | 306 | } |
pairmand | 3:8f2b7f4940b5 | 307 | } else { |
pairmand | 3:8f2b7f4940b5 | 308 | // DATA ^ LSB CRC = 0 |
pairmand | 3:8f2b7f4940b5 | 309 | CRC = CRC>>1; |
pairmand | 3:8f2b7f4940b5 | 310 | // clear MSB |
pairmand | 3:8f2b7f4940b5 | 311 | CRC = CRC & 0x7F; |
pairmand | 3:8f2b7f4940b5 | 312 | // No need to check bits, with DATA ^ LSB CRC = 0, they will remain unchanged |
pairmand | 3:8f2b7f4940b5 | 313 | } |
pairmand | 3:8f2b7f4940b5 | 314 | byte = byte>>1; |
pairmand | 3:8f2b7f4940b5 | 315 | } |
pairmand | 3:8f2b7f4940b5 | 316 | return CRC; |
pairmand | 3:8f2b7f4940b5 | 317 | } |
pairmand | 3:8f2b7f4940b5 | 318 | |
pairmand | 3:8f2b7f4940b5 | 319 | int DS1820::convert_temperature(bool wait, devices device) { |
pairmand | 3:8f2b7f4940b5 | 320 | // Convert temperature into scratchpad RAM for all devices at once |
pairmand | 3:8f2b7f4940b5 | 321 | int delay_time = 750; // Default delay time |
pairmand | 3:8f2b7f4940b5 | 322 | char resolution; |
pairmand | 3:8f2b7f4940b5 | 323 | if (device==all_devices) |
pairmand | 3:8f2b7f4940b5 | 324 | skip_ROM(); // Skip ROM command, will convert for ALL devices |
pairmand | 3:8f2b7f4940b5 | 325 | else { |
pairmand | 3:8f2b7f4940b5 | 326 | match_ROM(); |
pairmand | 3:8f2b7f4940b5 | 327 | if (FAMILY_CODE == FAMILY_CODE_DS18B20 ) { |
pairmand | 3:8f2b7f4940b5 | 328 | resolution = RAM[4] & 0x60; |
pairmand | 3:8f2b7f4940b5 | 329 | if (resolution == 0x00) // 9 bits |
pairmand | 3:8f2b7f4940b5 | 330 | delay_time = 94; |
pairmand | 3:8f2b7f4940b5 | 331 | if (resolution == 0x20) // 10 bits |
pairmand | 3:8f2b7f4940b5 | 332 | delay_time = 188; |
pairmand | 3:8f2b7f4940b5 | 333 | if (resolution == 0x40) // 11 bits. Note 12bits uses the 750ms default |
pairmand | 3:8f2b7f4940b5 | 334 | delay_time = 375; |
pairmand | 3:8f2b7f4940b5 | 335 | } |
pairmand | 3:8f2b7f4940b5 | 336 | } |
pairmand | 3:8f2b7f4940b5 | 337 | onewire_byte_out( 0x44); // perform temperature conversion |
pairmand | 3:8f2b7f4940b5 | 338 | if (_parasite_power) { |
pairmand | 3:8f2b7f4940b5 | 339 | _parasitepin = 1; // Parasite power strong pullup |
pairmand | 3:8f2b7f4940b5 | 340 | wait_ms(delay_time); |
pairmand | 3:8f2b7f4940b5 | 341 | _parasitepin = 0; |
pairmand | 3:8f2b7f4940b5 | 342 | delay_time = 0; |
pairmand | 3:8f2b7f4940b5 | 343 | } else { |
pairmand | 3:8f2b7f4940b5 | 344 | if (wait) { |
pairmand | 3:8f2b7f4940b5 | 345 | wait_ms(delay_time); |
pairmand | 3:8f2b7f4940b5 | 346 | delay_time = 0; |
pairmand | 3:8f2b7f4940b5 | 347 | } |
pairmand | 3:8f2b7f4940b5 | 348 | } |
pairmand | 3:8f2b7f4940b5 | 349 | return delay_time; |
pairmand | 3:8f2b7f4940b5 | 350 | } |
pairmand | 3:8f2b7f4940b5 | 351 | |
pairmand | 3:8f2b7f4940b5 | 352 | void DS1820::read_RAM() { |
pairmand | 3:8f2b7f4940b5 | 353 | // This will copy the DS1820's 9 bytes of RAM data |
pairmand | 3:8f2b7f4940b5 | 354 | // into the objects RAM array. Functions that use |
pairmand | 3:8f2b7f4940b5 | 355 | // RAM values will automaticly call this procedure. |
pairmand | 3:8f2b7f4940b5 | 356 | int i; |
pairmand | 3:8f2b7f4940b5 | 357 | match_ROM(); // Select this device |
pairmand | 3:8f2b7f4940b5 | 358 | onewire_byte_out( 0xBE); //Read Scratchpad command |
pairmand | 3:8f2b7f4940b5 | 359 | for(i=0;i<9;i++) { |
pairmand | 3:8f2b7f4940b5 | 360 | RAM[i] = onewire_byte_in(); |
pairmand | 3:8f2b7f4940b5 | 361 | } |
pairmand | 3:8f2b7f4940b5 | 362 | // if (!RAM_checksum_error()) |
pairmand | 3:8f2b7f4940b5 | 363 | // crcerr = 1; |
pairmand | 3:8f2b7f4940b5 | 364 | } |
pairmand | 3:8f2b7f4940b5 | 365 | |
pairmand | 3:8f2b7f4940b5 | 366 | bool DS1820::set_configuration_bits(unsigned int resolution) { |
pairmand | 3:8f2b7f4940b5 | 367 | bool answer = false; |
pairmand | 3:8f2b7f4940b5 | 368 | resolution = resolution - 9; |
pairmand | 3:8f2b7f4940b5 | 369 | if (resolution < 4) { |
pairmand | 3:8f2b7f4940b5 | 370 | resolution = resolution<<5; // align the bits |
pairmand | 3:8f2b7f4940b5 | 371 | RAM[4] = (RAM[4] & 0x60) | resolution; // mask out old data, insert new |
pairmand | 3:8f2b7f4940b5 | 372 | write_scratchpad ((RAM[2]<<8) + RAM[3]); |
pairmand | 3:8f2b7f4940b5 | 373 | // store_scratchpad (DS1820::this_device); // Need to test if this is required |
pairmand | 3:8f2b7f4940b5 | 374 | answer = true; |
pairmand | 3:8f2b7f4940b5 | 375 | } |
pairmand | 3:8f2b7f4940b5 | 376 | return answer; |
pairmand | 3:8f2b7f4940b5 | 377 | } |
pairmand | 3:8f2b7f4940b5 | 378 | |
pairmand | 3:8f2b7f4940b5 | 379 | int DS1820::read_scratchpad() { |
pairmand | 3:8f2b7f4940b5 | 380 | int answer; |
pairmand | 3:8f2b7f4940b5 | 381 | read_RAM(); |
pairmand | 3:8f2b7f4940b5 | 382 | answer = (RAM[2]<<8) + RAM[3]; |
pairmand | 3:8f2b7f4940b5 | 383 | return answer; |
pairmand | 3:8f2b7f4940b5 | 384 | } |
pairmand | 3:8f2b7f4940b5 | 385 | |
pairmand | 3:8f2b7f4940b5 | 386 | void DS1820::write_scratchpad(int data) { |
pairmand | 3:8f2b7f4940b5 | 387 | RAM[3] = data; |
pairmand | 3:8f2b7f4940b5 | 388 | RAM[2] = data>>8; |
pairmand | 3:8f2b7f4940b5 | 389 | match_ROM(); |
pairmand | 3:8f2b7f4940b5 | 390 | onewire_byte_out(0x4E); // Copy scratchpad into DS1820 ram memory |
pairmand | 3:8f2b7f4940b5 | 391 | onewire_byte_out(RAM[2]); // T(H) |
pairmand | 3:8f2b7f4940b5 | 392 | onewire_byte_out(RAM[3]); // T(L) |
pairmand | 3:8f2b7f4940b5 | 393 | if ( FAMILY_CODE == FAMILY_CODE_DS18B20 ) { |
pairmand | 3:8f2b7f4940b5 | 394 | onewire_byte_out(RAM[4]); // Configuration register |
pairmand | 3:8f2b7f4940b5 | 395 | } |
pairmand | 3:8f2b7f4940b5 | 396 | } |
pairmand | 3:8f2b7f4940b5 | 397 | |
pairmand | 3:8f2b7f4940b5 | 398 | void DS1820::store_scratchpad(devices device) { |
pairmand | 3:8f2b7f4940b5 | 399 | if (device==all_devices) |
pairmand | 3:8f2b7f4940b5 | 400 | skip_ROM(); // Skip ROM command, will store for ALL devices |
pairmand | 3:8f2b7f4940b5 | 401 | else |
pairmand | 3:8f2b7f4940b5 | 402 | match_ROM(); |
pairmand | 3:8f2b7f4940b5 | 403 | onewire_byte_out(0x48); // Write scratchpad into E2 command |
pairmand | 3:8f2b7f4940b5 | 404 | if (_parasite_power) |
pairmand | 3:8f2b7f4940b5 | 405 | _parasitepin=1; |
pairmand | 3:8f2b7f4940b5 | 406 | wait_ms(10); // Parasite power strong pullup for 10ms |
pairmand | 3:8f2b7f4940b5 | 407 | if (_parasite_power) |
pairmand | 3:8f2b7f4940b5 | 408 | _parasitepin=0; |
pairmand | 3:8f2b7f4940b5 | 409 | } |
pairmand | 3:8f2b7f4940b5 | 410 | |
pairmand | 3:8f2b7f4940b5 | 411 | int DS1820::recall_scratchpad(devices device) { |
pairmand | 3:8f2b7f4940b5 | 412 | // This copies the E2 values into the DS1820's memory. |
pairmand | 3:8f2b7f4940b5 | 413 | // If you specify all_devices this will return zero, otherwise |
pairmand | 3:8f2b7f4940b5 | 414 | // it will return the value of the scratchpad memory. |
pairmand | 3:8f2b7f4940b5 | 415 | int answer=0; |
pairmand | 3:8f2b7f4940b5 | 416 | if (device==all_devices) |
pairmand | 3:8f2b7f4940b5 | 417 | skip_ROM(); // Skip ROM command, will recall for ALL devices |
pairmand | 3:8f2b7f4940b5 | 418 | else |
pairmand | 3:8f2b7f4940b5 | 419 | match_ROM(); |
pairmand | 3:8f2b7f4940b5 | 420 | onewire_byte_out(0xB8); // Recall E2 data to scratchpad command |
pairmand | 3:8f2b7f4940b5 | 421 | wait_ms(10); // not sure I like polling for completion |
pairmand | 3:8f2b7f4940b5 | 422 | // it could cause an infinite loop |
pairmand | 3:8f2b7f4940b5 | 423 | if (device==DS1820::this_device) { |
pairmand | 3:8f2b7f4940b5 | 424 | read_RAM(); |
pairmand | 3:8f2b7f4940b5 | 425 | answer = read_scratchpad(); |
pairmand | 3:8f2b7f4940b5 | 426 | } |
pairmand | 3:8f2b7f4940b5 | 427 | return answer; |
pairmand | 3:8f2b7f4940b5 | 428 | } |
pairmand | 3:8f2b7f4940b5 | 429 | |
pairmand | 3:8f2b7f4940b5 | 430 | float DS1820::temperature(char scale) { |
pairmand | 3:8f2b7f4940b5 | 431 | // The data specs state that count_per_degree should be 0x10 (16), I found my devices |
pairmand | 3:8f2b7f4940b5 | 432 | // to have a count_per_degree of 0x4B (75). With the standard resolution of 1/2 deg C |
pairmand | 3:8f2b7f4940b5 | 433 | // this allowed an expanded resolution of 1/150th of a deg C. I wouldn't rely on this |
pairmand | 3:8f2b7f4940b5 | 434 | // being super acurate, but it does allow for a smooth display in the 1/10ths of a |
pairmand | 3:8f2b7f4940b5 | 435 | // deg C or F scales. |
pairmand | 3:8f2b7f4940b5 | 436 | float answer, remaining_count, count_per_degree; |
pairmand | 3:8f2b7f4940b5 | 437 | int reading; |
pairmand | 3:8f2b7f4940b5 | 438 | read_RAM(); |
pairmand | 3:8f2b7f4940b5 | 439 | if (RAM_checksum_error()) |
pairmand | 3:8f2b7f4940b5 | 440 | // Indicate we got a CRC error |
pairmand | 3:8f2b7f4940b5 | 441 | answer = -1000.0; |
pairmand | 3:8f2b7f4940b5 | 442 | else { |
pairmand | 3:8f2b7f4940b5 | 443 | reading = (RAM[1] << 8) + RAM[0]; |
pairmand | 3:8f2b7f4940b5 | 444 | if (reading & 0x8000) { // negative degrees C |
pairmand | 3:8f2b7f4940b5 | 445 | reading = 0-((reading ^ 0xffff) + 1); // 2's comp then convert to signed int |
pairmand | 3:8f2b7f4940b5 | 446 | } |
pairmand | 3:8f2b7f4940b5 | 447 | answer = reading +0.0; // convert to floating point |
pairmand | 3:8f2b7f4940b5 | 448 | if ( FAMILY_CODE == FAMILY_CODE_DS18B20 ) { |
pairmand | 3:8f2b7f4940b5 | 449 | answer = answer / 8.0; |
pairmand | 3:8f2b7f4940b5 | 450 | } |
pairmand | 3:8f2b7f4940b5 | 451 | else { |
pairmand | 3:8f2b7f4940b5 | 452 | remaining_count = RAM[6]; |
pairmand | 3:8f2b7f4940b5 | 453 | count_per_degree = RAM[7]; |
pairmand | 3:8f2b7f4940b5 | 454 | answer = answer - 0.25 + (count_per_degree - remaining_count) / count_per_degree; |
pairmand | 3:8f2b7f4940b5 | 455 | } |
pairmand | 3:8f2b7f4940b5 | 456 | if (scale=='C' or scale=='c') |
pairmand | 3:8f2b7f4940b5 | 457 | answer = answer / 2.0; |
pairmand | 3:8f2b7f4940b5 | 458 | else |
pairmand | 3:8f2b7f4940b5 | 459 | // Convert to deg F |
pairmand | 3:8f2b7f4940b5 | 460 | answer = answer * 9.0 / 10.0 + 32.0; |
pairmand | 3:8f2b7f4940b5 | 461 | } |
pairmand | 3:8f2b7f4940b5 | 462 | return answer; |
pairmand | 3:8f2b7f4940b5 | 463 | } |
pairmand | 3:8f2b7f4940b5 | 464 | |
pairmand | 3:8f2b7f4940b5 | 465 | bool DS1820::read_power_supply(devices device) { |
pairmand | 3:8f2b7f4940b5 | 466 | // This will return true if the device (or all devices) are Vcc powered |
pairmand | 3:8f2b7f4940b5 | 467 | // This will return false if the device (or ANY device) is parasite powered |
pairmand | 3:8f2b7f4940b5 | 468 | if (device==all_devices) |
pairmand | 3:8f2b7f4940b5 | 469 | skip_ROM(); // Skip ROM command, will poll for any device using parasite power |
pairmand | 3:8f2b7f4940b5 | 470 | else |
pairmand | 3:8f2b7f4940b5 | 471 | match_ROM(); |
pairmand | 3:8f2b7f4940b5 | 472 | onewire_byte_out(0xB4); // Read power supply command |
pairmand | 3:8f2b7f4940b5 | 473 | return onewire_bit_in(); |
pairmand | 3:8f2b7f4940b5 | 474 | } |
pairmand | 3:8f2b7f4940b5 | 475 | |
pairmand | 3:8f2b7f4940b5 | 476 |