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.hDS1621 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!