Hi, could some one help me to modify the code for (DS1621) for using "ports.h" library instead of "Wire.h".
what with what i must replace?
Wire.beginTransmission(SLAVEID);
Wire.send();
Wire.endTransmission();
Wire.receive();
Wire.requestFrom(SLAVEID, 1);
name.send();
name.write();
name.stop();
name.receive();
name.read();
//add this:
PortI2C myport (1 /, PortI2C::KHZ400 /);
DeviceI2C name (myport, 0x48);
here is the code:
#include <Wire.h>// DS1621 demo
// -- by Jon McPhalen (www.jonmcphalen.com)
// -- 21 DEC 2007
// SDA pin is Analog4
// SCL pin is Analog5
// DS1621 has A2, A1, and A0 pins connected to GND
// device ID and address
#define DEVTYPE 0x90 >> 1 // shift required by wire.h
#define DEVADDR 0x00 // DS1621 address is 0
#define SLAVEID DEVTYPE | DEVADDR
// DS1621 Registers & Commands
#define RDTEMP 0xAA // read temperature register
#define ACCESSTH 0xA1 // access high temperature register
#define ACCESSTL 0xA2 // access low temperature register
#define ACCESSCFG 0xAC // access configuration register
#define RDCNTR 0xA8 // read counter register
#define RDSLOPE 0xA9 // read slope register
#define STARTCNV 0xEE // start temperature conversion
#define STOPCNV 0X22 // stop temperature conversion
// DS1621 configuration bits
#define DONE B10000000 // conversion is done
#define THF B01000000 // high temp flag
#define TLF B00100000 // low temp flag
#define NVB B00010000 // non-volatile memory is busy
#define POL B00000010 // output polarity (1 = high, 0 = low)
#define ONESHOT B00000001 // 1 = one conversion; 0 = continuous conversion
void setup()
{
Wire.begin(); // connect I2C
startConversion(false); // stop if presently set to continuous
setConfig(POL | ONESHOT); // Tout = active high; 1-shot mode
setThresh(ACCESSTH, 27); // high temp threshold = 80F
setThresh(ACCESSTL, 24); // low temp threshold = 75F
Serial.begin(9600);
delay(5);
Serial.println("DS1621 Demo");
int tHthresh = getTemp(ACCESSTH);
Serial.print("High threshold = ");
Serial.println(tHthresh);
int tLthresh = getTemp(ACCESSTL);
Serial.print("Low threshold = ");
Serial.println(tLthresh);
}
void loop()
{
int tC, tFrac;
tC = getHrTemp(); // read high-resolution temperature
if (tC < 0) {
tC = -tC; // fix for integer division
Serial.print("-"); // indicate negative
}
tFrac = tC % 100; // extract fractional part
tC /= 100; // extract whole part
Serial.print(tC);
Serial.print(".");
if (tFrac < 10)
Serial.print("0");
Serial.println(tFrac);
delay(500);
}
// Set configuration register
void setConfig(byte cfg)
{
Wire.beginTransmission(SLAVEID);
Wire.send(ACCESSCFG);
Wire.send(cfg);
Wire.endTransmission();
delay(15); // allow EE write time to finish
}
// Read a DS1621 register
byte getReg(byte reg)
{
Wire.beginTransmission(SLAVEID);
Wire.send(reg); // set register to read
Wire.endTransmission();
Wire.requestFrom(SLAVEID, 1);
byte regVal = Wire.receive();
return regVal;
}
// Sets temperature threshold
// -- whole degrees C only
// -- works only with ACCESSTL and ACCESSTH
void setThresh(byte reg, int tC)
{
if (reg == ACCESSTL || reg == ACCESSTH) {
Wire.beginTransmission(SLAVEID);
Wire.send(reg); // select temperature reg
Wire.send(byte(tC)); // set threshold
Wire.send(0); // clear fractional bit
Wire.endTransmission();
delay(15);
}
}
// Start/Stop DS1621 temperature conversion
void startConversion(boolean start)
{
Wire.beginTransmission(SLAVEID);
if (start == true)
Wire.send(STARTCNV);
else
Wire.send(STOPCNV);
Wire.endTransmission();
}
// Reads temperature or threshold
// -- whole degrees C only
// -- works only with RDTEMP, ACCESSTL, and ACCESSTH
int getTemp(byte reg)
{
int tC;
if (reg == RDTEMP || reg == ACCESSTL || reg == ACCESSTH) {
byte tVal = getReg(reg);
if (tVal >= B10000000) { // negative?
tC = 0xFF00 | tVal; // extend sign bits
}
else {
tC = tVal;
}
return tC; // return threshold
}
return 0; // bad reg, return 0
}
// Read high resolution temperature
// -- returns temperature in 1/100ths degrees
// -- DS1620 must be in 1-shot mode
int getHrTemp()
{
startConversion(true); // initiate conversion
byte cfg = 0;
while (cfg < DONE) { // let it finish
cfg = getReg(ACCESSCFG);
}
int tHR = getTemp(RDTEMP); // get whole degrees reading
byte cRem = getReg(RDCNTR); // get counts remaining
byte slope = getReg(RDSLOPE); // get counts per degree
if (tHR >= 0)
tHR = (tHR 100 - 25) + ((slope - cRem) 100 / slope);
else {
tHR = -tHR;
tHR = (25 - tHR 100) + ((slope - cRem) 100 / slope);
}
return tHR;
}
