/** This routine writes the PIO bits * * \param id The serial number of the switch that is to be turned on or off * \param state The PIO Status Bit Assignment. (2 LSBs) * \return true(1): If set is successful * false(0): If set is not successful */ uint8_t ow_ds2413_write(uint8_t id[], uint8_t state) { uint8_t buff[5]; uint8_t wrByte; wrByte = state | 0xFC; // PIO Access Write Command ow_command(0x5a, id); // Channel write information buff[1] = ow_byte_wr(wrByte); // Inverted write byte buff[2] = ow_byte_wr(~wrByte); // Used to read the Status Bits buff[3] = ow_byte_wr(0xFF); // Used to read the PIO State buff[4] = ow_byte_wr(0xFF); if (buff[3] != 0xAA) { return false; } return true; }
uint8_t DS18X20_write_scratchpad( uint8_t id[], uint8_t th, uint8_t tl, uint8_t conf) { ow_reset(); //** if( ow_input_pin_state() ) { // only send if bus is "idle" = high ow_command( DS18X20_WRITE_SCRATCHPAD, id ); ow_byte_wr(th); ow_byte_wr(tl); if (id[0] == DS18B20_ID) ow_byte_wr(conf); // config avail. on B20 only return DS18X20_OK; } else { return DS18X20_ERROR; } }
/************************************************************* Send command to specific device **************************************************************/ void ow_command( uint8_t command, uint8_t *id ) { uint8_t i; ow_reset(); if( id ) { /* to a single device*/ ow_byte_wr( OW_MATCH_ROM ); i = OW_ROMCODE_SIZE; do { ow_byte_wr( *id ); id++; } while( --i ); } else /* to all devices*/ ow_byte_wr( OW_SKIP_ROM ); ow_byte_wr( command ); }
uint8_t DS18X20_write_scratchpad(uint8_t id[], uint8_t th, uint8_t tl, uint8_t conf) { ow_reset(); //** if (ow_input_pin_state()) { // only send if bus is "idle" = high ow_command(DS18X20_WRITE_SCRATCHPAD, id); ow_byte_wr(th); ow_byte_wr(tl); if (id[0] == DS18B20_ID) ow_byte_wr(conf); // config avail. on B20 only return DS18X20_OK; } else { #ifdef DS18X20_VERBOSE printf_P(PSTR("DS18X20_write_scratchpad: Short Circuit !\r")); #endif return DS18X20_ERROR; } }
/* * 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); }
/** This routine reads the PIO bits * * \param id The serial number of the switch that is to be turned on or off * \return true(1): If set is successful * false(0): If set is not successful */ uint8_t ow_ds2413_read(uint8_t id[]) { // PIO Access Read Command ow_command(0xf5, id); // Used to read the PIO Status Bit Assignment return ow_byte_wr(0xFF); }
void ow_command( uint8_t command, const uint8_t id[] ) { uint8_t i; ow_reset(); if( id ) { ow_byte_wr( OW_MATCH_ROM ); // to a single device i = OW_ROMCODE_SIZE; do { ow_byte_wr( *id ); id++; } while( --i ); } else { ow_byte_wr( OW_SKIP_ROM ); // to all devices } ow_byte_wr( command ); }
static void ow_command_intern( uint8_t command, uint8_t *id, uint8_t with_parasite_enable ) { uint8_t i; ow_reset(); if( id ) { ow_byte_wr( OW_MATCH_ROM ); // to a single device i = OW_ROMCODE_SIZE; do { ow_byte_wr( *id ); id++; } while( --i ); } else { ow_byte_wr( OW_SKIP_ROM ); // to all devices } if ( with_parasite_enable ) { ow_byte_wr_with_parasite_enable( command ); } else { ow_byte_wr( command ); } }
uint8_t ow_rom_search( uint8_t diff, uint8_t *id ) { uint8_t i, j, next_diff; uint8_t b; if( ow_reset() ) { return OW_PRESENCE_ERR; // error, no device found <--- early exit! } ow_byte_wr( OW_SEARCH_ROM ); // ROM search command next_diff = OW_LAST_DEVICE; // unchanged on last device i = OW_ROMCODE_SIZE * 8; // 8 bytes do { j = 8; // 8 bits do { b = ow_bit_io( 1 ); // read bit if( ow_bit_io( 1 ) ) { // read complement bit if( b ) { // 0b11 return OW_DATA_ERR; // data error <--- early exit! } } else { if( !b ) { // 0b00 = 2 devices if( diff > i || ((*id & 1) && diff != i) ) { b = 1; // now 1 next_diff = i; // next pass 0 } } } ow_bit_io( b ); // write bit *id >>= 1; if( b ) { *id |= 0x80; // store bit } i--; } while( --j ); id++; // next byte } while( i ); return next_diff; // to continue search }
/*************************************** * * -***************************************/ uint8_t ow_rom_search( uint8_t diff, uint8_t *id ) { uint8_t i, j, next_diff; uint8_t b; if( ow_reset() ) return OW_PRESENCE_ERR;/* error, no device found*/ ow_byte_wr( OW_SEARCH_ROM ); /*ROM search command*/ next_diff = OW_LAST_DEVICE; /* unchanged on last device*/ i = OW_ROMCODE_SIZE * 8; /* 8 bytes*/ do { j = 8; /* 8 bits*/ do{ b = ow_bit_io( 1 ); /* read bit*/ if( ow_bit_io( 1 ) ) { /* read complement bit*/ if( b )/* 11*/ return OW_DATA_ERR; /* data error*/ } else { if( !b ) { /* 00 = 2 devices*/ if( diff > i || ((*id & 1) && diff != i) ) { b = 1; /* now 1*/ next_diff = i; /* next pass 0*/ } } } ow_bit_io( b ); /* write bit*/ *id >>= 1; if( b ) *id |= 0x80;/* store bit*/ i--; } while( --j ); id++; /* next byte*/ } while( i ); /* to continue search*/ return next_diff; }
uint8_t ow_byte_rd( void ) { // read by sending 0xff (a dontcare?) return ow_byte_wr( 0xFF ); }
/* 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
uint8_t ow_byte_rd( void ) { // read by sending only "1"s, so bus gets released // after the init low-pulse in every slot return ow_byte_wr( 0xFF ); }