void loopTempSensors(void) { error = 0; if ( nSensors == 0 ) { error++; } if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { for ( i = 0; i < nSensors; i++ ) { if ( DS18X20_read_maxres( &gSensorIDs[i][0], &temp_eminus4 ) == DS18X20_OK ) { ;; // we're cool - read a value. //uart_put_temp_maxres( temp_eminus4 ); } else { error++; } } } else { error++; } if ( error ) { nSensors = search_sensors(); //uart_put_int( (int) nSensors ); error = 0; } }
void measure_temp(uint8_t nSensors, int32_t* temp_eminus4) { uint8_t i = 0; uint8_t result = 0; if (DS18X20_start_meas( DS18X20_POWER_EXTERN, NULL) == DS18X20_OK) { _delay_ms(DS18B20_TCONV_12BIT); for (i = 0; i < nSensors; i++) { vTaskSuspendAll(); result = DS18X20_read_maxres(&gSensorIDs[i][0], &temp_eminus4[i]); xTaskResumeAll(); } } }
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 numSensors = 0, i; // unsigned long nextflash = 0; // Configure all pins as inputs with pullups initially DDRA = 0x00; PORTA = 0xff; DDRB = 0x00; PORTB = 0xff; // Serial output line DDRB |= _BV(PINB0); PORTB |= _BV(PINB0); // LED // DDRA |= _BV(PINA0); // Radio power is PA1 PORTA &= ~_BV(PINA1); DDRA |= _BV(PINA1); // Onewire power is PA2 PORTA &= ~_BV(PINA2); DDRA |= _BV(PINA2); myPutStr("Hello world\r\n"); // Various power-saving things // Disable BOD while sleeping. I hope. MCUCR |= (_BV(BODS) | _BV(BODSE)); MCUCR &= ~_BV(BODSE); MCUCR |= (_BV(BODS)); // Disable the ADC ADCSRA &= ~_BV(ADEN); // Disable the Analog Comparator ACSR |= _BV(ACD); // Disable clocking of timer1 and ADC PRR |= (_BV(PRTIM1)|_BV(PRADC)); timerInit(); wdtInit(); initInterrupts(); // Power up the Onewire bus PORTA |= _BV(PINA2); while(numSensors == 0) { myPutStr("Scanning for sensors\r\n"); numSensors = search_sensors(); myPutStr("Found "); myPutUint8(numSensors); myPutStr(" sensors\r\n"); for (i=0;i<numSensors;i++) { uint8_t j; myPutStr("Sensor "); myPutUint8(i); myPutStr(" address "); for (j=0;j<OW_ROMCODE_SIZE;j++) { myPutUint8(gSensorIDs[i][j]); } if (gSensorIDs[i][0] == DS18S20_FAMILY_CODE ) { myPutStr(" DS18S20/DS1820"); } else if ( gSensorIDs[i][0] == DS1822_FAMILY_CODE ) { myPutStr(" DS1822"); } else { myPutStr(" DS18B20"); } if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) == DS18X20_POWER_PARASITE ) { myPutStr(" parasite\r\n"); } else { myPutStr(" external\r\n"); } // Enable 12 bit mode (won't do anything on DS18S20) DS18X20_write_scratchpad(&gSensorIDs[i][0], 0, 0, DS18B20_12_BIT); DS18X20_scratchpad_to_eeprom(DS18X20_get_power_status( &gSensorIDs[i][0] ),&gSensorIDs[i][0]); } } while(1) { unsigned long wakepoint; myRadioBuf_t radiobuf; //char debugbuf[10]; // if ((signed long)now - (signed long)nextflash >= 0) { // debugLedToggle(0); // nextflash = now + 1000; // } // Power up the Onewire bus PORTA |= _BV(PINA2); // Let is stabilize for a few ms wakepoint = getMillis() + 15; numSensors = search_sensors(); while (! ((signed long)getMillis() - (signed long)wakepoint >= 0)) { set_sleep_mode(SLEEP_MODE_IDLE); sleep_mode(); } if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { wakepoint = getMillis() + DS18B20_TCONV_12BIT; while (! ((signed long)getMillis() - (signed long)wakepoint >= 0)) { set_sleep_mode(SLEEP_MODE_IDLE); sleep_mode(); } // Power the radio up PORTA |= _BV(PINA1); // Wakepoint set to now +100ms to allow radio to wake wakepoint = getMillis() + 100; for ( i = 0; i < numSensors; i++ ) { radiobuf.tenthousandths = -9999999L; if (DS18X20_read_maxres( &gSensorIDs[i][0], &(radiobuf.tenthousandths) ) != DS18X20_OK) { radiobuf.tenthousandths = -9999999L; } //myPutStr("Sensor "); uint8_t j; for (j=0;j<OW_ROMCODE_SIZE;j++) { radiobuf.sensid[j] = gSensorIDs[i][j]; //myPutUint8(gSensorIDs[i][j]); } //myPutStr(" = "); //sprintf(debugbuf, "%d.%d\r\n", (int)(radiobuf.tenthousandths/10000), (int)(radiobuf.tenthousandths % 10000)); //myPutStr(debugbuf); if (0 == i) { // First time around, radio not initialized while (! ((signed long)getMillis() - (signed long)wakepoint >= 0)) { set_sleep_mode(SLEEP_MODE_IDLE); sleep_mode(); } radioInit(); // No auto ack radioSetAutoAck(0); radioOpenWritingPipe(pipe); } radiobuf.tstamp = getMillis(); //myPutStr("about to radioWrite, i="); //myPutUint8(i); //myPutStr("..."); radioWrite(&radiobuf,sizeof(radiobuf)); //myPutStr("Done\r\n"); } // Power the radio down PORTA &= ~_BV(PINA1); // Plus the CE and CSN pins PORTA &= ~_BV(PINA7); PORTB &= ~_BV(PINB2); // And the onewire bus PORTA &= ~_BV(PINA2); } else { myPutStr("Error measuring sensors\n"); } //tmp = getMillis(); //radioWrite(&tmp , sizeof(unsigned long) ); // Sleep hard until WDT fires set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_mode(); } }