static void th_tl_dump(uint8_t *sp) { DS18X20_read_scratchpad( &gSensorIDs[0][0], sp, DS18X20_SP_SIZE ); uart_puts_P( "TH/TL in scratchpad of sensor 1 now : " ); uart_put_int( sp[DS18X20_TH_REG] ); uart_puts_P( " / " ); uart_put_int( sp[DS18X20_TL_REG] ); uart_puts_P( NEWLINESTR ); }
static void uart_put_temp(int16_t decicelsius) { char s[10]; uart_put_int( decicelsius ); uart_puts_P(" deci°C, "); DS18X20_format_from_decicelsius( decicelsius, s, 10 ); uart_puts( s ); uart_puts_P(" °C"); }
static void DS18X20_uart_put_temp(const uint8_t subzero, const uint8_t cel, const uint8_t cel_frac_bits) { char buffer[sizeof(int)*8+1]; size_t i; uart_putc((subzero)?'-':'+'); uart_put_int((int)cel); uart_puts_P("."); itoa(cel_frac_bits*DS18X20_FRACCONV,buffer,10); for ( i = 0; i < 4-strlen(buffer); i++ ) { uart_puts_P("0"); } uart_puts(buffer); uart_puts_P("°C"); }
/* 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
int main( void ) { uint8_t nSensors, i; int16_t decicelsius; uint8_t error; uart_init((UART_BAUD_SELECT((BAUD),F_CPU))); #ifndef OW_ONE_BUS ow_set_bus(&PINA,&PORTA,&DDRA,PA6); #endif sei(); uart_puts_P( NEWLINESTR "DS18X20 1-Wire-Reader Demo by Martin Thomas" NEWLINESTR ); uart_puts_P( "-------------------------------------------" ); nSensors = search_sensors(); uart_put_int( (int)nSensors ); uart_puts_P( " DS18X20 Sensor(s) available:" NEWLINESTR ); #if DS18X20_VERBOSE for (i = 0; i < nSensors; i++ ) { uart_puts_P("# in Bus :"); uart_put_int( (int)i + 1); uart_puts_P(" : "); DS18X20_show_id_uart( &gSensorIDs[i][0], OW_ROMCODE_SIZE ); uart_puts_P( NEWLINESTR ); } #endif for ( i = 0; i < nSensors; i++ ) { uart_puts_P( "Sensor# " ); uart_put_int( (int)i+1 ); uart_puts_P( " is a " ); if ( gSensorIDs[i][0] == DS18S20_FAMILY_CODE ) { uart_puts_P( "DS18S20/DS1820" ); } else if ( gSensorIDs[i][0] == DS1822_FAMILY_CODE ) { uart_puts_P( "DS1822" ); } else { uart_puts_P( "DS18B20" ); } uart_puts_P( " which is " ); if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) == DS18X20_POWER_PARASITE ) { uart_puts_P( "parasite" ); } else { uart_puts_P( "externally" ); } uart_puts_P( " powered" NEWLINESTR ); } #if DS18X20_EEPROMSUPPORT if ( nSensors > 0 ) { eeprom_test(); } #endif if ( nSensors == 1 ) { uart_puts_P( NEWLINESTR "There is only one sensor " "-> Demo of \"DS18X20_read_decicelsius_single\":" NEWLINESTR ); i = gSensorIDs[0][0]; // family-code for conversion-routine DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ); _delay_ms( DS18B20_TCONV_12BIT ); DS18X20_read_decicelsius_single( i, &decicelsius ); uart_put_temp( decicelsius ); uart_puts_P( NEWLINESTR ); } for(;;) { // main loop error = 0; if ( nSensors == 0 ) { error++; } uart_puts_P( NEWLINESTR "Convert_T and Read Sensor by Sensor (reverse order)" NEWLINESTR ); for ( i = nSensors; i > 0; i-- ) { if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, &gSensorIDs[i-1][0] ) == DS18X20_OK ) { _delay_ms( DS18B20_TCONV_12BIT ); uart_puts_P( "Sensor# " ); uart_put_int( (int) i ); uart_puts_P(" = "); if ( DS18X20_read_decicelsius( &gSensorIDs[i-1][0], &decicelsius) == DS18X20_OK ) { uart_put_temp( decicelsius ); } else { uart_puts_P( "CRC Error (lost connection?)" ); error++; } uart_puts_P( NEWLINESTR ); } else { uart_puts_P( "Start meas. failed (short circuit?)" ); error++; } } uart_puts_P( NEWLINESTR "Convert_T for all Sensors and Read Sensor by Sensor" NEWLINESTR ); if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { _delay_ms( DS18B20_TCONV_12BIT ); for ( i = 0; i < nSensors; i++ ) { uart_puts_P( "Sensor# " ); uart_put_int( (int)i + 1 ); uart_puts_P(" = "); if ( DS18X20_read_decicelsius( &gSensorIDs[i][0], &decicelsius ) == DS18X20_OK ) { uart_put_temp( decicelsius ); } else { uart_puts_P( "CRC Error (lost connection?)" ); error++; } uart_puts_P( NEWLINESTR ); } #if DS18X20_MAX_RESOLUTION int32_t temp_eminus4; for ( i = 0; i < nSensors; i++ ) { uart_puts_P( "Sensor# " ); uart_put_int( i+1 ); uart_puts_P(" = "); if ( DS18X20_read_maxres( &gSensorIDs[i][0], &temp_eminus4 ) == DS18X20_OK ) { uart_put_temp_maxres( temp_eminus4 ); } else { uart_puts_P( "CRC Error (lost connection?)" ); error++; } uart_puts_P( NEWLINESTR ); } #endif } else { uart_puts_P( "Start meas. failed (short circuit?)" ); error++; } #if DS18X20_VERBOSE // all devices: uart_puts_P( NEWLINESTR "Verbose output" NEWLINESTR ); DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ); _delay_ms( DS18B20_TCONV_12BIT ); DS18X20_read_meas_all_verbose(); #endif if ( error ) { uart_puts_P( "*** problems - rescanning bus ..." ); nSensors = search_sensors(); uart_put_int( (int) nSensors ); uart_puts_P( " DS18X20 Sensor(s) available" NEWLINESTR ); error = 0; } _delay_ms(3000); } }
int main( void ) { uint8_t i=0; int16_t decicelsius; uint8_t error; uint8_t delayCounter=0; hw_init(); uart_init((UART_BAUD_SELECT((BAUD),F_CPU))); memset(&sensor_fw, 0, sizeof(_sensor_data)); /* init 485 write */ DDRB |= 0b0000001; //1 = output, 0 = input //PORTB |= 0b00000001; //Enable pin 5 internal pullup PORTB &= 0b11111110; //Enable pin 5 internal pullup read //PORTB |= 0b00000001; //Enable pin 5 internal pullup 485 write led_g_on(); _delay_ms(1000); led_y_on(); _delay_ms(1000); led_r_on(); _delay_ms(1000); led_r_off(); _delay_ms(1000); led_y_off(); _delay_ms(1000); led_g_off(); #ifndef OW_ONE_BUS ow_set_bus(&PIND,&PORTD,&DDRD,PD6); #endif led_g_on(); search_bus(); led_g_off(); sei(); sensor_fw.fw_state = FW_STATE_SENSOR_START_MEAS; for(;;) { // main loop switch (sensor_fw.fw_state) { case FW_STATE_SENSOR_SEARCH: led_g_on(); search_bus(); led_g_off(); uart_puts_P("FW_STATE_SENSOR_SEARCH? =0\n"); sensor_fw.fw_state = FW_STATE_SENSOR_START_MEAS; break; case FW_STATE_SENSOR_START_MEAS: if ( sensor_fw.sensor_num == 0 ) { sensor_fw.fw_state = FW_STATE_SENSOR_SEARCH; uart_puts_P("error sensor num =0\n"); break; } if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { sensor_fw.fw_state = FW_STATE_SENSOR_DELAY_750ms; //reset_timeout(); } else { sensor_fw.fw_state = FW_STATE_SENSOR_SEARCH; uart_puts_P("error start mes faul =0\n"); } break; case FW_STATE_SENSOR_DELAY_750ms: //if (TCNT1 > 5400) //750 ms _delay_ms( DS18B20_TCONV_12BIT ); { sensor_fw.fw_state = FW_STATE_SENSOR_READ_I; i = 0; } break; case FW_STATE_SENSOR_READ_I: if ( DS18X20_read_decicelsius(&sensor_fw.sensors[i].id[0], &decicelsius ) == DS18X20_OK ) { sensor_fw.sensors[i].temp = decicelsius; uart_puts_P( "Sensor# " ); uart_put_int( (int)i + 1 ); uart_puts_P(" = "); uart_put_temp( decicelsius ); uart_puts_P( NEWLINESTR ); } else { //uart_puts_P( "CRC Error (lost connection?)" ); sensor_fw.fw_state = FW_STATE_SENSOR_SEARCH; uart_puts_P("error lost connection? =0\n"); break; } i++; if (i >= sensor_fw.sensor_num) { sensor_fw.fw_state = FW_STATE_SENSOR_DELAY_5s; delayCounter = 0; //reset_timeout(); } //uart_puts_P( NEWLINESTR ); break; case FW_STATE_SENSOR_DELAY_5s: //if (TCNT1 > (5 * TICKS_PER_SEC)) /*5s*/ _delay_ms( 1000 ); { if(delayCounter > 5) sensor_fw.fw_state = FW_STATE_SENSOR_START_MEAS; delayCounter++; } break; case FW_STATE_READ_COMM: { //uart_puts_P( "Communication cmd rx\n" ); switch(sensor_fw.comm.rxbuff[2]) { case 0x01: sensor_fw.comm.txbuff[0] = 0x7E; sensor_fw.comm.txbuff[1] = 0x01; // dev_id sensor_fw.comm.txbuff[2] = 0x02; // len sensor_fw.comm.txbuff[3] = 0x00; // len sensor_fw.comm.txbuff[4] = 0xBB; // data sensor_fw.comm.txbuff[5] = 0xBB; // data sensor_fw.comm.txbuff[6] = 0x00; // crc sensor_fw.comm.txbuff[7] = 0x00; // crc uart_putData(sensor_fw.comm.txbuff,8); break; } sensor_fw.comm.rxlen = 0; sensor_fw.comm.valid_cmd = 0; sensor_fw.fw_state = FW_STATE_SENSOR_START_MEAS; } break; } if(sensor_fw.comm.valid_cmd) { sensor_fw.fw_state = FW_STATE_READ_COMM; } //_delay_ms(3000); } }