void OneWireSearchRom(void) { unsigned char bit,bit_complementary,pozycja,buffer[NUM_OF_THERMS]; for(unsigned char i=0;i < NUM_OF_THERMS;i++) { buffer[i]=0; } for(unsigned char i=0;i < NUM_OF_THERMS;i++) { pozycja=0; if (!OneWireReset()) return; OneWireWriteByte(0xF0); for(unsigned char bitNo=0;bitNo<64;bitNo++) { bit=OneWireReadBit(); bit_complementary=OneWireReadBit(); if ((!(bit|bit_complementary))&1) { if(buffer[pozycja+1]==0) { buffer[pozycja]++; } OneWireWriteBit((buffer[pozycja]-1)); ROM[i][bitNo/8]|=((buffer[pozycja]-1))<<(bitNo%8); pozycja++; } else { OneWireWriteBit(bit); ROM[i][bitNo/8]|=bit<<(bitNo%8); } } while(buffer[pozycja-1]==2) { buffer[pozycja]=0; pozycja--; } } }
int ds18b20_Read(){ for(int i=0;i < NUM_OF_THERMS;i++){ unsigned char scratchpad[2]; if (!OneWireReset()) {return 0;} if(NUM_OF_THERMS > 1){ OneWireWriteByte(0x55); for(int j=0;j<8;j++){ OneWireWriteByte(ROM[i][j]); } } else{ OneWireWriteByte(0xcc); } OneWireWriteByte(0xBE); for(int k=0; k<2; k++) scratchpad[k] = OneWireReadByte(); temperatures[i]= ((scratchpad[1] << 8) + scratchpad[0]) / 16.0; } return 1; }
int ds18b20_SetResolution(char resolution) { if(resolution == 12){ resolution = 0x7F; } else if(resolution == 11){ resolution = 0x5F; } else if(resolution == 10){ resolution = 0x3F; } else if(resolution == 9){ resolution = 0x1F; } else{ resolution = 0x7F; } for(int i=0;i < NUM_OF_THERMS;i++){ if (!OneWireReset()) {return 0;} if(NUM_OF_THERMS > 1){ OneWireWriteByte(0x55); for(int j=0;j<8;j++){ OneWireWriteByte(ROM[i][j]); } } else{ OneWireWriteByte(0xcc); } OneWireWriteByte(0x4E); OneWireWriteByte(0x00); OneWireWriteByte(0x00); OneWireWriteByte(resolution); if (!OneWireReset()) {return 0;} OneWireWriteByte(0x55); for(int j=0;j<8;j++){ OneWireWriteByte(ROM[i][j]); } OneWireWriteByte(0x48); _delay_ms(10); } return 1; }
unsigned char OneWireTouchByte(unsigned char sendbyte) { if (sendbyte == 0xff) return OneWireReadByte(); else { OneWireWriteByte(sendbyte); return sendbyte; } }
unsigned char ds18b20_ConvertT(void){ if (!OneWireReset()) return 0; OneWireWriteByte(0xcc); // SKIP ROM OneWireWriteByte(0x44); // CONVERT T return -1; }
/** * The 'OneWireSearch' function does a general search. This function continues from * the previous search state. The search state can be reset by using the * 'OneWireFirst' function. * * @retval true When a 1-Wire device was found and its Serial Number placed in * the global ROM * @retval false When no new device was found. Either the last search was * the last device or there are no devices on the 1-Wire Net. */ unsigned char OneWireSearch(void) { unsigned char id_bit_number; unsigned char last_zero, rom_byte_number, search_result; unsigned char id_bit, cmp_id_bit; unsigned char rom_byte_mask, search_direction, status; // initialize for search id_bit_number = 1; last_zero = 0; rom_byte_number = 0; rom_byte_mask = 1; search_result = false; // if the last call was not the last one if (!LastDeviceFlag) { // 1-Wire reset if (!OneWireReset()) { // reset the search LastDiscrepancy = 0; LastDeviceFlag = false; LastFamilyDiscrepancy = 0; return false; } // issue the search command OneWireWriteByte(0xF0); // loop to do the search do { // if this discrepancy if before the Last Discrepancy // on a previous next then pick the same as last time if (id_bit_number < LastDiscrepancy) { if ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0) search_direction = 1; else search_direction = 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; } // Perform a triple operation on the DS2482 which will perform // 2 read bits and 1 write bit status = DS2482SearchTriplet(search_direction); // check bit results in status byte id_bit = ((status & DS2482_STATUS_SBR) == DS2482_STATUS_SBR); cmp_id_bit = ((status & DS2482_STATUS_TSB) == DS2482_STATUS_TSB); search_direction = ((status & DS2482_STATUS_DIR) == DS2482_STATUS_DIR) ? (unsigned char) 1 : (unsigned char) 0; // check for no devices on 1-Wire if ((id_bit) && (cmp_id_bit)) break; else { if ((!id_bit) && (!cmp_id_bit) && (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; // 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) { 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] == 0)) { LastDiscrepancy = 0; LastDeviceFlag = false; LastFamilyDiscrepancy = 0; search_result = false; } return search_result; }