/* * Reads temp from the DS18B20 */ double ds1820_read(void) { ADCSRA &= ~(1<<ADEN)|~(1<<ADSC); uint8_t busy=0, temp1, temp2; int16_t temp3; double result; ow_reset(); ow_byte_wr(0xCC); ow_byte_wr(0x44); while(busy == 0) { busy = ow_byte_rd(); } ow_reset(); ow_byte_wr(0xCC); ow_byte_wr(0xBE); temp1 = ow_byte_rd(); temp2 = ow_byte_rd(); /*onewire_write(0xCC); //Skip ROM, address all devices onewire_write(0x44); //Start temperature conversion while(busy == 0) //Wait while busy (bus is low) busy = onewire_read(); onewire_reset(); onewire_write(0xCC); //Skip ROM, address all devices onewire_write(0xBE); //Read scratchpad temp1 = onewire_read(); temp2 = onewire_read();*/ temp3 = temp2; temp3 = temp3 << 8; temp3 = temp3 | temp1; //result = (float) temp3 / 2.0; //Calculation for DS18S20 with 0.5 deg C resolution result = (double) temp3 / 16.0; //Calculation for DS18B20 //_delay_ms(200); return(result); }
/* reads temperature (scratchpad) of sensor with rom-code id output: subzero==1 if temp.<0, cel: full celsius, mcel: frac in millicelsius*0.1 i.e.: subzero=1, cel=18, millicel=5000 = -18,5000°C */ uint8_t DS18X20_read_meas(uint8_t *id, uint8_t *subzero, uint8_t *cel, uint8_t *cel_frac_bits) { uint8_t i; uint8_t sp[DS18X20_SP_SIZE]; ow_reset(); //** ow_command(DS18X20_READ, id); for ( i=0 ; i< DS18X20_SP_SIZE; i++ ) sp[i]=ow_byte_rd(); if ( crc8( &sp[0], DS18X20_SP_SIZE ) ) return DS18X20_ERROR_CRC; DS18X20_meas_to_cel(id[0], sp, subzero, cel, cel_frac_bits); return DS18X20_OK; }
/* reads temperature (scratchpad) of a single sensor (uses skip-rom) output: subzero==1 if temp.<0, cel: full celsius, mcel: frac in millicelsius*0.1 i.e.: subzero=1, cel=18, millicel=5000 = -18,5000�C */ uint8_t DS18X20_read_meas_single(uint8_t familycode, uint8_t *subzero, uint8_t *cel, uint8_t *cel_frac_bits, uint16_t *maalt) { uint8_t i; uint8_t sp[DS18X20_SP_SIZE]; ow_command(DS18X20_READ, NULL); for (i = 0 ; i < DS18X20_SP_SIZE; i++) sp[i] = ow_byte_rd(); if (crc8(&sp[0], DS18X20_SP_SIZE)) return DS18X20_ERROR_CRC; DS18X20_meas_to_cel(familycode, sp, subzero, cel, cel_frac_bits, maalt); return DS18X20_OK; }
uint8_t DS18X20_read_scratchpad( uint8_t id[], uint8_t sp[] ) { uint8_t i; ow_reset(); //** if( ow_input_pin_state() ) { // only send if bus is "idle" = high ow_command( DS18X20_READ, id ); for ( i=0 ; i< DS18X20_SP_SIZE; i++ ) sp[i]=ow_byte_rd(); return DS18X20_OK; } else { return DS18X20_ERROR; } }
uint8_t DS18X20_read_scratchpad(uint8_t id[], uint8_t sp[]) { uint8_t i; ow_reset(); //** if (ow_input_pin_state()) { // only send if bus is "idle" = high ow_command(DS18X20_READ, id); for (i = 0 ; i < DS18X20_SP_SIZE; i++) sp[i] = ow_byte_rd(); return DS18X20_OK; } else { #ifdef DS18X20_VERBOSE printf_P(PSTR("DS18X20_read_scratchpad: Short Circuit !\r")); #endif return DS18X20_ERROR; } }
/* verbose output rom-search follows read-scratchpad in one loop */ uint8_t DS18X20_read_meas_all_verbose( void ) { uint8_t id[OW_ROMCODE_SIZE], sp[DS18X20_SP_SIZE], diff; uint8_t i; uint16_t meas; int16_t decicelsius; char s[10]; uint8_t subzero, cel, cel_frac_bits; for( diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; ) { diff = ow_rom_search( diff, &id[0] ); if( diff == OW_PRESENCE_ERR ) { uart_puts_P( "No Sensor found\r" ); return OW_PRESENCE_ERR; // <--- early exit! } if( diff == OW_DATA_ERR ) { uart_puts_P( "Bus Error\r" ); return OW_DATA_ERR; // <--- early exit! } DS18X20_show_id_uart( id, OW_ROMCODE_SIZE ); if( id[0] == DS18B20_FAMILY_CODE || id[0] == DS18S20_FAMILY_CODE || id[0] == DS1822_FAMILY_CODE ) { // temperature sensor uart_putc ('\r'); ow_byte_wr( DS18X20_READ ); // read command for ( i=0 ; i< DS18X20_SP_SIZE; i++ ) { sp[i]=ow_byte_rd(); } show_sp_uart( sp, DS18X20_SP_SIZE ); if ( crc8( &sp[0], DS18X20_SP_SIZE ) ) { uart_puts_P( " CRC FAIL " ); } else { uart_puts_P( " CRC O.K. " ); } uart_putc ('\r'); meas = sp[0]; // LSB Temp. from Scrachpad-Data meas |= (uint16_t) (sp[1] << 8); // MSB uart_puts_P( " T_raw="); uart_puthex_byte( (uint8_t)(meas >> 8) ); uart_puthex_byte( (uint8_t)meas ); uart_puts_P( " " ); if( id[0] == DS18S20_FAMILY_CODE ) { // 18S20 uart_puts_P( "S20/09" ); } else if ( id[0] == DS18B20_FAMILY_CODE || id[0] == DS1822_FAMILY_CODE ) { // 18B20 or 1822 i=sp[DS18B20_CONF_REG]; if ( (i & DS18B20_12_BIT) == DS18B20_12_BIT ) { uart_puts_P( "B20/12" ); } else if ( (i & DS18B20_11_BIT) == DS18B20_11_BIT ) { uart_puts_P( "B20/11" ); } else if ( (i & DS18B20_10_BIT) == DS18B20_10_BIT ) { uart_puts_P( " B20/10 " ); } else { // if ( (i & DS18B20_9_BIT) == DS18B20_9_BIT ) { uart_puts_P( "B20/09" ); } } uart_puts_P(" "); DS18X20_meas_to_cel( id[0], sp, &subzero, &cel, &cel_frac_bits ); DS18X20_uart_put_temp( subzero, cel, cel_frac_bits ); decicelsius = DS18X20_raw_to_decicelsius( id[0], sp ); if ( decicelsius == DS18X20_INVALID_DECICELSIUS ) { uart_puts_P("* INVALID *"); } else { uart_puts_P(" conv: "); uart_put_int(decicelsius); uart_puts_P(" deci°C "); DS18X20_format_from_decicelsius( decicelsius, s, 10 ); uart_puts_P(" fmt: "); uart_puts(s); uart_puts_P(" °C "); } uart_puts("\r"); } // if meas-sensor } // loop all sensors
/* verbose output rom-search follows read-scratchpad in one loop */ uint8_t DS18X20_read_meas_all_verbose( void ) { uint8_t id[OW_ROMCODE_SIZE], sp[DS18X20_SP_SIZE], diff; uint8_t i; uint16_t meas; uint8_t subzero, cel, cel_frac_bits; for( diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; ) { diff = ow_rom_search( diff, &id[0] ); if( diff == OW_PRESENCE_ERR ) { uart_puts_P( "No Sensor found\r\n" ); return OW_PRESENCE_ERR; } if( diff == OW_DATA_ERR ) { uart_puts_P( "Bus Error\r\n" ); return OW_DATA_ERR; } DS18X20_show_id_uart( id, OW_ROMCODE_SIZE ); if( id[0] == DS18B20_ID || id[0] == DS18S20_ID ) { // temperature sensor uart_puts_P ("\r\n"); ow_byte_wr( DS18X20_READ ); // read command for ( i=0 ; i< DS18X20_SP_SIZE; i++ ) sp[i]=ow_byte_rd(); show_sp_uart( sp, DS18X20_SP_SIZE ); if ( crc8( &sp[0], DS18X20_SP_SIZE ) ) uart_puts_P( " CRC FAIL " ); else uart_puts_P( " CRC O.K. " ); uart_puts_P ("\r\n"); meas = sp[0]; // LSB Temp. from Scrachpad-Data meas |= (uint16_t) (sp[1] << 8); // MSB uart_puts_P(" T_raw="); uart_puthex_byte((uint8_t)(meas>>8)); uart_puthex_byte((uint8_t)meas); uart_puts_P(" "); if( id[0] == DS18S20_ID ) { // 18S20 uart_puts_P( "S20/09" ); } else if ( id[0] == DS18B20_ID ) { // 18B20 i=sp[DS18B20_CONF_REG]; if ( (i & DS18B20_12_BIT) == DS18B20_12_BIT ) { uart_puts_P( "B20/12" ); } else if ( (i & DS18B20_11_BIT) == DS18B20_11_BIT ) { uart_puts_P( "B20/11" ); } else if ( (i & DS18B20_10_BIT) == DS18B20_10_BIT ) { uart_puts_P( " B20/10 " ); } else { // if ( (i & DS18B20_9_BIT) == DS18B20_9_BIT ) { uart_puts_P( "B20/09" ); } } uart_puts_P(" "); DS18X20_meas_to_cel(id[0], sp, &subzero, &cel, &cel_frac_bits); DS18X20_uart_put_temp(subzero, cel, cel_frac_bits); uart_puts("\r\n"); } // if meas-sensor } // loop all sensors