ads1115 analog-to-digital 4-channel added
[i2c-telemetry.git] / ds1621.c
1 /* Test DS1621 sur interface I2C GPIO Fonera (3/2008)                                   */
2 /* Inspir de "Un bus i2c pour La Fonera"                                                */
3 /* http://www.lefinnois.net/wp/index.php/2007/05/05/un-bus-i2c-pour-la-fonera/          */
4
5 /* Affichage de la t                                                                    */
6 /* Par domos domos78<at>free<point>fr                                                   */
7 /* http://vesta.homelinux.net/                                                          */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <sys/ioctl.h>
16 #include <linux/i2c-dev.h>
17 #include <linux/i2c.h>
18
19 #define I2C_SLAVE       0x0703          // Change slave address
20
21 static int i2c_fd ;
22 int res ;
23
24 #define DEVICE "/dev/i2c-0"
25 #define ds1621_addr 0x9E >> 1           // 0x4f - Address (A0+A1+A2 to Vcc)
26
27
28 /*------------------------------------------------------------------------------*/
29 /* Fonctions i2c                                                                */
30 /*------------------------------------------------------------------------------*/
31 void i2c_init()
32 {
33         if ((i2c_fd = open(DEVICE, O_RDWR)) < 0) 
34         {
35                 fprintf(stderr, "Erreur ouverture port: %s (%d)\n", strerror(errno), errno);
36                 exit(EXIT_FAILURE);
37         }
38 }
39
40 //-----------------------------------------------------------------------------
41 void SelectSlave(unsigned char slaveaddr)
42 {
43         if (ioctl(i2c_fd, I2C_SLAVE, slaveaddr) < 0) 
44         {
45                 fprintf(stderr, "Erreur selection esclave i2c: %s (%d)\n", strerror(errno), errno);
46                 close ( i2c_fd ) ;
47                 exit(EXIT_FAILURE) ;
48         } 
49 }
50
51 /*------------------------------------------------------------------------------*/
52 /* Fonctions DS1621                                                             */
53 /*------------------------------------------------------------------------------*/
54 int i2c_init_ds1621(unsigned char addr)
55 {
56         int res ;
57         char buff[10] ;
58         
59         SelectSlave(addr) ;
60         buff[0] = 0xAC ;
61         buff[1] = 0x01 ;
62         res = write( i2c_fd, buff, 2 );
63         if ( res < 0 )
64         {
65                 printf("Erreur init DS1621 addr 0x%x: '%s', Abandon programme !", addr, strerror(errno));
66                 close ( i2c_fd );
67                 exit(1) ;
68         }
69         /* start temperature conversion */
70         errno = 0 ;     
71         buff[0] = 0xEE ;
72         write( i2c_fd, buff, 1 );
73         sleep(1) ;
74         return 1 ;
75 }
76
77 //-----------------------------------------------------------------------------
78 double i2c_gettemp_ds1621(unsigned char addr)
79 {
80         int k, count, slope, temp;
81         char buff[10] ;
82         
83         SelectSlave(addr) ;
84         /* stop conversion */
85         errno = 0 ;     
86         buff[0] = 0x22 ;
87
88         if ( write( i2c_fd, buff, 1 ) < 0 ) return 255 ; // Write retourne -1 et strerror(errno))='Remote I/O error' si adr. i2c non connecte.
89         else
90         {
91                 /* Temperature reading (1 Celsius degree precision) */
92                 buff[0] = 0xAA ;
93                 write( i2c_fd, buff, 1 );
94                 read(i2c_fd, buff, 1) ;
95                 temp = buff[0] ;
96                 /* Counter reading (fraction of degree) ) */
97                 buff[0] = 0xA8 ;
98                 write( i2c_fd, buff, 1 );
99                 read(i2c_fd, buff, 1) ;
100                 count = buff[0] ;
101                 /* slope reading */
102                 buff[0] = 0xA9 ;
103                 write( i2c_fd, buff, 1 );
104                 read(i2c_fd, buff, 1) ;
105                 slope = buff[0] ;
106                 k = temp;
107                 if (slope != 0) 
108                 {
109                         k = temp*100-25+(100*(slope-count))/slope;
110                 }
111                 /* start temperature conversion */
112                 buff[0] = 0xEE ;
113                 write( i2c_fd, buff, 1 );
114                 return (float)k/100 ;
115         }
116 }
117
118 /*------------------------------------------------------------------------------*/
119 int main ( int argc, char ** argv )
120 {
121         // Init i2c.
122         i2c_init() ;
123
124         // Init ds1621.
125         i2c_init_ds1621(ds1621_addr) ;
126
127         // Affiche la t.
128         printf("%2.1f\n", i2c_gettemp_ds1621(ds1621_addr) ) ;
129
130         close ( i2c_fd );
131 }
132
133 /*------------------------------------------------------------------------------*/
134