Advertisement
Guest User

temp

a guest
Jan 23rd, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #define TempW 0x98 >> 1
  3.  
  4. void setup() {
  5.   // put your setup code here, to run once:
  6.   Wire.begin();
  7.   Serial.begin(9600);
  8.   delay(100);
  9.   Serial.println("");
  10.   Serial.println("-----------------------------------");
  11.   Serial.println("DS1631 test: Temp. sensor");
  12.   Serial.println("-----------------------------------");
  13.   Serial.println("");
  14.   // Stop conversion to be able to modify "Access Config" Register
  15.   Wire.beginTransmission(TempW);
  16.   Wire.write((int)(0x22)); // Stop conversion
  17.   Wire.endTransmission();
  18.    Wire.beginTransmission(TempW);
  19.   Wire.write((int)(0xAC)); // @AC : Acces Config
  20.   Wire.endTransmission();
  21.   Wire.requestFrom(TempW, 1); //Read 1 byte
  22.   Wire.available();
  23.   float AC = Wire.read(); // receive a byte
  24.   Serial.println("Prvi read");
  25.   Serial.print("Acces Config (Before): ");
  26.   Serial.print(AC);  
  27.   Serial.println("");
  28.   // WRITE into "Access Config" Register
  29.   Wire.beginTransmission(TempW);
  30.   Wire.write(0xAC); // @AC : Acces Config
  31.   Wire.write(0x0C); // Continuous conversion & 12 bits resolution
  32.   Wire.endTransmission();
  33.   // READ "Access Config" register
  34.   Wire.beginTransmission(TempW);
  35.   Wire.write((int)(0xAC)); // @AC : Acces Config
  36.   Wire.endTransmission();
  37.   Wire.requestFrom(TempW,1);
  38.   Wire.available();
  39.   AC = Wire.read();
  40.   Serial.println("Drugi vnos");
  41.   Serial.print("Acces Config (AFTER): ");
  42.   Serial.print(AC);  
  43.   Serial.println("");
  44.   // START conversion to get T°
  45.   Wire.beginTransmission(TempW);
  46.   Wire.write((int)(0x51)); // Start Conversion
  47.   Wire.endTransmission();
  48. }
  49.  
  50. void loop() {
  51.   //READ T°
  52.   Wire.beginTransmission(TempW);
  53.   Wire.write((int)(0xAA)); // @AA : Temperature
  54.   Wire.endTransmission();
  55.   Wire.requestFrom(TempW, 2); // READ 2 bytes
  56.   Wire.available(); // 1st byte
  57.   int Th = Wire.read(); // receive a byte
  58.   Wire.available(); // 2nd byte
  59.   int Tl = Wire.read(); // receive a byte
  60.   // T° processing
  61.   if(Th>=0x80) //if sign bit is set, then temp is negative
  62.   Th = Th - 256;
  63.   int T_dec=(10*(100*(Tl/16)))/16; // decimal part of the T°
  64.   // Display T° on "Serial Monitor"
  65.   Serial.print("Temperature : ");
  66.   Serial.print(Th);  
  67.   Serial.print(".");
  68.   if (T_dec<10)   Serial.print("0");
  69.   if (T_dec<100)   Serial.print("0");
  70.   Serial.print(T_dec);  
  71.   Serial.print(" degC / ");
  72.   Serial.print("Th register: ");
  73.   Serial.print(Th);  
  74.   Serial.print(" / ");
  75.   Serial.print("Tl register: ");
  76.   Serial.print(Tl);  
  77.   Serial.println("");
  78.   // Wait 1s before restart
  79.   delay(1000);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement