fd99ef7b1ee1f4ee445755aec554046a9d904623
[fw/sdcc] / device / examples / ds390 / i2c390 / ds1621.c
1 #include "i2clole.h"
2 #include "ds1621.h"
3
4 #if USE_FLOAT // defined in ds1621.h
5 static float temperature
6 signed char counter, slope;
7 float
8 #else
9 #include <stdio.h>
10 static char temperature[10];
11 char *
12 #endif
13
14 ReadDS1621(char address) {
15   int id=DS1621_ID + (address<<1);
16   
17   while (!I2CReset()) {
18     //fprintf (stderr, "I2C bus busy, retrying.\n");
19   }
20   
21   i2cTransmitBuffer[0]=0xac; // access config command
22   i2cTransmitBuffer[1]=0x09; // mode (8=continuous, 9=one-shot)
23   if (I2CSend(id, 2)) return -999;
24   
25   i2cTransmitBuffer[0]=0xee; // start conversion command
26   if (I2CSend(id, 1)) return -999;
27     
28   do {
29     i2cTransmitBuffer[0]=0xac; // access config command
30     if (I2CSendReceive(id, 1, 1)) return -999;
31   } while ((i2cReceiveBuffer[0]&0x80)==0); // wait for conversion done
32
33
34   // if we did not do this, sdcc thinks i2cReceiveBuffer[0] is still in r3
35   i2cReceiveBuffer[0]=0;
36
37   i2cTransmitBuffer[0]=0xaa; // read temperature command
38
39 #if !USE_FLOAT
40   if (I2CSendReceive(id, 1, 2)) return -999;
41   sprintf (temperature, "% 3bd.%c", i2cReceiveBuffer[0], 
42            i2cReceiveBuffer[1]?'5':'0');
43   return temperature;
44 #else
45   if (I2CSendReceive(id, 1, 1)) return -999;
46   temperature=i2cReceiveBuffer[0];
47   i2cTransmitBuffer[0]=0xa8; // read counter command
48   if (I2CSendReceive(id, 1, 1)) return -999;
49   counter=i2cReceiveBuffer[0];
50   
51   i2cTransmitBuffer[0]=0xa9; // read slope command
52   if (I2CSendReceive(id, 1, 1)) return -999;
53   slope=i2cReceiveBuffer[0];
54     
55   temperature=(int)temperature - 0.25 +
56     ((float)slope-(float)counter)/(float)slope;
57     return temperature;
58 #endif
59 }
60