Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #define TempW 0x98 >> 1
- void setup() {
- // put your setup code here, to run once:
- Wire.begin();
- Serial.begin(9600);
- delay(100);
- Serial.println("");
- Serial.println("-----------------------------------");
- Serial.println("DS1631 test: Temp. sensor");
- Serial.println("-----------------------------------");
- Serial.println("");
- // Stop conversion to be able to modify "Access Config" Register
- Wire.beginTransmission(TempW);
- Wire.write((int)(0x22)); // Stop conversion
- Wire.endTransmission();
- Wire.beginTransmission(TempW);
- Wire.write((int)(0xAC)); // @AC : Acces Config
- Wire.endTransmission();
- Wire.requestFrom(TempW, 1); //Read 1 byte
- Wire.available();
- float AC = Wire.read(); // receive a byte
- Serial.println("Prvi read");
- Serial.print("Acces Config (Before): ");
- Serial.print(AC);
- Serial.println("");
- // WRITE into "Access Config" Register
- Wire.beginTransmission(TempW);
- Wire.write(0xAC); // @AC : Acces Config
- Wire.write(0x0C); // Continuous conversion & 12 bits resolution
- Wire.endTransmission();
- // READ "Access Config" register
- Wire.beginTransmission(TempW);
- Wire.write((int)(0xAC)); // @AC : Acces Config
- Wire.endTransmission();
- Wire.requestFrom(TempW,1);
- Wire.available();
- AC = Wire.read();
- Serial.println("Drugi vnos");
- Serial.print("Acces Config (AFTER): ");
- Serial.print(AC);
- Serial.println("");
- // START conversion to get T°
- Wire.beginTransmission(TempW);
- Wire.write((int)(0x51)); // Start Conversion
- Wire.endTransmission();
- }
- void loop() {
- //READ T°
- Wire.beginTransmission(TempW);
- Wire.write((int)(0xAA)); // @AA : Temperature
- Wire.endTransmission();
- Wire.requestFrom(TempW, 2); // READ 2 bytes
- Wire.available(); // 1st byte
- int Th = Wire.read(); // receive a byte
- Wire.available(); // 2nd byte
- int Tl = Wire.read(); // receive a byte
- // T° processing
- if(Th>=0x80) //if sign bit is set, then temp is negative
- Th = Th - 256;
- int T_dec=(10*(100*(Tl/16)))/16; // decimal part of the T°
- // Display T° on "Serial Monitor"
- Serial.print("Temperature : ");
- Serial.print(Th);
- Serial.print(".");
- if (T_dec<10) Serial.print("0");
- if (T_dec<100) Serial.print("0");
- Serial.print(T_dec);
- Serial.print(" degC / ");
- Serial.print("Th register: ");
- Serial.print(Th);
- Serial.print(" / ");
- Serial.print("Tl register: ");
- Serial.print(Tl);
- Serial.println("");
- // Wait 1s before restart
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement