void find_sensor(uint8_t *diff, uint8_t id[]) { for (;;) { *diff = ow_rom_search( *diff, &id[0] ); if ( *diff==OW_PRESENCE_ERR || *diff==OW_DATA_ERR || *diff == OW_LAST_DEVICE ) return; return; } }
/* find DS18X20 Sensors on 1-Wire-Bus input/ouput: diff is the result of the last rom-search output: id is the rom-code of the sensor found */ void DS18X20_find_sensor(uint8_t *diff, uint8_t id[]) { for (;;) { *diff = ow_rom_search( *diff, &id[0] ); if ( *diff==OW_PRESENCE_ERR || *diff==OW_DATA_ERR || *diff == OW_LAST_DEVICE ) return; if ( id[0] == DS18B20_ID || id[0] == DS18S20_ID ) return; } }
/* 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