uint8_t classify_sensors(uint8_t nSensors) { int i = 0; /* classify sensors */ for (i = 0; i < nSensors; i++) { if (gSensorIDs[i][0] == DS18B20_FAMILY_CODE) { CDC_Device_SendString(&USB_Interface, "DS18B20 found "); } if (DS18X20_get_power_status( &gSensorIDs[i][0]) == DS18X20_POWER_PARASITE) { CDC_Device_SendString(&USB_Interface, "parasite\n\r"); } else { CDC_Device_SendString(&USB_Interface, "externally\n\r"); } } }
void search_bus() { uint8_t i; while(1) { sensor_fw.sensor_num = search_sensors(); if (sensor_fw.sensor_num > 0) break; _delay_ms(1000); } /* get power status */ for ( i = 0; i < sensor_fw.sensor_num; i++ ) { if ( DS18X20_get_power_status(&sensor_fw.sensors[i].id[0] ) == DS18X20_POWER_PARASITE ) { sensor_fw.sensors[i].power = POWER_PARASITE; } else { sensor_fw.sensors[i].power = POWER_EXTERNAL; } } }
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); } }
/*----------------------------------------------------------------------------- * Main Program *---------------------------------------------------------------------------*/ int main(void) { uint8_t nSensors, i; uint8_t subzero, cel, cel_frac_bits; Mcu_Init(); Timebase_Init(); Serial_Init(); sei(); printf( "\nDS18X20 1-Wire-Reader\n" ); printf( "-----------------------" ); nSensors = search_sensors(); printf( "%i DS18X20 Sensor(s) available:\n", (int) nSensors ); for (i=0; i<nSensors; i++) { printf("Sensor# %i is a ", (int) i+1); if ( gSensorIDs[i][0] == DS18S20_ID) printf("DS18S20/DS1820"); else printf("DS18B20"); printf(" which is "); if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) == DS18X20_POWER_PARASITE ) printf( "parasite" ); else printf( "externally" ); printf( " powered\n" ); } printf("CanInit...\n"); if (Can_Init() != CAN_OK) { printf("FAILED!\n"); } else { printf("OK!\n"); } uint32_t timeStamp = 0; Can_Message_t txMsg; Can_Message_t rxMsg; txMsg.DataLength = 2; txMsg.Id = 0; txMsg.RemoteFlag = 0; txMsg.ExtendedFlag = 1; /* main loop */ while (1) { /* service the CAN routines */ Can_Service(); /* check if any messages have been received */ while (Can_Receive(&rxMsg) == CAN_OK) { } /* check temperature and send on CAN once every other second */ if (Timebase_PassedTimeMillis(timeStamp) >= 2000) { timeStamp = Timebase_CurrentTime(); if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { printf("Measuring temperature... "); delay_ms(DS18B20_TCONV_12BIT); for ( i=0; i<nSensors; i++ ) { if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero, &cel, &cel_frac_bits) == DS18X20_OK ) { //txMsg.Data.bytes[0] = subzero; if (subzero) { txMsg.Data.bytes[i*2] = -cel; txMsg.Data.bytes[i*2+1] = ~(cel_frac_bits<<4); } else { txMsg.Data.bytes[i*2] = cel; txMsg.Data.bytes[i*2+1] = (cel_frac_bits<<4); } } else printf("CRC Error (lost connection?)\n"); } txMsg.DataLength = nSensors*2; printf("sending...\n"); /* send txMsg */ Can_Send(&txMsg); } else printf("Start meas. failed (short circuit?)\n"); } } return 0; }
/** * \ingroup usartcmdline * \b OWREAD-Befehl DS18x20 auf Bus suchen und anzeigen */ int16_t command_OWlookup(char *outbuffer) { if (outbuffer) // nur bei USART return cmd_502(outbuffer); #if USE_OW uint8_t i; uint8_t diff, nSens; uint16_t TWert; uint8_t subzero, cel, cel_frac_bits; uint8_t gSensorIDs[MAXLOOKUP][OW_ROMCODE_SIZE]; usart_write("\r\nScanning Bus for DS18X20"); nSens = 0; for( diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE && nSens < MAXLOOKUP ; ) { DS18X20_find_sensor( &diff, &gSensorIDs[nSens][0] ); if( diff == OW_PRESENCE_ERR ) { usart_write("\r\nNo Sensor found"); break; } if( diff == OW_DATA_ERR ) { usart_write("\r\nBus Error"); break; } nSens++; } usart_write("\n\r%i 1-Wire Sensoren gefunden.\r\n", nSens); // for (i=0; i<nSens; i++) { // // set 10-bit Resolution - Alarm-low-T 0 - Alarm-high-T 85 // DS18X20_write_scratchpad( &gSensorIDs[i][0] , 0, 85, DS18B20_12_BIT); // } for (i=0; i<nSens; i++) { usart_write("\r\n#%i ist ein ",(int) i+1); if ( gSensorIDs[i][0] == DS18S20_ID) usart_write("DS18S20/DS1820"); else usart_write("DS18B20"); usart_write(" mit "); if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) == DS18X20_POWER_PARASITE ) usart_write( "parasitaerer" ); else usart_write( "externer" ); usart_write( " Spannungsversorgung. " ); // T messen if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, &gSensorIDs[i][0] ) == DS18X20_OK ) { _delay_ms(DS18B20_TCONV_12BIT); if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero, &cel, &cel_frac_bits) == DS18X20_OK ) { DS18X20_show_id_uart( &gSensorIDs[i][0], OW_ROMCODE_SIZE ); TWert = DS18X20_temp_to_decicel(subzero, cel, cel_frac_bits); usart_write(" %i %i.%4i C %i",subzero, cel, cel_frac_bits,TWert); } else usart_write(" CRC Error (lost connection?)"); } else usart_write(" *** Messung fehlgeschlagen. (Kurzschluss?) ***"); } #endif return 0; }
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(); } }