Hardware Reference
In-Depth Information
Listing 12-13. DS1631 I2C main sketch DS1631Example.ino
/*
* DS1631_CPP Library Example
*/
#include <Wire.h>
#include "DS1631.h"
uint8_t conf = 0x0C;
uint8_t dev1 = DEV0;
DS1631 TempSensor(dev1); //Wire.begin hasn't happened yet
void setup()
{
Serial.begin(9600);
Wire.begin();
TempSensor.stopConversion();
TempSensor.setConfig(conf);
byte config = TempSensor.getConfig();
Serial.print("Config: dev:");
Serial.print(DEV0, BIN);
Serial.print(" set: ");
Serial.print(config, BIN);
Serial.print(conf, BIN);
Serial.print(" get: ");
Serial.println(config, BIN);
}
void loop()
{
float temp = TempSensor.getTemp();
Serial.print("TempC: ");
Serial.print(temp, 4);
Serial.print(" tempF: ");
Serial.println((temp*9/5) + 32, 4);
}
One key point is that Wire.begin() must be initiated for any I2C communication to occur. This explains why
Wire.begin() is established early in the setup() code, before TempSensor.setConfig(conf) is called. The Serial
library can print float values so the temperature returned as float would be printed automatically with two decimal
points, but because we have more detail, the code specifies four decimal places.
Lastly, it is possible to have up to eight DS1631 temperature sensors on one Arduino. In this version of the library,
the sketch would contain an array of sensors each configured with their own address, as follows:
DS1631 TmpSense[8] = {
DS1631(DEV0),
DS1631(DEV1),
DS1631(DEV2),
DS1631(DEV3),
 
Search WWH ::




Custom Search