Initial fork + minor fixes for BMP085 (mBar output) + shell scripts for ADS1115 and...
[i2c-telemetry.git] / ad5602_test.c
1 /* 
2     AD5602_test1.c
3     ---------------------
4     set the output voltage from the D/A
5     -----------------------------------
6  
7       1) send the i2c address
8       2) send HI_byte
9       3) send LO_byte
10       
11         usage :-  type with spaces but without the < >
12         <AD5602_test1>  <address as decimal> <HI_byte> <LO_byte>
13 */
14
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <sys/ioctl.h>
19 #include <linux/i2c.h>
20
21 int main(int argc, char** argv)
22 {
23   int i2c; 
24   int HI_byte;   /* last 4 bits are output byte HI bits */
25   int LO_byte;   /* first 4 bits are output byte LO bits */
26   int buf[1];    /* use to feed the i2c driver */
27   int address;   /* i2c bus address */
28
29  if (argc != 4)   /* report error if we are not getting just 3 inputs after the program name */
30  {
31   printf("Error. usage (decimal): %s AD5602_test1 address HI_byte LO_byte\n", argv[0]);           
32  }
33
34   address = atoi(argv[1]); /* address is the first number after the program name */
35   // address bits are  0 0 0 1   1 1 0 R/W as per manual
36   // the 1 0  before R/W is for the address pin (pin 1) open-circuit
37   // the R/W is 0 for write. Linux see the chip address as 0000 1110 (0E) - shifts a bit to the left - odd!
38   HI_byte = atoi(argv[2]); /* HI_byte is 0 0 0 0 D7 D6 D5 D4   */
39   LO_byte = atoi(argv[3]); /* LO_byte is D3 D2 D1 D0 X X X X   (X = anything) */
40
41   i2c = open("/dev/i2c/0",O_RDWR); /* open the i2c-bus number 0 */
42
43   ioctl(i2c,I2C_SLAVE,address); /* set the i2c-bus address of the chip we will talk to */
44
45   buf[0] = HI_byte;         /* HI_byte */
46   buf[1] = LO_byte;         /* LO_byte */
47
48
49   write(i2c,buf,2); /* we send 2 bytes <HI_byte> <LO_byte> */
50
51   printf("%d\n", buf[0]); /* just to prove it ran!*/
52   printf("%d\n", buf[1]);
53
54   i2c = close(i2c);
55 }
56
57 //  AD5602_test1 14 0 0  gives 0 volts
58 //  AD5602_test1 14 8 0  gives 2.5 volts     8 = 08 00001000    0 = 00 00000000
59 //  AD5602_test1 14 15 240  gives 5 volts   15 = 0F 00001111  240 = F0 11110000
60