DS1621 Library as learning experience

Hi,

I'm trying to create a library for the DS1621 I2C Temperature sensor as a learning experience to teach myself how to make libraries.

So far its gone quite well, however i've got a few issues.

Even though i've got #include <../Wire/Wire.h> in DS1621.h i still have to put it in the main sketch to get it to work. why?

Also i'm having trouble with the initialisation of the chip. It seems to be hanging at the Wire.endTransmission();. i've verified this by adding a serial print out of when it passes every line. I can comment this line out and it still seems to work, but does anyone know why this might be happening?

I've used code already on the forum as a basis to get this working and verified it does work as a basic sketch.

Heres the Sketch i'm using:

#include <DS1621.h>
#include <Wire.h>

DS1621 DS1621();

void setup() {
Serial.begin(9600);
Serial.print("Start");
}

void loop() {
Serial.print("T= ");
int tempC = THERM.getTemp();
Serial.println(tempC);
delay(1000);
}

heres the DS1621.cpp:

/* DS1621 I2C Thermometer Library
DS1621.CPP V1.1
14 April 2009
*/

extern "C" {

#include <../Wire/Wire.h>
}
#include "DS1621.h"
#define DEV_ID 0x90 >> 1 // shift required by wire.h

DS1621 THERM=DS1621();
DS1621::DS1621()
{
Serial.begin(9600);
Serial.println("Start Library Init");
Wire.begin();
Serial.println("Wire.begin();");
Wire.beginTransmission(DEV_ID); // connect to DS1621 (#0)
Serial.println("Wire.beginTransmission(DEV_ID);");
Wire.send(0xAC); // Access Config
Serial.println("Wire.send(0xAC);");
Wire.send(0x02); // set for continuous conversion
Serial.println("Wire.send(0x02);");
Wire.beginTransmission(DEV_ID); // restart
Serial.println("Wire.beginTransmission(DEV_ID);");
Wire.send(0xEE); // start conversions
Serial.println("Wire.send(0xEE);");
//Wire.endTransmission();
//Serial.println("Wire.endTransmission();");
Serial.println("End Library Init");
}

int DS1621::getTemp() {
Serial.println("getTemp() Start");
int tempC = 0;

delay(1000); //Time for measurement
Wire.beginTransmission(DEV_ID);
Wire.send(0xAA); // read temperature
Wire.endTransmission();
Wire.requestFrom(DEV_ID, 1); // request one byte from DS1621
tempC = Wire.receive(); // get whole degrees reading
Serial.print("Get Temp Finished. Temp = ");
Serial.println(tempC);
return(tempC);
}

and DS1621.h:

/* DS1621 I2C Thermometer Library
DS1621.H V1.1
14 April 2009
*/

// ensure this library description is only included once
#ifndef DS1621_h
#define DS1621_h

// include types & constants of Wiring core API
#include <WProgram.h>

// include types & constants of Wire ic2 lib
#include <../Wire/Wire.h>

class DS1621
{
public:
DS1621();
int getTemp();
};
extern DS1621 THERM;
#endif

This is still in its infancy, If anyone has any tips for better code/library creation i'd be happy to learn! Thanks all!

Nice lib! :slight_smile:

I would probably remove all the debug messages, becuase they use quite a bit of RAM. I counted over 200 bytes.

Maybe make them optional by having a #define DS1621_DEBUG true; in the DS1621.h and later use

#ifdef DS1621_DEBUG
Serial.println("Debug this");
#endif

I think the line DS1621 DS1621(); will have no effect. Or maybe cause an error.

It is like saying. int digitalRead();

Yeah I put tem in just to find out where the program was stalling. They'll come out soon. I also hope to add alot more functionality to it. Thanks for the tips!

No help for your problems, but a suggestion:

Make sure you write the code so that multiple devices can be used at once. Allow the user to pass the I2C address for a particular isntance, and allow for up to 8 instances of the object at one, since there can be 8 of them on the I2C bus at the same time.

-j

Hi Harry,

I'm trying to use I2C for a different device but am finding that the program is hanging at the Wire.endTransmission(); line.

Did you resolve your issue?