SHARE
TWEET

spark core one wire test

tidwelltimj Dec 22nd, 2013 1,286 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. //onewire.h
  4. #ifndef OneWire_h
  5. #define OneWire_h
  6.  
  7. #include <inttypes.h>
  8. // you can exclude onewire_search by defining that to 0
  9. #ifndef ONEWIRE_SEARCH
  10. #define ONEWIRE_SEARCH 1
  11. #endif
  12.  
  13. // You can exclude CRC checks altogether by defining this to 0
  14. #ifndef ONEWIRE_CRC
  15. #define ONEWIRE_CRC 1
  16. #endif
  17.  
  18.  
  19.  
  20. // You can allow 16-bit CRC checks by defining this to 1
  21. // (Note that ONEWIRE_CRC must also be 1.)
  22. #ifndef ONEWIRE_CRC16
  23. #define ONEWIRE_CRC16 1
  24. #endif
  25.  
  26. #define FALSE 0
  27. #define TRUE  1
  28.  
  29. class OneWire
  30. {
  31.   private:
  32.  
  33.         uint16_t _pin;
  34.         void DIRECT_WRITE_LOW(void);
  35.         void DIRECT_MODE_OUTPUT(void);
  36.         void DIRECT_WRITE_HIGH(void);
  37.         void DIRECT_MODE_INPUT(void);
  38.         uint8_t DIRECT_READ(void);
  39. #if ONEWIRE_SEARCH
  40.     // global search state
  41.     unsigned char ROM_NO[8];
  42.     uint8_t LastDiscrepancy;
  43.     uint8_t LastFamilyDiscrepancy;
  44.     uint8_t LastDeviceFlag;
  45. #endif
  46.  
  47.   public:
  48.     OneWire( uint16_t pin);
  49.  
  50.     // Perform a 1-Wire reset cycle. Returns 1 if a device responds
  51.     // with a presence pulse.  Returns 0 if there is no device or the
  52.     // bus is shorted or otherwise held low for more than 250uS
  53.     uint8_t reset(void);
  54.  
  55.     // Issue a 1-Wire rom select command, you do the reset first.
  56.     void select(const uint8_t rom[8]);
  57.  
  58.     // Issue a 1-Wire rom skip command, to address all on bus.
  59.     void skip(void);
  60.  
  61.     // Write a byte. If 'power' is one then the wire is held high at
  62.     // the end for parasitically powered devices. You are responsible
  63.     // for eventually depowering it by calling depower() or doing
  64.     // another read or write.
  65.     void write(uint8_t v, uint8_t power = 0);
  66.  
  67.     void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0);
  68.  
  69.     // Read a byte.
  70.     uint8_t read(void);
  71.  
  72.     void read_bytes(uint8_t *buf, uint16_t count);
  73.  
  74.     // Write a bit. The bus is always left powered at the end, see
  75.     // note in write() about that.
  76.     void write_bit(uint8_t v);
  77.  
  78.     // Read a bit.
  79.     uint8_t read_bit(void);
  80.  
  81.     // Stop forcing power onto the bus. You only need to do this if
  82.     // you used the 'power' flag to write() or used a write_bit() call
  83.     // and aren't about to do another read or write. You would rather
  84.     // not leave this powered if you don't have to, just in case
  85.     // someone shorts your bus.
  86.     void depower(void);
  87.  
  88. #if ONEWIRE_SEARCH
  89.     // Clear the search state so that if will start from the beginning again.
  90.     void reset_search();
  91.  
  92.     // Setup the search to find the device type 'family_code' on the next call
  93.     // to search(*newAddr) if it is present.
  94.     void target_search(uint8_t family_code);
  95.  
  96.     // Look for the next device. Returns 1 if a new address has been
  97.     // returned. A zero might mean that the bus is shorted, there are
  98.     // no devices, or you have already retrieved all of them.  It
  99.     // might be a good idea to check the CRC to make sure you didn't
  100.     // get garbage.  The order is deterministic. You will always get
  101.     // the same devices in the same order.
  102.     uint8_t search(uint8_t *newAddr);
  103. #endif
  104.  
  105. #if ONEWIRE_CRC
  106.     // Compute a Dallas Semiconductor 8 bit CRC, these are used in the
  107.     // ROM and scratchpad registers.
  108.     static uint8_t crc8(uint8_t *addr, uint8_t len);
  109.  
  110. #if ONEWIRE_CRC16
  111.     // Compute the 1-Wire CRC16 and compare it against the received CRC.
  112.     // Example usage (reading a DS2408):
  113.     //    // Put everything in a buffer so we can compute the CRC easily.
  114.     //    uint8_t buf[13];
  115.     //    buf[0] = 0xF0;    // Read PIO Registers
  116.     //    buf[1] = 0x88;    // LSB address
  117.     //    buf[2] = 0x00;    // MSB address
  118.     //    WriteBytes(net, buf, 3);    // Write 3 cmd bytes
  119.     //    ReadBytes(net, buf+3, 10);  // Read 6 data bytes, 2 0xFF, 2 CRC16
  120.     //    if (!CheckCRC16(buf, 11, &buf[11])) {
  121.     //        // Handle error.
  122.     //    }    
  123.     //          
  124.     // @param input - Array of bytes to checksum.
  125.     // @param len - How many bytes to use.
  126.     // @param inverted_crc - The two CRC16 bytes in the received data.
  127.     //                       This should just point into the received data,
  128.     //                       *not* at a 16-bit integer.
  129.     // @param crc - The crc starting value (optional)
  130.     // @return True, iff the CRC matches.
  131.     static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0);
  132.  
  133.     // Compute a Dallas Semiconductor 16 bit CRC.  This is required to check
  134.     // the integrity of data received from many 1-Wire devices.  Note that the
  135.     // CRC computed here is *not* what you'll get from the 1-Wire network,
  136.     // for two reasons:
  137.     //   1) The CRC is transmitted bitwise inverted.
  138.     //   2) Depending on the endian-ness of your processor, the binary
  139.     //      representation of the two-byte return value may have a different
  140.     //      byte order than the two bytes you get from 1-Wire.
  141.     // @param input - Array of bytes to checksum.
  142.     // @param len - How many bytes to use.
  143.     // @param crc - The crc starting value (optional)
  144.     // @return The CRC16, as defined by Dallas Semiconductor.
  145.     static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0);
  146. #endif
  147. #endif
  148. };
  149.  
  150. #endif
  151. OneWire::OneWire(uint16_t pin)
  152. {
  153.         pinMode(pin, INPUT);
  154.          _pin = pin;
  155.  
  156. }
  157.  
  158. void OneWire::DIRECT_WRITE_LOW(void)
  159. {
  160. PIN_MAP[_pin].gpio_peripheral->BRR = PIN_MAP[_pin].gpio_pin;
  161. }      
  162. void OneWire::DIRECT_MODE_OUTPUT(void)
  163. {
  164. GPIO_TypeDef *gpio_port = PIN_MAP[_pin].gpio_peripheral;
  165.     uint16_t gpio_pin = PIN_MAP[_pin].gpio_pin;
  166.  
  167.         GPIO_InitTypeDef GPIO_InitStructure;
  168.  
  169.         if (gpio_port == GPIOA )
  170.         {
  171.                 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  172.         }
  173.         else if (gpio_port == GPIOB )
  174.         {
  175.                 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  176.         }
  177.  
  178.     GPIO_InitStructure.GPIO_Pin = gpio_pin;
  179.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  180.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  181.     PIN_MAP[_pin].pin_mode = OUTPUT;
  182.     GPIO_Init(gpio_port, &GPIO_InitStructure);
  183. }
  184. void OneWire::DIRECT_WRITE_HIGH(void)
  185. {
  186. PIN_MAP[_pin].gpio_peripheral->BSRR = PIN_MAP[_pin].gpio_pin;
  187. }
  188. void OneWire::DIRECT_MODE_INPUT(void)
  189. {
  190.         GPIO_TypeDef *gpio_port = PIN_MAP[_pin].gpio_peripheral;
  191.     uint16_t gpio_pin = PIN_MAP[_pin].gpio_pin;
  192.  
  193.         GPIO_InitTypeDef GPIO_InitStructure;
  194.  
  195.         if (gpio_port == GPIOA )
  196.         {
  197.                 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  198.         }
  199.         else if (gpio_port == GPIOB )
  200.         {
  201.                 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  202.         }
  203.  
  204.     GPIO_InitStructure.GPIO_Pin = gpio_pin;
  205.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  206.     PIN_MAP[_pin].pin_mode = INPUT;
  207. GPIO_Init(gpio_port, &GPIO_InitStructure);
  208. }
  209. uint8_t OneWire::DIRECT_READ(void)
  210. {
  211. return GPIO_ReadInputDataBit(PIN_MAP[_pin].gpio_peripheral, PIN_MAP[_pin].gpio_pin);
  212. }
  213. // Perform the onewire reset function.  We will wait up to 250uS for
  214. // the bus to come high, if it doesn't then it is broken or shorted
  215. // and we return a 0;
  216. //
  217. // Returns 1 if a device asserted a presence pulse, 0 otherwise.
  218. //
  219.  
  220. uint8_t OneWire::reset(void)
  221. {
  222.         uint8_t r;
  223.         uint8_t retries = 125;
  224.  
  225.         noInterrupts();
  226.         DIRECT_MODE_INPUT();
  227.         interrupts();
  228.         // wait until the wire is high... just in case
  229.         do {
  230.                 if (--retries == 0) return 0;
  231.                 delayMicroseconds(2);
  232.         } while ( !DIRECT_READ());
  233.  
  234.         noInterrupts();
  235.         DIRECT_WRITE_LOW();
  236.         DIRECT_MODE_OUTPUT();   // drive output low
  237.         interrupts();
  238.         delayMicroseconds(480);
  239.         noInterrupts();
  240.         DIRECT_MODE_INPUT();    // allow it to float
  241.         delayMicroseconds(70);
  242.         r = !DIRECT_READ();
  243.         interrupts();
  244.         delayMicroseconds(410);
  245.         return r;
  246. }
  247. void OneWire::write_bit(uint8_t v)
  248. {
  249.        
  250.  
  251.         if (v & 1) {
  252.                 noInterrupts();
  253.                 DIRECT_WRITE_LOW();
  254.                 DIRECT_MODE_OUTPUT();   // drive output low
  255.                 delayMicroseconds(10);
  256.                 DIRECT_WRITE_HIGH();    // drive output high
  257.                 interrupts();
  258.                 delayMicroseconds(55);
  259.         } else {
  260.                 noInterrupts();
  261.                 DIRECT_WRITE_LOW();
  262.                 DIRECT_MODE_OUTPUT();   // drive output low
  263.                 delayMicroseconds(65);
  264.                 DIRECT_WRITE_HIGH();    // drive output high
  265.                 interrupts();
  266.                 delayMicroseconds(5);
  267.         }
  268. }
  269.  
  270. //
  271. // Read a bit. Port and bit is used to cut lookup time and provide
  272. // more certain timing.
  273. //
  274. uint8_t OneWire::read_bit(void)
  275. {
  276.        
  277.         uint8_t r;
  278.  
  279.         noInterrupts();
  280.         DIRECT_MODE_OUTPUT();
  281.         DIRECT_WRITE_LOW();
  282.         delayMicroseconds(3);
  283.         DIRECT_MODE_INPUT();    // let pin float, pull up will raise
  284.         delayMicroseconds(10);
  285.         r = DIRECT_READ();
  286.         interrupts();
  287.         delayMicroseconds(53);
  288.         return r;
  289. }
  290.  
  291. //
  292. // Write a byte. The writing code uses the active drivers to raise the
  293. // pin high, if you need power after the write (e.g. DS18S20 in
  294. // parasite power mode) then set 'power' to 1, otherwise the pin will
  295. // go tri-state at the end of the write to avoid heating in a short or
  296. // other mishap.
  297. //
  298. void OneWire::write(uint8_t v, uint8_t power /* = 0 */) {
  299.     uint8_t bitMask;
  300.  
  301.     for (bitMask = 0x01; bitMask; bitMask <<= 1) {
  302.         OneWire::write_bit( (bitMask & v)?1:0);
  303.     }
  304.     if ( !power) {
  305.         noInterrupts();
  306.         DIRECT_MODE_INPUT();
  307.         DIRECT_WRITE_LOW();
  308.         interrupts();
  309.     }
  310. }
  311.  
  312. void OneWire::write_bytes(const uint8_t *buf, uint16_t count, bool power /* = 0 */) {
  313.   for (uint16_t i = 0 ; i < count ; i++)
  314.     write(buf[i]);
  315.   if (!power) {
  316.     noInterrupts();
  317.     DIRECT_MODE_INPUT();
  318.     DIRECT_WRITE_LOW();
  319.     interrupts();
  320.   }
  321. }
  322.  
  323. //
  324. // Read a byte
  325. //
  326. uint8_t OneWire::read() {
  327.     uint8_t bitMask;
  328.     uint8_t r = 0;
  329.  
  330.     for (bitMask = 0x01; bitMask; bitMask <<= 1) {
  331.         if ( OneWire::read_bit()) r |= bitMask;
  332.     }
  333.     return r;
  334. }
  335.  
  336. void OneWire::read_bytes(uint8_t *buf, uint16_t count) {
  337.   for (uint16_t i = 0 ; i < count ; i++)
  338.     buf[i] = read();
  339. }
  340.  
  341. //
  342. // Do a ROM select
  343. //
  344. void OneWire::select(const uint8_t rom[8])
  345. {
  346.     uint8_t i;
  347.  
  348.     write(0x55);           // Choose ROM
  349.  
  350.     for (i = 0; i < 8; i++) write(rom[i]);
  351. }
  352.  
  353. //
  354. // Do a ROM skip
  355. //
  356. void OneWire::skip()
  357. {
  358.     write(0xCC);           // Skip ROM
  359. }
  360.  
  361. void OneWire::depower()
  362. {
  363.         noInterrupts();
  364.         DIRECT_MODE_INPUT();
  365.         interrupts();
  366. }
  367.  
  368. #if ONEWIRE_SEARCH
  369.  
  370. //
  371. // You need to use this function to start a search again from the beginning.
  372. // You do not need to do it for the first search, though you could.
  373. //
  374. void OneWire::reset_search()
  375. {
  376.   // reset the search state
  377.   LastDiscrepancy = 0;
  378.   LastDeviceFlag = FALSE;
  379.   LastFamilyDiscrepancy = 0;
  380.   for(int i = 7; ; i--) {
  381.     ROM_NO[i] = 0;
  382.     if ( i == 0) break;
  383.   }
  384. }
  385.  
  386. // Setup the search to find the device type 'family_code' on the next call
  387. // to search(*newAddr) if it is present.
  388. //
  389. void OneWire::target_search(uint8_t family_code)
  390. {
  391.    // set the search state to find SearchFamily type devices
  392.    ROM_NO[0] = family_code;
  393.    for (uint8_t i = 1; i < 8; i++)
  394.       ROM_NO[i] = 0;
  395.    LastDiscrepancy = 64;
  396.    LastFamilyDiscrepancy = 0;
  397.    LastDeviceFlag = FALSE;
  398. }
  399.  
  400. //
  401. // Perform a search. If this function returns a '1' then it has
  402. // enumerated the next device and you may retrieve the ROM from the
  403. // OneWire::address variable. If there are no devices, no further
  404. // devices, or something horrible happens in the middle of the
  405. // enumeration then a 0 is returned.  If a new device is found then
  406. // its address is copied to newAddr.  Use OneWire::reset_search() to
  407. // start over.
  408. //
  409. // --- Replaced by the one from the Dallas Semiconductor web site ---
  410. //--------------------------------------------------------------------------
  411. // Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
  412. // search state.
  413. // Return TRUE  : device found, ROM number in ROM_NO buffer
  414. //        FALSE : device not found, end of search
  415. //
  416. uint8_t OneWire::search(uint8_t *newAddr)
  417. {
  418.    uint8_t id_bit_number;
  419.    uint8_t last_zero, rom_byte_number, search_result;
  420.    uint8_t id_bit, cmp_id_bit;
  421.  
  422.    unsigned char rom_byte_mask, search_direction;
  423.  
  424.    // initialize for search
  425.    id_bit_number = 1;
  426.    last_zero = 0;
  427.    rom_byte_number = 0;
  428.    rom_byte_mask = 1;
  429.    search_result = 0;
  430.  
  431.    // if the last call was not the last one
  432.    if (!LastDeviceFlag)
  433.    {
  434.       // 1-Wire reset
  435.       if (!reset())
  436.       {
  437.          // reset the search
  438.          LastDiscrepancy = 0;
  439.          LastDeviceFlag = FALSE;
  440.          LastFamilyDiscrepancy = 0;
  441.          return FALSE;
  442.       }
  443.  
  444.       // issue the search command
  445.       write(0xF0);
  446.  
  447.       // loop to do the search
  448.       do
  449.       {
  450.          // read a bit and its complement
  451.          id_bit = read_bit();
  452.          cmp_id_bit = read_bit();
  453.  
  454.          // check for no devices on 1-wire
  455.          if ((id_bit == 1) && (cmp_id_bit == 1))
  456.             break;
  457.          else
  458.          {
  459.             // all devices coupled have 0 or 1
  460.             if (id_bit != cmp_id_bit)
  461.                search_direction = id_bit;  // bit write value for search
  462.             else
  463.             {
  464.                // if this discrepancy if before the Last Discrepancy
  465.                // on a previous next then pick the same as last time
  466.                if (id_bit_number < LastDiscrepancy)
  467.                   search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
  468.                else
  469.                   // if equal to last pick 1, if not then pick 0
  470.                   search_direction = (id_bit_number == LastDiscrepancy);
  471.  
  472.                // if 0 was picked then record its position in LastZero
  473.                if (search_direction == 0)
  474.                {
  475.                   last_zero = id_bit_number;
  476.  
  477.                   // check for Last discrepancy in family
  478.                   if (last_zero < 9)
  479.                      LastFamilyDiscrepancy = last_zero;
  480.                }
  481.             }
  482.  
  483.             // set or clear the bit in the ROM byte rom_byte_number
  484.             // with mask rom_byte_mask
  485.             if (search_direction == 1)
  486.               ROM_NO[rom_byte_number] |= rom_byte_mask;
  487.             else
  488.               ROM_NO[rom_byte_number] &= ~rom_byte_mask;
  489.  
  490.             // serial number search direction write bit
  491.             write_bit(search_direction);
  492.  
  493.             // increment the byte counter id_bit_number
  494.             // and shift the mask rom_byte_mask
  495.             id_bit_number++;
  496.             rom_byte_mask <<= 1;
  497.  
  498.             // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
  499.             if (rom_byte_mask == 0)
  500.             {
  501.                 rom_byte_number++;
  502.                 rom_byte_mask = 1;
  503.             }
  504.          }
  505.       }
  506.       while(rom_byte_number < 8);  // loop until through all ROM bytes 0-7
  507.  
  508.       // if the search was successful then
  509.       if (!(id_bit_number < 65))
  510.       {
  511.          // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
  512.          LastDiscrepancy = last_zero;
  513.  
  514.          // check for last device
  515.          if (LastDiscrepancy == 0)
  516.             LastDeviceFlag = TRUE;
  517.  
  518.          search_result = TRUE;
  519.       }
  520.    }
  521.  
  522.    // if no device found then reset counters so next 'search' will be like a first
  523.    if (!search_result || !ROM_NO[0])
  524.    {
  525.       LastDiscrepancy = 0;
  526.       LastDeviceFlag = FALSE;
  527.       LastFamilyDiscrepancy = 0;
  528.       search_result = FALSE;
  529.    }
  530.    for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i];
  531.    return search_result;
  532.   }
  533.  
  534. #endif
  535.  
  536. #if ONEWIRE_CRC
  537. // The 1-Wire CRC scheme is described in Maxim Application Note 27:
  538. // "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products"
  539. //
  540.  
  541.  
  542. //
  543. // Compute a Dallas Semiconductor 8 bit CRC directly.
  544. // this is much slower, but much smaller, than the lookup table.
  545. //
  546. uint8_t OneWire::crc8( uint8_t *addr, uint8_t len)
  547. {
  548.         uint8_t crc = 0;
  549.        
  550.         while (len--) {
  551.                 uint8_t inbyte = *addr++;
  552.                 for (uint8_t i = 8; i; i--) {
  553.                         uint8_t mix = (crc ^ inbyte) & 0x01;
  554.                         crc >>= 1;
  555.                         if (mix) crc ^= 0x8C;
  556.                         inbyte >>= 1;
  557.                 }
  558.         }
  559.         return crc;
  560. }
  561. #endif
  562.  
  563. #if ONEWIRE_CRC16
  564. bool OneWire::check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc)
  565. {
  566.     crc = ~crc16(input, len, crc);
  567.     return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1];
  568. }
  569.  
  570. uint16_t OneWire::crc16(const uint8_t* input, uint16_t len, uint16_t crc)
  571. {
  572.     static const uint8_t oddparity[16] =
  573.         { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
  574.  
  575.     for (uint16_t i = 0 ; i < len ; i++) {
  576.       // Even though we're just copying a byte from the input,
  577.       // we'll be doing 16-bit computation with it.
  578.       uint16_t cdata = input[i];
  579.       cdata = (cdata ^ crc) & 0xff;
  580.       crc >>= 8;
  581.  
  582.       if (oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4])
  583.           crc ^= 0xC001;
  584.  
  585.       cdata <<= 6;
  586.       crc ^= cdata;
  587.       cdata <<= 1;
  588.       crc ^= cdata;
  589.     }
  590.     return crc;
  591. }
  592. #endif
  593.  
  594.  
  595.  
  596. OneWire ds = OneWire(D3);  // on pin 10 (a 4.7K resistor is necessary)
  597.  
  598. void setup() {
  599.   Serial1.begin(9600);
  600. }
  601.  
  602. void loop() {
  603.   byte i;
  604.   byte present = 0;
  605.   byte type_s;
  606.   byte data[12];
  607.   byte addr[8];
  608.   float celsius, fahrenheit;
  609.  
  610.   if ( !ds.search(addr)) {
  611.     Serial1.println("No more addresses.");
  612.     Serial1.println();
  613.     ds.reset_search();
  614.     delay(250);
  615.     return;
  616.   }
  617.  
  618.   Serial1.print("ROM =");
  619.   for( i = 0; i < 8; i++) {
  620.     Serial1.write(' ');
  621.     Serial1.print(addr[i], HEX);
  622.   }
  623.  
  624.   if (OneWire::crc8(addr, 7) != addr[7]) {
  625.       Serial1.println("CRC is not valid!");
  626.       return;
  627.   }
  628.   Serial1.println();
  629.  
  630.   // the first ROM byte indicates which chip
  631.   switch (addr[0]) {
  632.     case 0x10:
  633.       Serial.println("  Chip = DS18S20");  // or old DS1820
  634.       type_s = 1;
  635.       break;
  636.     case 0x28:
  637.       Serial1.println("  Chip = DS18B20");
  638.       type_s = 0;
  639.       break;
  640.     case 0x22:
  641.       Serial1.println("  Chip = DS1822");
  642.       type_s = 0;
  643.       break;
  644.     default:
  645.       Serial1.println("Device is not a DS18x20 family device.");
  646.       return;
  647.   }
  648.  
  649.   ds.reset();
  650.   ds.select(addr);
  651.   ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  652.  
  653.   delay(1000);     // maybe 750ms is enough, maybe not
  654.   // we might do a ds.depower() here, but the reset will take care of it.
  655.  
  656.   present = ds.reset();
  657.   ds.select(addr);    
  658.   ds.write(0xBE);         // Read Scratchpad
  659.  
  660.   Serial1.print("  Data = ");
  661.   Serial1.print(present, HEX);
  662.   Serial1.print(" ");
  663.   for ( i = 0; i < 9; i++) {           // we need 9 bytes
  664.     data[i] = ds.read();
  665.     Serial1.print(data[i], HEX);
  666.     Serial1.print(" ");
  667.   }
  668.   Serial1.print(" CRC=");
  669.   Serial1.print(OneWire::crc8(data, 8), HEX);
  670.   Serial1.println();
  671.  
  672.   // Convert the data to actual temperature
  673.   // because the result is a 16 bit signed integer, it should
  674.   // be stored to an "int16_t" type, which is always 16 bits
  675.   // even when compiled on a 32 bit processor.
  676.   int16_t raw = (data[1] << 8) | data[0];
  677.   if (type_s) {
  678.     raw = raw << 3; // 9 bit resolution default
  679.     if (data[7] == 0x10) {
  680.       // "count remain" gives full 12 bit resolution
  681.       raw = (raw & 0xFFF0) + 12 - data[6];
  682.     }
  683.   } else {
  684.     byte cfg = (data[4] & 0x60);
  685.     // at lower res, the low bits are undefined, so let's zero them
  686.     if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
  687.     else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  688.     else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  689.     //// default is 12 bit resolution, 750 ms conversion time
  690.   }
  691.   celsius = (float)raw / 16.0;
  692.   fahrenheit = celsius * 1.8 + 32.0;
  693.   Serial1.print("  Temperature = ");
  694.   Serial1.print(celsius);
  695.   Serial1.print(" Celsius, ");
  696.   Serial1.print(fahrenheit);
  697.   Serial1.println(" Fahrenheit");
  698. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top