package stamp.peripheral.io; import stamp.core.*; import stamp.peripheral.io.I2C; /* * I2C Temperature Sensor. * * This class manipulates a Dallas DS1621 digital thermometer and * thermostat IC * * DS1621(I2C, Adr, OpMode) constructor method I2C is pointer to an I2C object * Adr is the I2C device address (read) and OpMode 1 for One shot temp reading * 0 for continuous temp reading * gettemp() returns temp in C * startconv() & stopconv() starts and stops temp conversion within the IC thus * lowering its power consumption * set1shot(boolean) sets one shot mode * setTH(int) & setTL(int) set thermostat trigger and reset temps the Tout pin * is configured to be active high in this class * * @version 1.0 27 August 2004 * @author Steven Tron */ public class DS1621 { final static int RTemp = 0xAA; final static int TH = 0xA1; final static int TL = 0xA2; final static int Conf = 0xAC; final static int SConv = 0xEE; final static int EConv = 0x22; final static int Nak = 0x1; final static int Ack = 0x0; protected I2C ioBus; protected int WAddr; protected int RAddr; protected boolean Mode; protected StringBuffer tempval = new StringBuffer(5); public DS1621(I2C bus, int Adr, boolean OpMode) { this.ioBus = bus; this.WAddr = Adr++; this.RAddr = Adr; this.Mode = OpMode; this.set1shot(OpMode); } private void setreg(int reg) { ioBus.start(); ioBus.write(WAddr); ioBus.write(reg); } public void startconv() { setreg(SConv); ioBus.stop(); } public void stopconv() { setreg(EConv); ioBus.stop(); } public void set1shot(boolean tmode) { setreg(Conf); if (tmode) { ioBus.write(3); Mode = true; } else { ioBus.write(2); Mode = false; startconv(); } ioBus.stop(); } public void setTH(int THVal) { setreg(TH); ioBus.write(THVal); ioBus.write(0); ioBus.stop(); } public void setTL(int TLVal) { setreg(TL); ioBus.write(TLVal); ioBus.write(0); ioBus.stop(); } public String gettemp() { int LTemp; if (Mode) { startconv(); } tempval.clear(); setreg(RTemp); ioBus.start(); ioBus.write(RAddr); tempval.append(ioBus.read(Ack)); LTemp = ioBus.read(Nak); if (LTemp > 0) { tempval.append(".5"); } else { tempval.append(".0"); } tempval.append("C"); ioBus.stop(); if (Mode) { stopconv(); } return tempval.toString(); } }