//#define CONSOLE_FLOAT_S( dp ,cp, var ) sprintf(cp, "%03d.%03d\r\n\0", (unsigned int)floor(var), (unsigned int)((var-floor(var))*pow(10, dp))); static float ds1820_read_device(uint8_t * rom_code){ float retval; uint16_t ds1820_temperature1 = 10000; int ii; uint8_t code = 0; uint8_t sp1[9]; //temp to hold scratchpad memory char c[25]; int tt = 0; float rem = 0.0; //portENTER_CRITICAL(); code = ds1820_reset(); if (code != NO_ERROR) ds1820_error(code); portENTER_CRITICAL(); ds1820_reset(); ds1820_write_byte(MATCH_ROM); for (ii = 0; ii < 8; ii++) { ds1820_write_byte(rom_code[ii]); } ds1820_write_byte(READ_SCRATCHPAD); for (ii = 0; ii < 9; ii++){ sp1[ii] = ds1820_read_byte(); } ds1820_reset(); ds1820_temperature1 = sp1[1] & 0x0f; ds1820_temperature1 <<= 8; ds1820_temperature1 |= sp1[0]; // if the sensor is a 'B' type, we need a different conversion if (rom_code[0] == '\x28') { ds1820_temperature1 >>= 4; rem = 0.0; if (sp1[0]&(0x01)) rem += POW_NEG_4; if (sp1[0]&(0x02)) rem += POW_NEG_3; if (sp1[0]&(0x04)) rem += POW_NEG_2; if (sp1[0]&(0x08)) rem += POW_NEG_1; retval = (float)ds1820_temperature1 + rem; }
static uint8_t ds1820_search(){ uint8_t ii; //char console_text[30]; ds1820_reset(); ds1820_write_byte(READ_ROM); portENTER_CRITICAL(); for (ii = 0; ii < 8; ii++) { rom[ii] = ds1820_read_byte(); } portEXIT_CRITICAL(); ds1820_reset(); return 0; }
/** * Called from ds1820_refrech_all() */ int ds1820_get_temp(int sensor){ uint8_t scratchpad[9]; int temp_val = 0; int temp = 0; do{ if(ds1820_reset(sensor)){ return DS1820_TEMP_NONE; } ds1820_write_byte(DS1820_CMD_SKIPROM, sensor); ds1820_write_byte(DS1820_CMD_RSCRATCHPAD, sensor); for(int i = 0; i < 9; i++){ //Read scratchpad ds1820_read_byte(&scratchpad[i], sensor); } ds1820_reset(sensor); }while(ds1820_crc_check(scratchpad)); //while CRC matches //temp = MSB LSB temp_val = scratchpad[0]; temp_val |= (scratchpad[1]<<8); temp_val <<= 7; //keep sign bit temp_val &= 0xFF00; //clear LSB temp_val -= 0x40; //subtract 0.25d = 40h temp_val += (((scratchpad[7] - scratchpad[6])<<8) / scratchpad[7]); temp = temp_val >> 8; temp *= 10; temp += (((temp_val & 0x00FF) * 100)>>8) / 10; return temp; }