-
Notifications
You must be signed in to change notification settings - Fork 20
/
ADS1115_4Channel.c
executable file
·153 lines (139 loc) · 3.61 KB
/
ADS1115_4Channel.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// ADS1115
// This code is designed to work with the ADS1115_I2CADC I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Analog-Digital-Converters?sku=ADS1115_I2CADC#tabs-0-product_tabset-2
#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
void main()
{
// Create I2C bus
int file;
char *bus = "/dev/i2c-1";
if ((file = open(bus, O_RDWR)) < 0)
{
printf("Failed to open the bus. \n");
exit(1);
}
// Get I2C device, ADS1115 I2C address is 0x48(72)
ioctl(file, I2C_SLAVE, 0x48);
// Select configuration register(0x01)
// AINP = AIN0 and AINN = AIN1, +/- 2.048V
// Continuous conversion mode, 128 SPS(0x84, 0x83)
char config[3] = {0};
config[0] = 0x01;
config[1] = 0x84;
config[2] = 0x83;
write(file, config, 3);
sleep(1);
// Read 2 bytes of data from register(0x00)
// raw_adc msb, raw_adc lsb
char reg[1] = {0x00};
write(file, reg, 1);
char data[2]={0};
if(read(file, data, 2) != 2)
{
printf("Error : Input/Output Error \n");
}
else
{
// Convert the data
int raw_adc = (data[0] * 256 + data[1]);
if (raw_adc > 32767)
{
raw_adc -= 65535;
}
// Output data to screen
printf("Digital Value of Analog Input on AIN0 & AIN1: %d \n", raw_adc);
}
// Select configuration register(0x01)
// AINP = AIN0 and AINN = AIN3, +/- 2.048V
// Continuous conversion mode, 128 SPS(0x84, 0x83)
char config[3] = {0};
config[0] = 0x01;
config[1] = 0x94;
config[2] = 0x83;
write(file, config, 3);
sleep(1);
// Read 2 bytes of data from register(0x00)
// raw_adc msb, raw_adc lsb
char reg[1] = {0x00};
write(file, reg, 1);
char data[2]={0};
if(read(file, data, 2) != 2)
{
printf("Error : Input/Output Error \n");
}
else
{
// Convert the data
raw_adc = (data[0] * 256 + data[1]);
if (raw_adc > 32767)
{
raw_adc -= 65535;
}
// Output data to screen
printf("Digital Value of Analog Input on AIN0 & AIN3: %d \n", raw_adc);
}
// Select configuration register(0x01)
// AINP = AIN1 and AINN = AIN3, +/- 2.048V
// Continuous conversion mode, 128 SPS(0x84, 0x83)
char config[3] = {0};
config[0] = 0x01;
config[1] = 0xA4;
config[2] = 0x83;
write(file, config, 3);
sleep(1);
// Read 2 bytes of data from register(0x00)
// raw_adc msb, raw_adc lsb
char reg[1] = {0x00};
write(file, reg, 1);
char data[2]={0};
if(read(file, data, 2) != 2)
{
printf("Error : Input/Output Error \n");
}
else
{
// Convert the data
raw_adc = (data[0] * 256 + data[1]);
if (raw_adc > 32767)
{
raw_adc -= 65535;
}
// Output data to screen
printf("Digital Value of Analog Input on AIN1 & AIN3: %d \n", raw_adc);
}
// Select configuration register(0x01)
// AINP = AIN2 and AINN = AIN3, +/- 2.048V
// Continuous conversion mode, 128 SPS(0x84, 0x83)
char config[3] = {0};
config[0] = 0x01;
config[1] = 0xB4;
config[2] = 0x83;
write(file, config, 3);
sleep(1);
// Read 2 bytes of data from register(0x00)
// raw_adc msb, raw_adc lsb
char reg[1] = {0x00};
write(file, reg, 1);
char data[2]={0};
if(read(file, data, 2) != 2)
{
printf("Error : Input/Output Error \n");
}
else
{
// Convert the data
raw_adc = (data[0] * 256 + data[1]);
if (raw_adc > 32767)
{
raw_adc -= 65535;
}
// Output data to screen
printf("Digital Value of Analog Input on AIN2 & AIN3: %d \n", raw_adc);
}
}