2 sensors

Dependencies:   LinkedList

Dependents:   Sample_program_Font72

Committer:
WaleedElmughrabi
Date:
Mon Jan 07 19:16:23 2019 +0000
Revision:
16:e11cde157bf6
Parent:
14:c591209285e9
Test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pairmand 3:8f2b7f4940b5 1 #include "DS1820.h"
Sissors 5:2cd4928e8147 2
Sissors 14:c591209285e9 3 #ifdef TARGET_STM
Sissors 14:c591209285e9 4 //STM targets use opendrain mode since their GPIO code is too bad to be used like the others
Sissors 14:c591209285e9 5 #define ONEWIRE_INPUT(pin) pin->write(1)
Sissors 14:c591209285e9 6 #define ONEWIRE_OUTPUT(pin)
Sissors 14:c591209285e9 7 #define ONEWIRE_INIT(pin) pin->output(); pin->mode(OpenDrain)
Sissors 14:c591209285e9 8
Sissors 14:c591209285e9 9 // TEMP, remove once STM fixed their stuff
Sissors 14:c591209285e9 10 // Enable GPIO clock and return GPIO base address
Sissors 14:c591209285e9 11 static uint32_t Set_GPIO_Clock(uint32_t port_idx) {
Sissors 14:c591209285e9 12 uint32_t gpio_add = 0;
Sissors 14:c591209285e9 13 switch (port_idx) {
Sissors 14:c591209285e9 14 case PortA:
Sissors 14:c591209285e9 15 gpio_add = GPIOA_BASE;
Sissors 14:c591209285e9 16 __GPIOA_CLK_ENABLE();
Sissors 14:c591209285e9 17 break;
Sissors 14:c591209285e9 18 case PortB:
Sissors 14:c591209285e9 19 gpio_add = GPIOB_BASE;
Sissors 14:c591209285e9 20 __GPIOB_CLK_ENABLE();
Sissors 14:c591209285e9 21 break;
Sissors 14:c591209285e9 22 #if defined(GPIOC_BASE)
Sissors 14:c591209285e9 23 case PortC:
Sissors 14:c591209285e9 24 gpio_add = GPIOC_BASE;
Sissors 14:c591209285e9 25 __GPIOC_CLK_ENABLE();
Sissors 14:c591209285e9 26 break;
Sissors 14:c591209285e9 27 #endif
Sissors 14:c591209285e9 28 #if defined(GPIOD_BASE)
Sissors 14:c591209285e9 29 case PortD:
Sissors 14:c591209285e9 30 gpio_add = GPIOD_BASE;
Sissors 14:c591209285e9 31 __GPIOD_CLK_ENABLE();
Sissors 14:c591209285e9 32 break;
WaleedElmughrabi 16:e11cde157bf6 33 /*
Sissors 14:c591209285e9 34 #endif
Sissors 14:c591209285e9 35 #if defined(GPIOF_BASE)
Sissors 14:c591209285e9 36 case PortF:
Sissors 14:c591209285e9 37 gpio_add = GPIOF_BASE;
Sissors 14:c591209285e9 38 __GPIOF_CLK_ENABLE();
WaleedElmughrabi 16:e11cde157bf6 39 break;
WaleedElmughrabi 16:e11cde157bf6 40 */
Sissors 14:c591209285e9 41 #endif
Sissors 14:c591209285e9 42 default:
Sissors 14:c591209285e9 43 error("Pinmap error: wrong port number.");
Sissors 14:c591209285e9 44 break;
Sissors 14:c591209285e9 45 }
Sissors 14:c591209285e9 46 return gpio_add;
Sissors 14:c591209285e9 47 }
Sissors 14:c591209285e9 48
Sissors 14:c591209285e9 49
Sissors 14:c591209285e9 50 #else
Sissors 14:c591209285e9 51 #define ONEWIRE_INPUT(pin) pin->input()
Sissors 14:c591209285e9 52 #define ONEWIRE_OUTPUT(pin) pin->output()
Sissors 14:c591209285e9 53 #define ONEWIRE_INIT(pin)
Sissors 14:c591209285e9 54 #endif
Sissors 14:c591209285e9 55
Sissors 5:2cd4928e8147 56 LinkedList<node> DS1820::probes;
pairmand 3:8f2b7f4940b5 57
pairmand 3:8f2b7f4940b5 58
Sissors 5:2cd4928e8147 59 DS1820::DS1820 (PinName data_pin, PinName power_pin, bool power_polarity) : _datapin(data_pin), _parasitepin(power_pin) {
pairmand 3:8f2b7f4940b5 60 int byte_counter;
Sissors 5:2cd4928e8147 61 _power_polarity = power_polarity;
florian 9:3821ca0b7f14 62
florian 9:3821ca0b7f14 63 _power_mosfet = power_pin != NC;
Sissors 5:2cd4928e8147 64
pairmand 3:8f2b7f4940b5 65 for(byte_counter=0;byte_counter<9;byte_counter++)
pairmand 3:8f2b7f4940b5 66 RAM[byte_counter] = 0x00;
Sissors 5:2cd4928e8147 67
Sissors 14:c591209285e9 68 ONEWIRE_INIT((&_datapin));
Sissors 14:c591209285e9 69 // Temp code since the above doesn't actually do anything in mbed revisions up to 133
Sissors 14:c591209285e9 70 #ifdef TARGET_STM
Sissors 14:c591209285e9 71
Sissors 14:c591209285e9 72 uint32_t port_index = STM_PORT(data_pin);
Sissors 14:c591209285e9 73 uint32_t pin_index = STM_PIN(data_pin);
Sissors 14:c591209285e9 74
Sissors 14:c591209285e9 75 // Enable GPIO clock
Sissors 14:c591209285e9 76 uint32_t gpio_add = Set_GPIO_Clock(port_index);
Sissors 14:c591209285e9 77 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
Sissors 14:c591209285e9 78
Sissors 14:c591209285e9 79 gpio->OTYPER |= (uint32_t)(1 << pin_index);
Sissors 14:c591209285e9 80 #endif
Sissors 14:c591209285e9 81
Sissors 5:2cd4928e8147 82 if (!unassignedProbe(&_datapin, _ROM))
Sissors 5:2cd4928e8147 83 error("No unassigned DS1820 found!\n");
Sissors 5:2cd4928e8147 84 else {
Sissors 5:2cd4928e8147 85 _datapin.input();
Sissors 5:2cd4928e8147 86 probes.append(this);
Sissors 5:2cd4928e8147 87 _parasite_power = !read_power_supply();
Sissors 5:2cd4928e8147 88 }
pairmand 3:8f2b7f4940b5 89 }
Sissors 5:2cd4928e8147 90
Sissors 5:2cd4928e8147 91 DS1820::~DS1820 (void) {
Sissors 5:2cd4928e8147 92 node *tmp;
Sissors 5:2cd4928e8147 93 for(int i=1; i<=probes.length(); i++)
Sissors 5:2cd4928e8147 94 {
Sissors 5:2cd4928e8147 95 tmp = probes.pop(i);
Sissors 5:2cd4928e8147 96 if (tmp->data == this)
Sissors 5:2cd4928e8147 97 probes.remove(i);
Sissors 5:2cd4928e8147 98 }
pairmand 3:8f2b7f4940b5 99 }
Sissors 5:2cd4928e8147 100
pairmand 3:8f2b7f4940b5 101
Sissors 5:2cd4928e8147 102 bool DS1820::onewire_reset(DigitalInOut *pin) {
pairmand 3:8f2b7f4940b5 103 // This will return false if no devices are present on the data bus
pairmand 3:8f2b7f4940b5 104 bool presence=false;
Sissors 14:c591209285e9 105 ONEWIRE_OUTPUT(pin);
Sissors 5:2cd4928e8147 106 pin->write(0); // bring low for 500 us
pairmand 3:8f2b7f4940b5 107 wait_us(500);
Sissors 14:c591209285e9 108 ONEWIRE_INPUT(pin); // let the data line float high
pairmand 3:8f2b7f4940b5 109 wait_us(90); // wait 90us
Sissors 5:2cd4928e8147 110 if (pin->read()==0) // see if any devices are pulling the data line low
pairmand 3:8f2b7f4940b5 111 presence=true;
pairmand 3:8f2b7f4940b5 112 wait_us(410);
pairmand 3:8f2b7f4940b5 113 return presence;
pairmand 3:8f2b7f4940b5 114 }
pairmand 3:8f2b7f4940b5 115
Sissors 5:2cd4928e8147 116 void DS1820::onewire_bit_out (DigitalInOut *pin, bool bit_data) {
Sissors 14:c591209285e9 117 ONEWIRE_OUTPUT(pin);
Sissors 5:2cd4928e8147 118 pin->write(0);
pairmand 3:8f2b7f4940b5 119 wait_us(3); // DXP modified from 5
pairmand 3:8f2b7f4940b5 120 if (bit_data) {
Sissors 8:d87e11e8d012 121 pin->write(1); // bring data line high
pairmand 3:8f2b7f4940b5 122 wait_us(55);
pairmand 3:8f2b7f4940b5 123 } else {
pairmand 3:8f2b7f4940b5 124 wait_us(55); // keep data line low
Sissors 8:d87e11e8d012 125 pin->write(1);
pairmand 3:8f2b7f4940b5 126 wait_us(10); // DXP added to allow bus to float high before next bit_out
pairmand 3:8f2b7f4940b5 127 }
pairmand 3:8f2b7f4940b5 128 }
pairmand 3:8f2b7f4940b5 129
pairmand 3:8f2b7f4940b5 130 void DS1820::onewire_byte_out(char data) { // output data character (least sig bit first).
pairmand 3:8f2b7f4940b5 131 int n;
pairmand 3:8f2b7f4940b5 132 for (n=0; n<8; n++) {
Sissors 5:2cd4928e8147 133 onewire_bit_out(&this->_datapin, data & 0x01);
pairmand 3:8f2b7f4940b5 134 data = data >> 1; // now the next bit is in the least sig bit position.
pairmand 3:8f2b7f4940b5 135 }
pairmand 3:8f2b7f4940b5 136 }
pairmand 3:8f2b7f4940b5 137
Sissors 5:2cd4928e8147 138 bool DS1820::onewire_bit_in(DigitalInOut *pin) {
pairmand 3:8f2b7f4940b5 139 bool answer;
Sissors 14:c591209285e9 140 ONEWIRE_OUTPUT(pin);
Sissors 5:2cd4928e8147 141 pin->write(0);
pairmand 3:8f2b7f4940b5 142 wait_us(3); // DXP modofied from 5
Sissors 14:c591209285e9 143 ONEWIRE_INPUT(pin);
pairmand 3:8f2b7f4940b5 144 wait_us(10); // DXP modified from 5
Sissors 5:2cd4928e8147 145 answer = pin->read();
pairmand 3:8f2b7f4940b5 146 wait_us(45); // DXP modified from 50
pairmand 3:8f2b7f4940b5 147 return answer;
pairmand 3:8f2b7f4940b5 148 }
pairmand 3:8f2b7f4940b5 149
pairmand 3:8f2b7f4940b5 150 char DS1820::onewire_byte_in() { // read byte, least sig byte first
pairmand 3:8f2b7f4940b5 151 char answer = 0x00;
pairmand 3:8f2b7f4940b5 152 int i;
pairmand 3:8f2b7f4940b5 153 for (i=0; i<8; i++) {
pairmand 3:8f2b7f4940b5 154 answer = answer >> 1; // shift over to make room for the next bit
Sissors 5:2cd4928e8147 155 if (onewire_bit_in(&this->_datapin))
pairmand 3:8f2b7f4940b5 156 answer = answer | 0x80; // if the data port is high, make this bit a 1
pairmand 3:8f2b7f4940b5 157 }
pairmand 3:8f2b7f4940b5 158 return answer;
pairmand 3:8f2b7f4940b5 159 }
Sissors 5:2cd4928e8147 160
Sissors 5:2cd4928e8147 161 bool DS1820::unassignedProbe(PinName pin) {
Sissors 5:2cd4928e8147 162 DigitalInOut _pin(pin);
Sissors 14:c591209285e9 163 ONEWIRE_INIT((&_pin));
Sissors 14:c591209285e9 164 // Temp code since the above doesn't actually do anything in mbed revisions up to 133
Sissors 14:c591209285e9 165 #ifdef TARGET_STM
Sissors 14:c591209285e9 166
Sissors 14:c591209285e9 167 uint32_t port_index = STM_PORT(pin);
Sissors 14:c591209285e9 168 uint32_t pin_index = STM_PIN(pin);
Sissors 14:c591209285e9 169
Sissors 14:c591209285e9 170 // Enable GPIO clock
Sissors 14:c591209285e9 171 uint32_t gpio_add = Set_GPIO_Clock(port_index);
Sissors 14:c591209285e9 172 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
Sissors 14:c591209285e9 173
Sissors 14:c591209285e9 174 gpio->OTYPER |= (uint32_t)(1 << pin_index);
Sissors 14:c591209285e9 175 #endif
Sissors 5:2cd4928e8147 176 char ROM_address[8];
Sissors 5:2cd4928e8147 177 return search_ROM_routine(&_pin, 0xF0, ROM_address);
pairmand 3:8f2b7f4940b5 178 }
pairmand 3:8f2b7f4940b5 179
Sissors 5:2cd4928e8147 180 bool DS1820::unassignedProbe(DigitalInOut *pin, char *ROM_address) {
Sissors 5:2cd4928e8147 181 return search_ROM_routine(pin, 0xF0, ROM_address);
pairmand 3:8f2b7f4940b5 182 }
pairmand 3:8f2b7f4940b5 183
Sissors 5:2cd4928e8147 184 bool DS1820::search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address) {
Sissors 5:2cd4928e8147 185 bool DS1820_done_flag = false;
Sissors 5:2cd4928e8147 186 int DS1820_last_descrepancy = 0;
Sissors 5:2cd4928e8147 187 char DS1820_search_ROM[8] = {0, 0, 0, 0, 0, 0, 0, 0};
Sissors 5:2cd4928e8147 188
pairmand 3:8f2b7f4940b5 189 int descrepancy_marker, ROM_bit_index;
pairmand 3:8f2b7f4940b5 190 bool return_value, Bit_A, Bit_B;
pairmand 3:8f2b7f4940b5 191 char byte_counter, bit_mask;
pairmand 3:8f2b7f4940b5 192
pairmand 3:8f2b7f4940b5 193 return_value=false;
Sissors 5:2cd4928e8147 194 while (!DS1820_done_flag) {
Sissors 5:2cd4928e8147 195 if (!onewire_reset(pin)) {
Sissors 5:2cd4928e8147 196 return false;
pairmand 3:8f2b7f4940b5 197 } else {
pairmand 3:8f2b7f4940b5 198 ROM_bit_index=1;
pairmand 3:8f2b7f4940b5 199 descrepancy_marker=0;
Sissors 12:196e9e54b033 200 char command_shift = command;
Sissors 5:2cd4928e8147 201 for (int n=0; n<8; n++) { // Search ROM command or Search Alarm command
Sissors 12:196e9e54b033 202 onewire_bit_out(pin, command_shift & 0x01);
Sissors 12:196e9e54b033 203 command_shift = command_shift >> 1; // now the next bit is in the least sig bit position.
Sissors 5:2cd4928e8147 204 }
pairmand 3:8f2b7f4940b5 205 byte_counter = 0;
pairmand 3:8f2b7f4940b5 206 bit_mask = 0x01;
pairmand 3:8f2b7f4940b5 207 while (ROM_bit_index<=64) {
Sissors 5:2cd4928e8147 208 Bit_A = onewire_bit_in(pin);
Sissors 5:2cd4928e8147 209 Bit_B = onewire_bit_in(pin);
pairmand 3:8f2b7f4940b5 210 if (Bit_A & Bit_B) {
pairmand 3:8f2b7f4940b5 211 descrepancy_marker = 0; // data read error, this should never happen
pairmand 3:8f2b7f4940b5 212 ROM_bit_index = 0xFF;
pairmand 3:8f2b7f4940b5 213 } else {
pairmand 3:8f2b7f4940b5 214 if (Bit_A | Bit_B) {
pairmand 3:8f2b7f4940b5 215 // Set ROM bit to Bit_A
pairmand 3:8f2b7f4940b5 216 if (Bit_A) {
pairmand 3:8f2b7f4940b5 217 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one
pairmand 3:8f2b7f4940b5 218 } else {
pairmand 3:8f2b7f4940b5 219 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero
pairmand 3:8f2b7f4940b5 220 }
pairmand 3:8f2b7f4940b5 221 } else {
pairmand 3:8f2b7f4940b5 222 // both bits A and B are low, so there are two or more devices present
pairmand 3:8f2b7f4940b5 223 if ( ROM_bit_index == DS1820_last_descrepancy ) {
pairmand 3:8f2b7f4940b5 224 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one
pairmand 3:8f2b7f4940b5 225 } else {
pairmand 3:8f2b7f4940b5 226 if ( ROM_bit_index > DS1820_last_descrepancy ) {
pairmand 3:8f2b7f4940b5 227 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero
pairmand 3:8f2b7f4940b5 228 descrepancy_marker = ROM_bit_index;
pairmand 3:8f2b7f4940b5 229 } else {
pairmand 3:8f2b7f4940b5 230 if (( DS1820_search_ROM[byte_counter] & bit_mask) == 0x00 )
pairmand 3:8f2b7f4940b5 231 descrepancy_marker = ROM_bit_index;
pairmand 3:8f2b7f4940b5 232 }
pairmand 3:8f2b7f4940b5 233 }
pairmand 3:8f2b7f4940b5 234 }
Sissors 5:2cd4928e8147 235 onewire_bit_out (pin, DS1820_search_ROM[byte_counter] & bit_mask);
pairmand 3:8f2b7f4940b5 236 ROM_bit_index++;
pairmand 3:8f2b7f4940b5 237 if (bit_mask & 0x80) {
pairmand 3:8f2b7f4940b5 238 byte_counter++;
pairmand 3:8f2b7f4940b5 239 bit_mask = 0x01;
pairmand 3:8f2b7f4940b5 240 } else {
pairmand 3:8f2b7f4940b5 241 bit_mask = bit_mask << 1;
pairmand 3:8f2b7f4940b5 242 }
pairmand 3:8f2b7f4940b5 243 }
pairmand 3:8f2b7f4940b5 244 }
pairmand 3:8f2b7f4940b5 245 DS1820_last_descrepancy = descrepancy_marker;
pairmand 3:8f2b7f4940b5 246 if (ROM_bit_index != 0xFF) {
Sissors 5:2cd4928e8147 247 int i = 1;
Sissors 5:2cd4928e8147 248 node *list_container;
Sissors 5:2cd4928e8147 249 while(1) {
Sissors 5:2cd4928e8147 250 list_container = probes.pop(i);
Sissors 5:2cd4928e8147 251 if (list_container == NULL) { //End of list, or empty list
Sissors 5:2cd4928e8147 252 if (ROM_checksum_error(DS1820_search_ROM)) { // Check the CRC
Sissors 5:2cd4928e8147 253 return false;
Sissors 5:2cd4928e8147 254 }
Sissors 5:2cd4928e8147 255 for(byte_counter=0;byte_counter<8;byte_counter++)
Sissors 5:2cd4928e8147 256 ROM_address[byte_counter] = DS1820_search_ROM[byte_counter];
Sissors 5:2cd4928e8147 257 return true;
Sissors 5:2cd4928e8147 258 } else { //Otherwise, check if ROM is already known
Sissors 5:2cd4928e8147 259 bool equal = true;
Sissors 5:2cd4928e8147 260 DS1820 *pointer = (DS1820*) list_container->data;
Sissors 5:2cd4928e8147 261 char *ROM_compare = pointer->_ROM;
Sissors 5:2cd4928e8147 262
Sissors 5:2cd4928e8147 263 for(byte_counter=0;byte_counter<8;byte_counter++) {
Sissors 5:2cd4928e8147 264 if ( ROM_compare[byte_counter] != DS1820_search_ROM[byte_counter])
Sissors 5:2cd4928e8147 265 equal = false;
Sissors 5:2cd4928e8147 266 }
Sissors 5:2cd4928e8147 267 if (equal)
Sissors 5:2cd4928e8147 268 break;
Sissors 5:2cd4928e8147 269 else
Sissors 5:2cd4928e8147 270 i++;
Sissors 5:2cd4928e8147 271 }
Sissors 5:2cd4928e8147 272 }
pairmand 3:8f2b7f4940b5 273 }
pairmand 3:8f2b7f4940b5 274 }
pairmand 3:8f2b7f4940b5 275 if (DS1820_last_descrepancy == 0)
pairmand 3:8f2b7f4940b5 276 DS1820_done_flag = true;
pairmand 3:8f2b7f4940b5 277 }
pairmand 3:8f2b7f4940b5 278 return return_value;
pairmand 3:8f2b7f4940b5 279 }
pairmand 3:8f2b7f4940b5 280
pairmand 3:8f2b7f4940b5 281 void DS1820::match_ROM() {
pairmand 3:8f2b7f4940b5 282 // Used to select a specific device
pairmand 3:8f2b7f4940b5 283 int i;
Sissors 5:2cd4928e8147 284 onewire_reset(&this->_datapin);
pairmand 3:8f2b7f4940b5 285 onewire_byte_out( 0x55); //Match ROM command
Sissors 5:2cd4928e8147 286 for (i=0;i<8;i++) {
Sissors 5:2cd4928e8147 287 onewire_byte_out(_ROM[i]);
Sissors 5:2cd4928e8147 288 }
pairmand 3:8f2b7f4940b5 289 }
pairmand 3:8f2b7f4940b5 290
pairmand 3:8f2b7f4940b5 291 void DS1820::skip_ROM() {
Sissors 5:2cd4928e8147 292 onewire_reset(&this->_datapin);
pairmand 3:8f2b7f4940b5 293 onewire_byte_out(0xCC); // Skip ROM command
pairmand 3:8f2b7f4940b5 294 }
pairmand 3:8f2b7f4940b5 295
Sissors 5:2cd4928e8147 296 bool DS1820::ROM_checksum_error(char *_ROM_address) {
Sissors 11:1a3c3002b50c 297 char _CRC=0x00;
pairmand 3:8f2b7f4940b5 298 int i;
pairmand 3:8f2b7f4940b5 299 for(i=0;i<7;i++) // Only going to shift the lower 7 bytes
Sissors 11:1a3c3002b50c 300 _CRC = CRC_byte(_CRC, _ROM_address[i]);
pairmand 3:8f2b7f4940b5 301 // After 7 bytes CRC should equal the 8th byte (ROM CRC)
Sissors 11:1a3c3002b50c 302 return (_CRC!=_ROM_address[7]); // will return true if there is a CRC checksum mis-match
pairmand 3:8f2b7f4940b5 303 }
pairmand 3:8f2b7f4940b5 304
pairmand 3:8f2b7f4940b5 305 bool DS1820::RAM_checksum_error() {
Sissors 11:1a3c3002b50c 306 char _CRC=0x00;
pairmand 3:8f2b7f4940b5 307 int i;
pairmand 3:8f2b7f4940b5 308 for(i=0;i<8;i++) // Only going to shift the lower 8 bytes
Sissors 11:1a3c3002b50c 309 _CRC = CRC_byte(_CRC, RAM[i]);
pairmand 3:8f2b7f4940b5 310 // After 8 bytes CRC should equal the 9th byte (RAM CRC)
Sissors 11:1a3c3002b50c 311 return (_CRC!=RAM[8]); // will return true if there is a CRC checksum mis-match
pairmand 3:8f2b7f4940b5 312 }
pairmand 3:8f2b7f4940b5 313
Sissors 11:1a3c3002b50c 314 char DS1820::CRC_byte (char _CRC, char byte ) {
pairmand 3:8f2b7f4940b5 315 int j;
pairmand 3:8f2b7f4940b5 316 for(j=0;j<8;j++) {
Sissors 11:1a3c3002b50c 317 if ((byte & 0x01 ) ^ (_CRC & 0x01)) {
pairmand 3:8f2b7f4940b5 318 // DATA ^ LSB CRC = 1
Sissors 11:1a3c3002b50c 319 _CRC = _CRC>>1;
pairmand 3:8f2b7f4940b5 320 // Set the MSB to 1
Sissors 11:1a3c3002b50c 321 _CRC = _CRC | 0x80;
pairmand 3:8f2b7f4940b5 322 // Check bit 3
Sissors 11:1a3c3002b50c 323 if (_CRC & 0x04) {
Sissors 11:1a3c3002b50c 324 _CRC = _CRC & 0xFB; // Bit 3 is set, so clear it
pairmand 3:8f2b7f4940b5 325 } else {
Sissors 11:1a3c3002b50c 326 _CRC = _CRC | 0x04; // Bit 3 is clear, so set it
pairmand 3:8f2b7f4940b5 327 }
pairmand 3:8f2b7f4940b5 328 // Check bit 4
Sissors 11:1a3c3002b50c 329 if (_CRC & 0x08) {
Sissors 11:1a3c3002b50c 330 _CRC = _CRC & 0xF7; // Bit 4 is set, so clear it
pairmand 3:8f2b7f4940b5 331 } else {
Sissors 11:1a3c3002b50c 332 _CRC = _CRC | 0x08; // Bit 4 is clear, so set it
pairmand 3:8f2b7f4940b5 333 }
pairmand 3:8f2b7f4940b5 334 } else {
pairmand 3:8f2b7f4940b5 335 // DATA ^ LSB CRC = 0
Sissors 11:1a3c3002b50c 336 _CRC = _CRC>>1;
pairmand 3:8f2b7f4940b5 337 // clear MSB
Sissors 11:1a3c3002b50c 338 _CRC = _CRC & 0x7F;
pairmand 3:8f2b7f4940b5 339 // No need to check bits, with DATA ^ LSB CRC = 0, they will remain unchanged
pairmand 3:8f2b7f4940b5 340 }
pairmand 3:8f2b7f4940b5 341 byte = byte>>1;
pairmand 3:8f2b7f4940b5 342 }
Sissors 11:1a3c3002b50c 343 return _CRC;
pairmand 3:8f2b7f4940b5 344 }
pairmand 3:8f2b7f4940b5 345
Sissors 5:2cd4928e8147 346 int DS1820::convertTemperature(bool wait, devices device) {
pairmand 3:8f2b7f4940b5 347 // Convert temperature into scratchpad RAM for all devices at once
pairmand 3:8f2b7f4940b5 348 int delay_time = 750; // Default delay time
pairmand 3:8f2b7f4940b5 349 char resolution;
pairmand 3:8f2b7f4940b5 350 if (device==all_devices)
pairmand 3:8f2b7f4940b5 351 skip_ROM(); // Skip ROM command, will convert for ALL devices
pairmand 3:8f2b7f4940b5 352 else {
pairmand 3:8f2b7f4940b5 353 match_ROM();
Sissors 5:2cd4928e8147 354 if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) {
pairmand 3:8f2b7f4940b5 355 resolution = RAM[4] & 0x60;
pairmand 3:8f2b7f4940b5 356 if (resolution == 0x00) // 9 bits
pairmand 3:8f2b7f4940b5 357 delay_time = 94;
pairmand 3:8f2b7f4940b5 358 if (resolution == 0x20) // 10 bits
pairmand 3:8f2b7f4940b5 359 delay_time = 188;
pairmand 3:8f2b7f4940b5 360 if (resolution == 0x40) // 11 bits. Note 12bits uses the 750ms default
pairmand 3:8f2b7f4940b5 361 delay_time = 375;
pairmand 3:8f2b7f4940b5 362 }
pairmand 3:8f2b7f4940b5 363 }
Sissors 5:2cd4928e8147 364
pairmand 3:8f2b7f4940b5 365 onewire_byte_out( 0x44); // perform temperature conversion
pairmand 3:8f2b7f4940b5 366 if (_parasite_power) {
Sissors 5:2cd4928e8147 367 if (_power_mosfet) {
Sissors 5:2cd4928e8147 368 _parasitepin = _power_polarity; // Parasite power strong pullup
Sissors 5:2cd4928e8147 369 wait_ms(delay_time);
Sissors 5:2cd4928e8147 370 _parasitepin = !_power_polarity;
Sissors 5:2cd4928e8147 371 delay_time = 0;
Sissors 5:2cd4928e8147 372 } else {
Sissors 5:2cd4928e8147 373 _datapin.output();
Sissors 5:2cd4928e8147 374 _datapin.write(1);
Sissors 5:2cd4928e8147 375 wait_ms(delay_time);
Sissors 5:2cd4928e8147 376 _datapin.input();
Sissors 5:2cd4928e8147 377 }
pairmand 3:8f2b7f4940b5 378 } else {
pairmand 3:8f2b7f4940b5 379 if (wait) {
pairmand 3:8f2b7f4940b5 380 wait_ms(delay_time);
pairmand 3:8f2b7f4940b5 381 delay_time = 0;
pairmand 3:8f2b7f4940b5 382 }
pairmand 3:8f2b7f4940b5 383 }
pairmand 3:8f2b7f4940b5 384 return delay_time;
pairmand 3:8f2b7f4940b5 385 }
pairmand 3:8f2b7f4940b5 386
pairmand 3:8f2b7f4940b5 387 void DS1820::read_RAM() {
pairmand 3:8f2b7f4940b5 388 // This will copy the DS1820's 9 bytes of RAM data
pairmand 3:8f2b7f4940b5 389 // into the objects RAM array. Functions that use
pairmand 3:8f2b7f4940b5 390 // RAM values will automaticly call this procedure.
pairmand 3:8f2b7f4940b5 391 int i;
pairmand 3:8f2b7f4940b5 392 match_ROM(); // Select this device
pairmand 3:8f2b7f4940b5 393 onewire_byte_out( 0xBE); //Read Scratchpad command
pairmand 3:8f2b7f4940b5 394 for(i=0;i<9;i++) {
pairmand 3:8f2b7f4940b5 395 RAM[i] = onewire_byte_in();
pairmand 3:8f2b7f4940b5 396 }
pairmand 3:8f2b7f4940b5 397 // if (!RAM_checksum_error())
pairmand 3:8f2b7f4940b5 398 // crcerr = 1;
pairmand 3:8f2b7f4940b5 399 }
pairmand 3:8f2b7f4940b5 400
Sissors 5:2cd4928e8147 401 bool DS1820::setResolution(unsigned int resolution) {
pairmand 3:8f2b7f4940b5 402 bool answer = false;
pairmand 3:8f2b7f4940b5 403 resolution = resolution - 9;
pairmand 3:8f2b7f4940b5 404 if (resolution < 4) {
pairmand 3:8f2b7f4940b5 405 resolution = resolution<<5; // align the bits
pairmand 3:8f2b7f4940b5 406 RAM[4] = (RAM[4] & 0x60) | resolution; // mask out old data, insert new
pairmand 3:8f2b7f4940b5 407 write_scratchpad ((RAM[2]<<8) + RAM[3]);
pairmand 3:8f2b7f4940b5 408 // store_scratchpad (DS1820::this_device); // Need to test if this is required
pairmand 3:8f2b7f4940b5 409 answer = true;
pairmand 3:8f2b7f4940b5 410 }
pairmand 3:8f2b7f4940b5 411 return answer;
pairmand 3:8f2b7f4940b5 412 }
pairmand 3:8f2b7f4940b5 413
pairmand 3:8f2b7f4940b5 414 void DS1820::write_scratchpad(int data) {
pairmand 3:8f2b7f4940b5 415 RAM[3] = data;
pairmand 3:8f2b7f4940b5 416 RAM[2] = data>>8;
pairmand 3:8f2b7f4940b5 417 match_ROM();
pairmand 3:8f2b7f4940b5 418 onewire_byte_out(0x4E); // Copy scratchpad into DS1820 ram memory
pairmand 3:8f2b7f4940b5 419 onewire_byte_out(RAM[2]); // T(H)
pairmand 3:8f2b7f4940b5 420 onewire_byte_out(RAM[3]); // T(L)
Sissors 5:2cd4928e8147 421 if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) {
pairmand 3:8f2b7f4940b5 422 onewire_byte_out(RAM[4]); // Configuration register
pairmand 3:8f2b7f4940b5 423 }
pairmand 3:8f2b7f4940b5 424 }
pairmand 3:8f2b7f4940b5 425
pairmand 3:8f2b7f4940b5 426 float DS1820::temperature(char scale) {
pairmand 3:8f2b7f4940b5 427 // The data specs state that count_per_degree should be 0x10 (16), I found my devices
pairmand 3:8f2b7f4940b5 428 // to have a count_per_degree of 0x4B (75). With the standard resolution of 1/2 deg C
pairmand 3:8f2b7f4940b5 429 // this allowed an expanded resolution of 1/150th of a deg C. I wouldn't rely on this
pairmand 3:8f2b7f4940b5 430 // being super acurate, but it does allow for a smooth display in the 1/10ths of a
pairmand 3:8f2b7f4940b5 431 // deg C or F scales.
pairmand 3:8f2b7f4940b5 432 float answer, remaining_count, count_per_degree;
pairmand 3:8f2b7f4940b5 433 int reading;
pairmand 3:8f2b7f4940b5 434 read_RAM();
pairmand 3:8f2b7f4940b5 435 if (RAM_checksum_error())
pairmand 3:8f2b7f4940b5 436 // Indicate we got a CRC error
Sissors 7:58b61681818f 437 answer = invalid_conversion;
pairmand 3:8f2b7f4940b5 438 else {
pairmand 3:8f2b7f4940b5 439 reading = (RAM[1] << 8) + RAM[0];
pairmand 3:8f2b7f4940b5 440 if (reading & 0x8000) { // negative degrees C
pairmand 3:8f2b7f4940b5 441 reading = 0-((reading ^ 0xffff) + 1); // 2's comp then convert to signed int
pairmand 3:8f2b7f4940b5 442 }
pairmand 3:8f2b7f4940b5 443 answer = reading +0.0; // convert to floating point
Sissors 5:2cd4928e8147 444 if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) {
Sissors 11:1a3c3002b50c 445 answer = answer / 16.0f;
pairmand 3:8f2b7f4940b5 446 }
pairmand 3:8f2b7f4940b5 447 else {
pairmand 3:8f2b7f4940b5 448 remaining_count = RAM[6];
pairmand 3:8f2b7f4940b5 449 count_per_degree = RAM[7];
Sissors 11:1a3c3002b50c 450 answer = floor(answer/2.0f) - 0.25f + (count_per_degree - remaining_count) / count_per_degree;
pairmand 3:8f2b7f4940b5 451 }
florian 10:d297ce9ce422 452 if (scale=='F' or scale=='f')
pairmand 3:8f2b7f4940b5 453 // Convert to deg F
Sissors 11:1a3c3002b50c 454 answer = answer * 9.0f / 5.0f + 32.0f;
pairmand 3:8f2b7f4940b5 455 }
pairmand 3:8f2b7f4940b5 456 return answer;
pairmand 3:8f2b7f4940b5 457 }
pairmand 3:8f2b7f4940b5 458
pairmand 3:8f2b7f4940b5 459 bool DS1820::read_power_supply(devices device) {
pairmand 3:8f2b7f4940b5 460 // This will return true if the device (or all devices) are Vcc powered
pairmand 3:8f2b7f4940b5 461 // This will return false if the device (or ANY device) is parasite powered
pairmand 3:8f2b7f4940b5 462 if (device==all_devices)
pairmand 3:8f2b7f4940b5 463 skip_ROM(); // Skip ROM command, will poll for any device using parasite power
pairmand 3:8f2b7f4940b5 464 else
pairmand 3:8f2b7f4940b5 465 match_ROM();
pairmand 3:8f2b7f4940b5 466 onewire_byte_out(0xB4); // Read power supply command
Sissors 5:2cd4928e8147 467 return onewire_bit_in(&this->_datapin);
pairmand 3:8f2b7f4940b5 468 }
pairmand 3:8f2b7f4940b5 469
pairmand 3:8f2b7f4940b5 470