int OWReadByte(void) { int loop, result = 0; for (loop = 0; loop < 8; loop++) { result >>= 1; if (OWReadBit()) result |= 0x80; } return result; }
//----------------------------------------------------------------------------- // Read 1-Wire data byte and return it // int OWReadByte(void) { int loop, result=0; for (loop = 0; loop < 8; loop++) { // shift the result to get it ready for the next bit result >>= 1; // if result is one, then set MS bit if (OWReadBit()) result |= 0x80; } return result; }
//----------------------------------------------------------------------------- // Write a 1-Wire data byte and return the sampled result. // int OWTouchByte(int data) { int loop, result=0; for (loop = 0; loop < 8; loop++) { // shift the result to get it ready for the next bit result >>= 1; // If sending a '1' then read a bit else write a '0' if (data & 0x01) { if (OWReadBit()) result |= 0x80; } else OWWriteBit(0); // shift the data byte for the next bit data >>= 1; } return result; }
//-------------------------------------------------------------------------- //! Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing //! search state. //! @return TRUE : device found, ROM number in ROM_NO buffer //! FALSE : device not found, end of search // int OWSearch() { int id_bit_number; int last_zero, rom_byte_number, search_result; int id_bit, cmp_id_bit; uchar 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; crc8 = 0; // if the last call was not the last one if (!LastDeviceFlag) { // 1-Wire reset if (!OWReset()) { // reset the search LastDiscrepancy = 0; LastDeviceFlag = FALSE; LastFamilyDiscrepancy = 0; return FALSE; } // issue the search command OWWriteByte(0xF0); // loop to do the search do { // read a bit and its complement id_bit = OWReadBit(); cmp_id_bit = OWReadBit(); // 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 if (id_bit_number == LastDiscrepancy) search_direction = 1; else search_direction = 0; // 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] &= ~rom_byte_mask; // serial number search direction write bit OWWriteBit(search_direction); // increment the byte counter id_bit_number // and shift the mask rom_byte_mask id_bit_number++; 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) { docrc8(ROM_NO[rom_byte_number]); // accumulate the CRC 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) || (crc8 != 0))) { // 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; } return search_result; }
/** * @brief Reads temperature on a ds1820 temperature sensor * @param temperature Pointer to the measured temperature * @return 0 if temperature read successfully */ int bbb_one_wire_DS1820_read(float* temperature, string* data) { /* int get[10]; char temp_lsb,temp_msb; int k; char temp_f,temp_c; OWTouchReset(); OWWriteByte(0xCC); //Skip ROM OWWriteByte(0x44); // Start Conversion tickDelay(5); OWTouchReset(); OWWriteByte(0xCC); // Skip ROM OWWriteByte(0xBE); // Read Scratch Pad for (k=0;k<9;k++) { get[k] = OWReadByte(); } cout << "ScratchPAD DATA = " << get[8] << get[7] << get[6] << get[5] << get[4] << get[3] << get[2] << get[1] << get[0] << endl; temp_msb = get[1]; // Sign byte + lsbit temp_lsb = get[0]; // Temp data plus lsb if (temp_msb <= 0x80) { temp_lsb = (temp_lsb/2); } // shift to get whole degree temp_msb = temp_msb & 0x80; // mask all but the sign bit if (temp_msb >= 0x80) { temp_lsb = (~temp_lsb)+1; } // twos complement if (temp_msb >= 0x80) { temp_lsb = (temp_lsb/2); }// shift to get whole degree if (temp_msb >= 0x80) { temp_lsb = ((-1)*temp_lsb); } // add sign bit //cout << "TempC= " << (int)temp_lsb << " degrees C" << endl;// print temp. C *temperature = (int)temp_lsb; return 1; */ int i = 0; int a = 1; unsigned short dataCRC16; OWTouchReset(); OWWriteByte(SKIP_ROM); OWWriteByte(CONVERT_T); while(a) { tickDelay(5); if(OWReadBit()) a = 0; } OWTouchReset(); OWWriteByte(SKIP_ROM); OWWriteByte(READ_SCRATCHPAD); *temperature = OWReadByte() + (OWReadByte() << 8); return 1; /* if(OWTouchReset()) return 0; OWWriteByte(0xCC); OWWriteByte(0xA5); OWWriteByte((page << 5) & 0xFF); OWWriteByte(0); for(i = 0; i < 32; i++) *data += OWReadByte() + "-"; OWWriteByte(0xFF); */ // return result; }