Advertisement
Guest User

Untitled

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