-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathADS1115_sample.c.html
More file actions
executable file
·126 lines (122 loc) · 11.6 KB
/
ADS1115_sample.c.html
File metadata and controls
executable file
·126 lines (122 loc) · 11.6 KB
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>C:\Users\David\Documents\CAMBRIDGE\YEAR3\Y3_Summerwork\CODE\(5)\ADS1115_sample.c</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="generator" content="Geany 1.23.1" />
<meta name="date" content="2013-09-09T22:08:00" />
<style type="text/css">
body
{
font-family: DejaVu Sans Mono, monospace;
font-size: 10pt;
}
.style_0
{
color: #000000;
background-color: #ffffff;
}
.style_2
{
color: #d00000;
background-color: #ffffff;
}
.style_4
{
color: #007f00;
background-color: #ffffff;
}
.style_5
{
color: #00007f;
background-color: #ffffff;
font-weight: bold;
}
.style_6
{
color: #ff8000;
background-color: #ffffff;
}
.style_9
{
color: #007f7f;
background-color: #ffffff;
}
.style_10
{
color: #301010;
background-color: #ffffff;
}
.style_11
{
color: #000000;
background-color: #ffffff;
}
.style_16
{
color: #991111;
background-color: #ffffff;
font-weight: bold;
}
</style>
</head>
<body>
<p>
<span class="style_9">#include </span><span class="style_6"><stdio.h></span><br />
<span class="style_9">#include </span><span class="style_6"><fcntl.h></span><span class="style_11"> </span><span class="style_2">// open</span><br />
<span class="style_9">#include </span><span class="style_6"><inttypes.h></span><span class="style_11"> </span><span class="style_2">// uint8_t, etc</span><br />
<span class="style_9">#include </span><span class="style_6"><linux/i2c-dev.h> </span><span class="style_2">// I2C bus definitions</span><br />
<br />
<span class="style_5">int </span><span class="style_11">main</span><span class="style_10">() {</span><br />
<br />
<span class="style_5">int </span><span class="style_11">ADS_address </span><span class="style_10">= </span><span class="style_4">0x48</span><span class="style_10">; </span><span class="style_2">// Address of our device on the I2C bus</span><br />
<span class="style_5">int </span><span class="style_11">I2CFile</span><span class="style_10">;</span><br />
<br />
<span class="style_16">uint8_t </span><span class="style_11">writeBuf</span><span class="style_10">[</span><span class="style_4">3</span><span class="style_10">]; </span><span class="style_2">// Buffer to store the 3 bytes that we write to the I2C device</span><br />
<span class="style_16">uint8_t </span><span class="style_11">readBuf</span><span class="style_10">[</span><span class="style_4">2</span><span class="style_10">]; </span><span class="style_2">// 2 byte buffer to store the data read from the I2C device</span><br />
<br />
<span class="style_16">int16_t </span><span class="style_11">val</span><span class="style_10">; </span><span class="style_2">// Stores the 16 bit value of our ADC conversion</span><br />
<br />
<span class="style_11">I2CFile </span><span class="style_10">= </span><span class="style_11">open</span><span class="style_10">(</span><span class="style_6">"/dev/i2c-1"</span><span class="style_10">, </span><span class="style_11">O_RDWR</span><span class="style_10">); </span><span class="style_2">// Open the I2C device</span><br />
<br />
<span class="style_11">ioctl</span><span class="style_10">(</span><span class="style_11">I2CFile</span><span class="style_10">, </span><span class="style_11">I2C_SLAVE</span><span class="style_10">, </span><span class="style_11">ADS_address</span><span class="style_10">); </span><span class="style_2">// Specify the address of the I2C Slave to communicate with</span><br />
<br />
<span class="style_2">// These three bytes are written to the ADS1115 to set the config register and start a conversion </span><br />
<span class="style_11">writeBuf</span><span class="style_10">[</span><span class="style_4">0</span><span class="style_10">] = </span><span class="style_4">1</span><span class="style_10">; </span><span class="style_2">// This sets the pointer register so that the following two bytes write to the config register</span><br />
<span class="style_11">writeBuf</span><span class="style_10">[</span><span class="style_4">1</span><span class="style_10">] = </span><span class="style_4">0xC3</span><span class="style_10">; </span><span class="style_2">// This sets the 8 MSBs of the config register (bits 15-8) to 11000011</span><br />
<span class="style_11">writeBuf</span><span class="style_10">[</span><span class="style_4">2</span><span class="style_10">] = </span><span class="style_4">0x03</span><span class="style_10">; </span><span class="style_2">// This sets the 8 LSBs of the config register (bits 7-0) to 00000011</span><br />
<br />
<span class="style_2">// Initialize the buffer used to read data from the ADS1115 to 0</span><br />
<span class="style_11">readBuf</span><span class="style_10">[</span><span class="style_4">0</span><span class="style_10">]= </span><span class="style_4">0</span><span class="style_10">; </span><br />
<span class="style_11">readBuf</span><span class="style_10">[</span><span class="style_4">1</span><span class="style_10">]= </span><span class="style_4">0</span><span class="style_10">;</span><br />
<br />
<span class="style_2">// Write writeBuf to the ADS1115, the 3 specifies the number of bytes we are writing,</span><br />
<span class="style_2">// this begins a single conversion</span><br />
<span class="style_11">write</span><span class="style_10">(</span><span class="style_11">I2CFile</span><span class="style_10">, </span><span class="style_11">writeBuf</span><span class="style_10">, </span><span class="style_4">3</span><span class="style_10">); </span><br />
<br />
<span class="style_2">// Wait for the conversion to complete, this requires bit 15 to change from 0->1</span><br />
<span class="style_5">while </span><span class="style_10">((</span><span class="style_11">readBuf</span><span class="style_10">[</span><span class="style_4">0</span><span class="style_10">] & </span><span class="style_4">0x80</span><span class="style_10">) == </span><span class="style_4">0</span><span class="style_10">) </span><span class="style_2">// readBuf[0] contains 8 MSBs of config register, AND with 10000000 to select bit 15</span><br />
<span class="style_10">{</span><br />
<span class="style_11">read</span><span class="style_10">(</span><span class="style_11">I2CFile</span><span class="style_10">, </span><span class="style_11">readBuf</span><span class="style_10">, </span><span class="style_4">2</span><span class="style_10">); </span><span class="style_2">// Read the config register into readBuf</span><br />
<span class="style_10">}</span><br />
<br />
<span class="style_11">writeBuf</span><span class="style_10">[</span><span class="style_4">0</span><span class="style_10">] = </span><span class="style_4">0</span><span class="style_10">; </span><span class="style_2">// Set pointer register to 0 to read from the conversion register</span><br />
<span class="style_11">write</span><span class="style_10">(</span><span class="style_11">I2CFile</span><span class="style_10">, </span><span class="style_11">writeBuf</span><span class="style_10">, </span><span class="style_4">1</span><span class="style_10">);</span><br />
<br />
<span class="style_11">read</span><span class="style_10">(</span><span class="style_11">I2CFile</span><span class="style_10">, </span><span class="style_11">readBuf</span><span class="style_10">, </span><span class="style_4">2</span><span class="style_10">); </span><span class="style_2">// Read the contents of the conversion register into readBuf</span><br />
<br />
<span class="style_11">val </span><span class="style_10">= </span><span class="style_11">readBuf</span><span class="style_10">[</span><span class="style_4">0</span><span class="style_10">] << </span><span class="style_4">8 </span><span class="style_10">| </span><span class="style_11">readBuf</span><span class="style_10">[</span><span class="style_4">1</span><span class="style_10">]; </span><span class="style_2">// Combine the two bytes of readBuf into a single 16 bit result </span><br />
<br />
<span class="style_11">printf</span><span class="style_10">(</span><span class="style_6">"Voltage Reading %f (V) \n"</span><span class="style_10">, (</span><span class="style_5">float</span><span class="style_10">)</span><span class="style_11">val</span><span class="style_10">*</span><span class="style_4">4.096</span><span class="style_10">/</span><span class="style_4">32767.0</span><span class="style_10">); </span><span class="style_2">// Print the result to terminal, first convert from binary value to mV</span><br />
<br />
<span class="style_11">close</span><span class="style_10">(</span><span class="style_11">I2CFile</span><span class="style_10">);</span><br />
<br />
<span class="style_5">return </span><span class="style_4">0</span><span class="style_10">;</span><br />
<br />
<span class="style_10">}</span><br />
<br />
<br />
</p>
</body>
</html>