void DS18B20ReadRom(u8 pin, u8 *romcode) { u8 i; if (!OneWireReset(pin)) { OneWireWrite(pin, READROM); for (i = 0; i < 8; i++) // Reads the ROM Code from a device romcode[i] = OneWireRead(pin); } }
/*JSON{ "type" : "method", "class" : "OneWire", "name" : "read", "generate" : "jswrap_onewire_read", "params" : [["count","JsVar","(optional) The amount of bytes to read"]], "return" : ["JsVar","The byte that was read, or a Uint8Array if count was specified and >=0"] } Read a byte */ JsVar *jswrap_onewire_read(JsVar *parent, JsVar *count) { Pin pin = onewire_getpin(parent); if (!jshIsPinValid(pin)) return 0; if (jsvIsNumeric(count)) { JsVarInt c = jsvGetInteger(count); JsVar *arr = jsvNewTypedArray(ARRAYBUFFERVIEW_UINT8, c); if (!arr) return 0; JsvArrayBufferIterator it; jsvArrayBufferIteratorNew(&it, arr, 0); while (c--) { jsvArrayBufferIteratorSetByteValue(&it, (char)OneWireRead(pin, 8)); jsvArrayBufferIteratorNext(&it); } jsvArrayBufferIteratorFree(&it); return arr; } else { return jsvNewFromInteger(OneWireRead(pin, 8)); } }
u8 DS18B20Read(u8 pin, u8 num, u8 resolution, DS18B20_Temperature * t) { u8 res, busy = LOW; u8 temp_lsb, temp_msb; u16 temp; switch (resolution) { case RES12BIT: res = 0b01100000; break; // 12-bit resolution case RES11BIT: res = 0b01000000; break; // 11-bit resolution case RES10BIT: res = 0b00100000; break; // 10-bit resolution case RES9BIT: res = 0b00000000; break; // 9-bit resolution default: res = 0b00000000; break; // 9-bit resolution /// NB: The power-up default of these bits is R0 = 1 and R1 = 1 (12-bit resolution) } if (!DS18B20Configure(pin, num, 0, 0, res)) return FALSE; // no alarm if (OneWireReset(pin)) return FALSE; if (num == SKIPROM) { // Skip ROM, address all devices OneWireWrite(pin, SKIPROM); } else { // Talk to a particular device if (!DS18B20MatchRom(pin, num)) return FALSE; } OneWireWrite(pin, CONVERT_T); // Start temperature conversion while (busy == LOW) // Wait while busy ( = bus is low) busy = OneWireRead(pin); if (OneWireReset(pin)) return FALSE; if (num == SKIPROM) { // Skip ROM, address all devices OneWireWrite(pin, SKIPROM); } else { // Talk to a particular device if (!DS18B20MatchRom(pin, num)) return FALSE; } OneWireWrite(pin, READ_SCRATCHPAD);// Read scratchpad temp_lsb = OneWireRead(pin); // byte 0 of scratchpad : temperature lsb temp_msb = OneWireRead(pin); // byte 1 of scratchpad : temperature msb if (OneWireReset(pin)) return FALSE; // Calculation // --------------------------------------------------------------------- // Temperature Register Format // BIT7 BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0 // LS BYTE 2^3 2^2 2^1 2^0 2^-1 2^-2 2^-3 2^-4 // BIT15 BIT14 BIT13 BIT12 BIT11 BIT10 BIT9 BIT8 // MS BYTE S S S S S 2^6 2^5 2^4 // S = SIGN temp = temp_msb; temp = (temp << 8) + temp_lsb; // combine msb & lsb into 16 bit variable if (temp_msb & 0b11111000) // test if sign is set, i.e. negative { t->sign = 1; temp = (temp ^ 0xFFFF) + 1; // 2's complement conversion } else { t->sign = 0; } t->integer = (temp >> 4) & 0x7F; // fractional part is removed, leaving only integer part /* t->fraction = 0; // fractional part if (BitRead(temp, 0)) t->fraction += 625; if (BitRead(temp, 1)) t->fraction += 1250; if (BitRead(temp, 2)) t->fraction += 2500; if (BitRead(temp, 3)) t->fraction += 5000; */ t->fraction = (temp & 0x0F) * 625; t->fraction /= 100; // two digits after decimal return TRUE; }
u8 DS18B20ReadMeasure(u8 pin, u8 num, DS18B20_Temperature * t) { u8 res, busy = LOW; u8 temp_lsb, temp_msb; u16 temp; if (OneWireReset(pin)) return FALSE; if (num == SKIPROM) { // Skip ROM, address all devices OneWireWrite(pin, SKIPROM); } else { // Talk to a particular device if (!DS18B20MatchRom(pin, num)) return FALSE; } OneWireWrite(pin, READ_SCRATCHPAD);// Read scratchpad temp_lsb = OneWireRead(pin); // byte 0 of scratchpad : temperature lsb temp_msb = OneWireRead(pin); // byte 1 of scratchpad : temperature msb if (OneWireReset(pin)) return FALSE; // Calculation // --------------------------------------------------------------------- // Temperature Register Format // BIT7 BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0 // LS BYTE 2^3 2^2 2^1 2^0 2^-1 2^-2 2^-3 2^-4 // BIT15 BIT14 BIT13 BIT12 BIT11 BIT10 BIT9 BIT8 // MS BYTE S S S S S 2^6 2^5 2^4 // S = SIGN temp = temp_msb; temp = (temp << 8) + temp_lsb; // combine msb & lsb into 16 bit variable if (temp_msb & 0b11111000) // test if sign is set, i.e. negative { t->sign = 1; temp = (temp ^ 0xFFFF) + 1; // 2's complement conversion } else { t->sign = 0; } t->integer = (temp >> 4) & 0x7F; // fractional part is removed, leaving only integer part /* t->fraction = 0; // fractional part if (BitRead(temp, 0)) t->fraction += 625; if (BitRead(temp, 1)) t->fraction += 1250; if (BitRead(temp, 2)) t->fraction += 2500; if (BitRead(temp, 3)) t->fraction += 5000; */ t->fraction = (temp & 0x0F) * 625; t->fraction /= 100; // two digits after decimal return TRUE; }
/*JSON{ "type" : "method", "class" : "OneWire", "name" : "search", "generate" : "jswrap_onewire_search", "params" : [ ["command","int32","(Optional) command byte. If not specified (or zero), this defaults to 0xF0. This can could be set to 0xEC to perform a DS18B20 'Alarm Search Command'"] ], "return" : ["JsVar","An array of devices that were found"] } Search for devices */ JsVar *jswrap_onewire_search(JsVar *parent, int command) { // search - code from http://www.maximintegrated.com/app-notes/index.mvp/id/187 Pin pin = onewire_getpin(parent); if (!jshIsPinValid(pin)) return 0; JsVar *array = jsvNewEmptyArray(); if (!array) return 0; if (command<=0 || command>255) command = 0xF0; // normal search command // global search state unsigned char ROM_NO[8]; int LastDiscrepancy; int LastFamilyDiscrepancy; int LastDeviceFlag; // reset the search state LastDiscrepancy = 0; LastDeviceFlag = FALSE; LastFamilyDiscrepancy = 0; int search_result = true; while (search_result) { int id_bit_number; int last_zero, rom_byte_number; unsigned char id_bit, cmp_id_bit; unsigned char rom_byte_mask, search_direction; // initialize for search id_bit_number = 1; last_zero = 0; rom_byte_number = 0; rom_byte_mask = 1; search_result = 0; // if the last call was not the last one if (!LastDeviceFlag) { // 1-Wire reset if (!OneWireReset(pin)) { // reset the search LastDiscrepancy = 0; LastDeviceFlag = FALSE; LastFamilyDiscrepancy = 0; return array; } // issue the search command OneWireWrite(pin, 8, (unsigned long long)command); // loop to do the search do { // read a bit and its complement id_bit = (unsigned char)OneWireRead(pin, 1); cmp_id_bit = (unsigned char)OneWireRead(pin, 1); // check for no devices on 1-wire if ((id_bit == 1) && (cmp_id_bit == 1)) break; else { // all devices coupled have 0 or 1 if (id_bit != cmp_id_bit) search_direction = id_bit; // bit write value for search else { // if this discrepancy if before the Last Discrepancy // on a previous next then pick the same as last time if (id_bit_number < LastDiscrepancy) search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0); else // if equal to last pick 1, if not then pick 0 search_direction = (id_bit_number == LastDiscrepancy); // if 0 was picked then record its position in LastZero if (search_direction == 0) { last_zero = id_bit_number; // check for Last discrepancy in family if (last_zero < 9) LastFamilyDiscrepancy = last_zero; } } // set or clear the bit in the ROM byte rom_byte_number // with mask rom_byte_mask if (search_direction == 1) ROM_NO[rom_byte_number] |= rom_byte_mask; else ROM_NO[rom_byte_number] &= (unsigned char)~rom_byte_mask; // serial number search direction write bit OneWireWrite(pin, 1, search_direction); // increment the byte counter id_bit_number // and shift the mask rom_byte_mask id_bit_number++; rom_byte_mask = (unsigned char)(rom_byte_mask << 1); // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask if (rom_byte_mask == 0) { rom_byte_number++; rom_byte_mask = 1; } } } while(rom_byte_number < 8); // loop until through all ROM bytes 0-7 // if the search was successful then if (!((id_bit_number < 65))) { // search successful so set LastDiscrepancy,LastDeviceFlag,search_result LastDiscrepancy = last_zero; // check for last device if (LastDiscrepancy == 0) LastDeviceFlag = TRUE; search_result = TRUE; } } // if no device found then reset counters so next 'search' will be like a first if (!search_result || !ROM_NO[0]) { LastDiscrepancy = 0; LastDeviceFlag = FALSE; LastFamilyDiscrepancy = 0; search_result = FALSE; } if (search_result) { int i; char buf[17]; for (i=0;i<8;i++) { buf[i*2] = itoch((ROM_NO[i]>>4) & 15); buf[i*2+1] = itoch(ROM_NO[i] & 15); } buf[16]=0; jsvArrayPushAndUnLock(array, jsvNewFromString(buf)); } NOT_USED(LastFamilyDiscrepancy); } return array; }
u8 DS18B20Read(u8 pin, u8 num, u8 resolution, TEMPERATURE * t) { u8 res, busy = 0; u8 temp_lsb, temp_msb; switch (resolution) { case RES12BIT: res = Bin(01100000); break; // 12-bit resolution case RES11BIT: res = Bin(01000000); break; // 11-bit resolution case RES10BIT: res = Bin(00100000); break; // 10-bit resolution case RES9BIT: res = Bin(00000000); break; // 9-bit resolution default: res = Bin(00000000); break; // 9-bit resolution /// NB: The power-up default of these bits is R0 = 1 and R1 = 1 (12-bit resolution) } DS18B20Configure(pin, num, 0, 0, res); // no alarm if (OneWireReset(pin)) return FALSE; if (num == SKIPROM) { // Skip ROM, address all devices OneWireWrite(pin, SKIPROM); } else { // Talk to a particular device DS18B20MatchRom(pin, num); } OneWireWrite(pin, CONVERT_T); // Start temperature conversion while (busy == 0) // Wait while busy ( = bus is low) busy = OneWireRead(pin); if (OneWireReset(pin)) return FALSE; if (num == SKIPROM) { // Skip ROM, address all devices OneWireWrite(pin, SKIPROM); } else { // Talk to a particular device DS18B20MatchRom(pin, num); } OneWireWrite(pin, READ_SCRATCHPAD);// Read scratchpad temp_lsb = OneWireRead(pin); // byte 0 of scratchpad : temperature lsb temp_msb = OneWireRead(pin); // byte 1 of scratchpad : temperature msb OneWireReset(pin); // Calculation // --------------------------------------------------------------------- // Temperature Register Format // BIT7 BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0 // LS BYTE 2^3 2^2 2^1 2^0 2^-1 2^-2 2^-3 2^-4 // BIT15 BIT14 BIT13 BIT12 BIT11 BIT10 BIT9 BIT8 // MS BYTE S S S S S 2^6 2^5 2^4 // S = SIGN if (temp_msb >= Bin(11111000)) // test if temperature is negative { t->sign = 1; temp_msb -= Bin(11111000); } else { t->sign = 0; } t->integer = temp_lsb >> 4; // fractional part is removed, it remains only integer part t->integer |= (temp_msb << 4); // integer part from temp_msb is added t->fraction = 0; // fractional part ( if (BitRead(temp_lsb, 0)) t->fraction += 625; if (BitRead(temp_lsb, 1)) t->fraction += 1250; if (BitRead(temp_lsb, 2)) t->fraction += 2500; if (BitRead(temp_lsb, 3)) t->fraction += 5000; t->fraction /= 100; // two digits after decimal return TRUE; }