Imported Upstream version 2.9.0
[debian/cc1111] / device / examples / ds390 / i2c390 / ds1621.c
1 #include <tinibios.h>
2
3 #include "ds1621.h"
4
5 float ReadDS1621(char address) {
6   float temperature;
7   signed char counter, slope;
8
9   int id=DS1621_ID + (address<<1);
10   
11   while (!I2CReset()) {
12     //fprintf (stderr, "I2C bus busy, retrying.\n");
13   }
14   
15   i2cTransmitBuffer[0]=0xac; // access config command
16   i2cTransmitBuffer[1]=0x09; // mode (8=continuous, 9=one-shot)
17   if (I2CSend(id, 2)) return -999;
18   
19   i2cTransmitBuffer[0]=0xee; // start conversion command
20   if (I2CSend(id, 1)) return -999;
21     
22   do {
23     i2cTransmitBuffer[0]=0xac; // access config command
24     if (I2CSendReceive(id, 1, 1)) return -999;
25   } while ((i2cReceiveBuffer[0]&0x80)==0); // wait for conversion done
26
27   i2cTransmitBuffer[0]=0xaa; // read temperature command
28
29   if (I2CSendReceive(id, 1, 1)) return -999;
30   temperature=i2cReceiveBuffer[0];
31   i2cTransmitBuffer[0]=0xa8; // read counter command
32   if (I2CSendReceive(id, 1, 1)) return -999;
33   counter=i2cReceiveBuffer[0];
34   
35   i2cTransmitBuffer[0]=0xa9; // read slope command
36   if (I2CSendReceive(id, 1, 1)) return -999;
37   slope=i2cReceiveBuffer[0];
38   
39   temperature=temperature - 0.25 +
40     ((float)slope-(float)counter)/(float)slope;
41   return temperature;
42 }