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