A small library to read the tempature from ds1621(with little changes all those ds162x) devices over I2C.
ds1621.cpp
- Committer:
- nullsub
- Date:
- 2010-11-02
- Revision:
- 1:eda09b9ea6e2
- Parent:
- 0:0687b136fcab
File content as of revision 1:eda09b9ea6e2:
#include "ds1621.h" static I2C* i2c; I2C * init_tempature(I2C *interface) { i2c = interface; if (i2c == NULL) { i2c = new I2C(p28,p27); } i2c->frequency(100000);//100khz return i2c; } /*FIXME: return a float or something instead of a lousy string*/ void get_tempature(unsigned int adress, char * s) { // adress of the i2c ds1621 sensor, char buffer to contain the unsigned char TempH, TempL; int8_t data; adress &= 0x0E; data = 0xEE; i2c->write(DS1621_Write|adress,(char *)&data,sizeof(data)); data = 0xAA; i2c->write(DS1621_Write|adress,(char *)&data,sizeof(data)); i2c->read(DS1621_Read|adress,(char *)&TempH,sizeof(TempH),1); i2c->read(DS1621_Read|adress,(char *)&TempL,sizeof(TempL),0); pc.printf("temph = 0x%x, TempL = 0x%x",TempH,TempL); /*itoa(TempH,s,10); // convert to string while (*s) { s++; // goto end of string } *s++ = '.'; if (TempL == 0x80) { *s++ = '5'; } else { *s++ = '0'; } *s = 0x00; //mark endo of string*/ }