Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.31 KB | None | 0 0
  1. uint8_t TM_DS18B20_Read(TM_OneWire_t* OneWire, uint8_t *ROM, float *destination) {
  2.     uint16_t temperature;
  3.     uint8_t resolution;
  4.     int8_t digit, minus = 0;
  5.     uint16_t decimal;
  6.     uint8_t i = 0;
  7.     uint8_t data[9];
  8.     uint8_t crc;
  9.  
  10.     /* Check if device is DS18B20 */
  11.     if (!TM_DS18B20_Is(ROM)) {
  12.         return 0;
  13.     }
  14.  
  15.     /* Check if line is released, if it is, then conversion is complete */
  16.     if (!TM_OneWire_ReadBit(OneWire)) {
  17.         /* Conversion is not finished yet */
  18.         return 0;
  19.     }
  20.  
  21.     /* Reset line */
  22.     TM_OneWire_Reset(OneWire);
  23.     /* Select ROM number */
  24.     TM_OneWire_SelectWithPointer(OneWire, ROM);
  25.     /* Read scratchpad command by onewire protocol */
  26.     TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_RSCRATCHPAD);
  27.  
  28.     /* Get data */
  29.     for (i = 0; i < 9; i++) {
  30.         /* Read byte by byte */
  31.         data[i] = TM_OneWire_ReadByte(OneWire);
  32.     }
  33.  
  34.     /* Calculate CRC */
  35.     crc = TM_OneWire_CRC8(data, 8);
  36.  
  37.     /* Check if CRC is ok */
  38.     if (crc != data[8]) {
  39.         /* CRC invalid */
  40.         return 0;
  41.     }
  42.  
  43.     /* First two bytes of scratchpad are temperature values */
  44.     temperature = data[0] | (data[1] << 8);
  45.  
  46.     /* Reset line */
  47.     TM_OneWire_Reset(OneWire);
  48.  
  49.     /* Check if temperature is negative */
  50.     if (temperature & 0x8000) {
  51.         /* Two's complement, temperature is negative */
  52.         temperature = ~temperature + 1;
  53.         minus = 1;
  54.     }
  55.  
  56.  
  57.     /* Get sensor resolution */
  58.     resolution = ((data[4] & 0x60) >> 5) + 9;
  59.  
  60.  
  61.     /* Store temperature integer digits and decimal digits */
  62.     digit = temperature >> 4;
  63.     digit |= ((temperature >> 8) & 0x7) << 4;
  64.  
  65.      /*Store decimal digits */
  66.     switch (resolution) {
  67.         case 9: {
  68. /*HERE IT GOES TO THE LOOP*/
  69.             decimal = (float)((temperature >> 3) & 0x01);
  70.             decimal = decimal * (float)DS18B20_DECIMAL_STEPS_9BIT;
  71.         } break;
  72.         case 10: {
  73.             decimal = (temperature >> 2) & 0x03;
  74.             decimal *= (float)DS18B20_DECIMAL_STEPS_10BIT;
  75.         } break;
  76.         case 11: {
  77.             decimal = (temperature >> 1) & 0x07;
  78.             decimal *= (float)DS18B20_DECIMAL_STEPS_11BIT;
  79.         } break;
  80.         case 12: {
  81.             decimal = temperature & 0x0F;
  82.             decimal *= (float)DS18B20_DECIMAL_STEPS_12BIT;
  83.         } break;
  84.         default: {
  85.             decimal = 0xFF;
  86.             digit = 0;
  87.         }
  88.     }
  89.  
  90.     /* Check for negative part */
  91.     decimal = digit + decimal;
  92.     if (minus) {
  93.         decimal = 0 - decimal;
  94.     }
  95.  
  96.     /* Set to pointer */
  97.     *destination = decimal;
  98.  
  99.     /* Return 1, temperature valid */
  100.     return 1;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement