#include /* Read up to X number of DS18S20 1-wire thermometers and output the temperate in C onto the serial port. Output format is D=2E:0:4B:46:FF:FF:E:10:91:22.875 Where "D=" is the start of record, 2E:0:4B:46:FF:FF:E:10 is the device's 48bit address 91 is the CRC calculated and transmitted by the device 22.875 is the temperature measured by the device in C */ #define CONVERT 0x44 #define READSCRATCH 0xBE #define SKIP_ROM 0xCC #define MATCH_ROM 0x55 void setup(void) { Serial.begin(9600); } void loop(void) { byte i; byte s; byte present = 0; byte data[12]; byte addr[8]; char buff[15]; float real_temp; float temp_count; float read_temp; int sensors[]= { 13, 12, 11, 9, 8, 7, 4, 2 }; // pins that 1-wire sensors are on int num_sensors = 8; // set to the number of DS18S20P sensors attached while(1) { for (s=0; s< num_sensors; s++) { // We loop over sensors[] because we are in a 1-wire star topology OneWire ds(sensors[s]); // set the sensor to read Serial.print("Reading pin "); //just a little debug output Serial.println(sensors[s]); // if ( !ds.search(addr)) // Search for 1-wire device { ds.reset_search(); //stop the search } if ( OneWire::crc8( addr, 7) != addr[7]) // Check CRC is valid { Serial.print("CRC is not valid!\n"); } if ( addr[0] != 0x10) { // Make sure it is a DS18S20 device for ( i = 0; i < 9; i++) { // If it isn't Serial.print(addr[i], HEX); // print out the device address Serial.print(":"); } Serial.println(" is not a DS18S20 family device."); } ds.reset(); // Reset device ds.select(addr); // Select device ds.write(CONVERT,1); // Issue Convert to the device (wake up, read temp) delay(1000); // It takes at least 750ms between each sensor to read temp present = ds.reset(); // Reset device ds.select(addr); // Select device ds.write(READSCRATCH,1); // Read the Temp from the DS scratchpad Serial.print("D="); for ( i = 0; i < 9; i++) { // we need 9 bytes Serial.print(addr[i], HEX); // print out the device address Serial.print(":"); } for ( i = 0; i < 9; i++) { // Grab the 9 bytes worth of data from the sensor data[i] = ds.read(); // that we found earlier Serial.print(data[i], HEX); Serial.print(":"); } if(OneWire::crc8( data, 8) == data[8]) // Check CRC is valid { // CRC is ok read_temp=((data[1]<<8) | data[0]) >> 1 ; // Manipulate the temperature temp_count=float(data[7] - data[6])/(float)data[7]; // Convert to real temperature real_temp = ((float)read_temp-0.25)+temp_count; tempToAscii(real_temp,buff); // Convert float to ascii Serial.println(buff); // Sent it out on serial } else { //CRC faild display ------- Serial.println("CRC fail"); } } } } void tempToAscii(double temp, char *buff) { int frac; frac=(unsigned int)(temp*1000)%1000; //get three numbers to the right of the decimal point itoa((int)temp,buff,10); strcat(buff,"."); itoa(frac,&buff[strlen(buff)],10); //put the frac after the decimal }