-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathltc2309.c
116 lines (95 loc) · 3 KB
/
ltc2309.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* http://elinux.org/Interfacing_with_I2C_Devices
* https://www.kernel.org/doc/Documentation/i2c/dev-interface
* http://blog.chrysocome.net/2013/03/programming-i2c.html
*
*/
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define ADCTF 0.001 // 4.096v / 2^12
/************************************************************
* Read LTC2309 ADC
*
* To Start a conversion, write control word to select with channel
* then small pause for conversion time, then read back value
************************************************************/
float ReadADCVoltage(int dev, int channel)
{
char buf[3] = {0};
int bytesWritten, bytesRead;
int voltage, cntrlword;
switch (channel) {
case 0 : cntrlword = 0x88; break;
case 2 : cntrlword = 0x98; break;
case 4 : cntrlword = 0xA8; break;
case 6 : cntrlword = 0xB8; break;
case 1 : cntrlword = 0xC8; break;
case 3 : cntrlword = 0xD8; break;
case 5 : cntrlword = 0xE8; break;
case 7 : cntrlword = 0xF8; break;
}
buf[0] = cntrlword;
bytesWritten = write(dev,buf,1);
printf("Bytes written: %i\n",bytesWritten);
usleep(10000);
bytesRead = read(dev,buf,2);
printf("Bytes read: %i\n",bytesRead);
printf("DAC Read : BytesRead:%d Byte1Val: %x Byte2Val: %x\n",bytesRead,buf[0],buf[1]);
voltage = (((buf[0] << 8) | buf[1]) >> 4);
printf("Voltage Readback: %f\n",voltage*ADCTF);
return (float) voltage;
}
int main(int argc, char *argv[])
{
int dev;
char filename[40], buf[10];
int addr;
int addr1 = 0b00001000; // 1st ADC i2c address
int addr2 = 0b00001010; // 2nd ADC i2c address
int i;
float rdback, voltage;
int boardNum, channelNum;
if (argc != 2) {
printf("Usage: %s channel(0:7) \n", argv[0]);
printf("Ch0 : Temperature 1\nCh1 : Temperature 4\nCh2 : Temperature 3\nCh3 : Temperature 1\n");
printf("Ch4 : Bias Voltage\nCh5 : Bias Current\nCh6 : Peltier1 Current\nCh7 : Peltier2 Current\n");
printf("Ch8 : Detector Leakage\nCh9 : Peltier Voltage\n");
exit(1);
}
channelNum = atoi(argv[1]);
printf("Reading ADC: ch: %d \n",channelNum);
sprintf(filename,"/dev/i2c-0");
if ((dev = open(filename,O_RDWR)) < 0) {
printf("Failed to open the bus.");
exit(1);
}
if (channelNum < 8)
addr = addr1;
else if(channelNum<16){
addr = addr2;
channelNum = channelNum - 8;
}
else {
printf("Bad channel number\n");
exit(1);
}
printf("Address=%x\n",addr);
if (ioctl(dev,I2C_SLAVE,addr) < 0) {
printf("Failed to acquire bus access and/or talk to slave.\n");
exit(1);
}
// Enable Internal Reference
//EnableIntRef(dev);
//SetDacVoltage(dev,channelNum,voltage);
rdback = ReadADCVoltage(dev,channelNum);
printf("\n\n");
return 0;
}