Advertisement
Guest User

Untitled

a guest
May 5th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <avr/wdt.h>
  4.  
  5. const unsigned char ADDR = 0b1001000;
  6. unsigned char integer = 0;
  7.  
  8. void read_temp(int address);
  9.  
  10. unsigned char lowValue = 0, highValue = 0;
  11.  
  12. void setup()
  13. {
  14.   // put your setup code here, to run once:
  15.   wdt_enable(WDTO_2S);
  16.   Serial.begin(9600);
  17.   Wire.begin();
  18.   delay(1000);
  19. }
  20.  
  21. void loop()
  22. {
  23.   wdt_reset();
  24.   read_temp(ADDR);
  25.  
  26.   // Serial.print(F("Raw value: ")); Serial.print(highValue); Serial.print(F(" ")); Serial.println(lowValue);
  27.   if (highValue & 0b10000000)
  28.   {
  29.     Serial.print(F("-"));
  30.     integer = highValue & 0b01111111;
  31.     integer = highValue ^ 0xFF;
  32.     integer += 1;
  33.   }
  34.   else
  35.   {
  36.     integer = highValue;
  37.   }
  38.  
  39.   Serial.print(integer);
  40.   if (lowValue & 0b10000000)
  41.   {
  42.     Serial.print(F(".5"));
  43.   }
  44.   Serial.println("");
  45.   for (unsigned char idx = 0; idx < 10; ++idx)
  46.   {
  47.     wdt_reset();
  48.     delay(1000);
  49.   }
  50.   Serial.flush();
  51. }
  52.  
  53. void read_temp(int address)
  54. {
  55.   //start the communication with IC with the address xx
  56.   Wire.beginTransmission(address);
  57.   //send a bit and ask for register zero
  58.   Wire.write(0);
  59.   //end transmission
  60.   Wire.endTransmission();
  61.   //request 1 byte from address xx
  62.   Wire.requestFrom(address, 2);
  63.   //wait for response
  64.   while (Wire.available() == 0);
  65.   //put the temperature in variable c
  66.   highValue = Wire.read();
  67.   lowValue = Wire.read();
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement