-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
367 lines (253 loc) · 10.1 KB
/
Copy pathtest.cpp
File metadata and controls
367 lines (253 loc) · 10.1 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//////////////////////////////////////////////////////////////////////////
///AD7147-1 Driver by Md Shafiqur Rahman & Jacob Girgis
#include <Arduino.h> // Use the arduino functions (digitalWrite/Read, analogRead, tone, Serial, .etc)
#include <Wire.h> // used for I2C communication
#include "avr/pgmspace.h" // allows for better memory allocation
#include <stdlib.h> // standard library
#include <inttypes.h> // recognize uint8_t, uint16_t and other types
#include <time.h>
#include <stdio.h>
//REGISTERS and ADDRESSES
#define AD7147_ADDR 0x2C
#define PWR_CONTROL 0x000
#define STAGE_CAL_EN 0x001
#define AMB_COMP_CTRL0 0x002
#define AMB_COMP_CTRL1 0x003
#define AMB_COMP_CTRL2 0x004
#define STAGE_LOW_INT_ENABLE 0x005
#define STAGE_HIGH_INT_ENABLE 0x006
#define STAGE_COMPLETE_INT_ENABLE 0x007
#define STAGE_LOW_INT_STATUS 0x008
#define STAGE_HIGH_INT_STATUS 0x009
#define STAGE_COMPLETE_INT_STATUS 0x00A
#define STAGE0_CONNECTION60 0x080
#define STAGE0_CONNECTION127 0x081
#define STAGE0_AFE_OFFSET 0x082
#define STAGE1_CONNECTION60 0x088
#define STAGE1_CONNECTION127 0x089
#define STAGE1_AFE_OFFSET 0x08A
#define STAGE2_CONNECTION60 0x090
#define STAGE2_CONNECTION127 0x091
#define STAGE2_AFE_OFFSET 0x092
#define STAGE3_CONNECTION60 0x098
#define STAGE3_CONNECTION127 0x099
#define STAGE4_CONNECTION60 0x0A0
#define STAGE4_CONNECTION127 0x0A1
#define STAGE5_CONNECTION60 0x0A8
#define STAGE5_CONNECTION127 0x0A9
#define STAGE6_CONNECTION60 0x0B0
#define STAGE6_CONNECTION127 0x0B1
#define STAGE7_CONNECTION60 0x0B8
#define STAGE7_CONNECTION127 0x0B9
#define STAGE8_CONNECTION60 0x0C0
#define STAGE8_CONNECTION127 0x0C1
#define STAGE9_CONNECTION60 0x0C8
#define STAGE9_CONNECTION127 0x0C9
#define STAGE10_CONNECTION60 0x0D0
#define STAGE10_CONNECTION127 0x0D1
#define STAGE11_CONNECTION60 0x0D8
#define STAGE11_CONNECTION127 0x0D9
#define CDC_RESULT_S0 0x00B
#define CDC_RESULT_S1 0x00C
#define CDC_RESULT_S2 0x00D
// this function reads from an register and returns the 16 bit register value
uint16_t readByte(uint16_t address) {
uint16_t rx1Byte, rx2Byte, rxFinal16bitbyte;
Wire.beginTransmission(AD7147_ADDR); // Begin Transmission to the 7 bit i2c address
Wire.write((address >> 8) & 0xFF); // writes the upper byte
Wire.write((address) & 0xFF); // writes the lower byte
int i2cStatus = Wire.endTransmission(false); // i2cStatus will return 1 if there is an error.
_delay_ms(1); // delay for 1 ms just in case it takes too long.
Wire.requestFrom(AD7147_ADDR, 2); //Wire.availible will see how much data is in the buffer, only go into the loop if there is non zero amount of data
if (Wire.available()>=1) {
rx1Byte = Wire.read(); // get the upper byte (8 bits)
rx2Byte = Wire.read(); //get the lower byte (8 bits)
rxFinal16bitbyte = (rx1Byte << 8) | rx2Byte; //combined of the two 8 bits to a 16 bit
return rxFinal16bitbyte; // return final number
}
else {
// return an error code
return -1;
}
}
// this function will block write to a specific register
bool writeByte(uint16_t address, uint16_t data16bit) { // register address and data value to be sent as an argument
Wire.beginTransmission(AD7147_ADDR); // Begin Transmission to the 7 bit i2c address, so this is auomatically writing the 7 bit address?
Wire.write((address >> 8) & 0xFF); // writes the upper byte
Wire.write((address) & 0xFF); // writes the lower byte
Wire.write((data16bit >> 8) & 0xFF); // write the data value upper byte
Wire.write((data16bit) & 0xFF); // write the data value lower byte
int i2cStatus = Wire.endTransmission(); // write all the commands in the queue and close the connection
_delay_ms(1); // let it do its thing, in case it takes a long time
if (i2cStatus) // return false if i2cStatus is 1, indicating an error
return false;
else
return true;
}
void writeStage0_Connection60(){
writeByte(STAGE0_CONNECTION60,0b0011111111111110); //CIN0 Connected to CDC Positive input, all other CINx are connected to BIAS
}
void writeStage0_Connection127(){
writeByte(STAGE0_CONNECTION127,0b0001111111111111);
}
void writeStage1_Connection60(){
writeByte(STAGE1_CONNECTION60,0b0011111111111011); //CIN1 Connected to CDC Positive input, all other CINx are connected to BIAS
}
void writeStage1_Connection127(){
writeByte(STAGE1_CONNECTION127,0b0001111111111111);
}
void writeStage2_Connection60(){
writeByte(STAGE2_CONNECTION60,0b0011111111101111); //CIN2 Connected to CDC Positive input, all other CINx are connected to BIAS
}
void writeStage2_Connection127(){
writeByte(STAGE2_CONNECTION127,0b0001111111111111);
}
void writeStage3_Connection60(){
writeByte(STAGE3_CONNECTION60,0b0011111111111111); //all CINx are connected to BIAS
}
void writeStage3_Connection127(){
writeByte(STAGE3_CONNECTION127,0b1100111111111111);
}
void writeStage4_Connection60(){
writeByte(STAGE4_CONNECTION60,0b0011111111111111); //all CINx are connected to BIAS
}
void writeStage4_Connection127(){
writeByte(STAGE4_CONNECTION127,0b1100111111111111);
}
void writeStage5_Connection60(){
writeByte(STAGE5_CONNECTION60,0b0011111111111111); //all CINx are connected to BIAS
}
void writeStage5_Connection127(){
writeByte(STAGE5_CONNECTION127,0b1100111111111111);
}
void writeStage6_Connection60(){
writeByte(STAGE6_CONNECTION60,0b0011111111111111); //all CINx are connected to BIAS
}
void writeStage6_Connection127(){
writeByte(STAGE6_CONNECTION127,0b1100111111111111);
}
void writeStage7_Connection60(){
writeByte(STAGE7_CONNECTION60,0b0011111111111111); //all CINx are connected to BIAS
}
void writeStage7_Connection127(){
writeByte(STAGE7_CONNECTION127,0b1100111111111111);
}
void writeStage8_Connection60(){
writeByte(STAGE8_CONNECTION60,0b0011111111111111); //all CINx are connected to BIAS
}
void writeStage8_Connection127(){
writeByte(STAGE8_CONNECTION127,0b1100111111111111);
}
void writeStage9_Connection60(){
writeByte(STAGE9_CONNECTION60,0b0011111111111111); //all CINx are connected to BIAS
}
void writeStage9_Connection127(){
writeByte(STAGE9_CONNECTION127,0b1100111111111111);
}
void writeStage10_Connection60(){
writeByte(STAGE10_CONNECTION60,0b0011111111111111); //all CINx are connected to BIAS
}
void writeStage10_Connection127(){
writeByte(STAGE10_CONNECTION127,0b1100111111111111);
}
void writeStage11_Connection60(){
writeByte(STAGE11_CONNECTION60,0b0011111111111111); //all CINx are connected to BIAS
}
void writeStage11_Connection127(){
writeByte(STAGE11_CONNECTION127,0b1100111111111111);
}
void writeStage0_Afe_Offset(){
writeByte(STAGE0_AFE_OFFSET,0b1000010010010111);
}
void writeStage1_Afe_Offset(){
writeByte(STAGE1_AFE_OFFSET,0b1000001010010001);
}
void writeStage2_Afe_Offset(){
writeByte(STAGE2_AFE_OFFSET,0b1000001110010100);
}
void writePwr_Control(){
writeByte(PWR_CONTROL,0b0000101000100000);
}
void writeStage_Cal_En(){
writeByte(STAGE_CAL_EN,0b0000000000000111); // Calibration for STAGE 0,1,2 are enabled
}
void writeStage_Low_Int_Enable(){
writeByte(STAGE_LOW_INT_ENABLE,0b0000000000000000); // Low interrupt disabled for all the stages
}
void writeStage_Hight_Int_Enable(){
writeByte(STAGE_HIGH_INT_ENABLE,0b0000000000000000); // High interrupt disabled for all the stages
}
void writeStage_Complete_Int_Enable(){
writeByte(STAGE_COMPLETE_INT_ENABLE,0b0000000000000100); // Conversion complete interrupt after STAGE 2
}
void readStage_Complete_Int_Status(){
readByte(STAGE_COMPLETE_INT_STATUS);
}
int main(){ // this function runs immeadiately upon upload
init(); // calls some arduino intializing to allow the arduino library to be used
Serial.begin(9600); // Start USART0 (TX0 and RX0)
Wire.begin(); // Start the Wire library
writeStage0_Connection60();
writeStage0_Connection127();
writeStage0_Afe_Offset();
writeStage1_Connection60();
writeStage1_Connection127();
writeStage1_Afe_Offset();
writeStage2_Connection60();
writeStage2_Connection127();
writeStage2_Afe_Offset();
writeStage3_Connection60();
writeStage3_Connection127();
writeStage4_Connection60();
writeStage4_Connection127();
writeStage5_Connection60();
writeStage5_Connection127();
writeStage6_Connection60();
writeStage6_Connection127();
writeStage7_Connection60();
writeStage7_Connection127();
writeStage8_Connection60();
writeStage8_Connection127();
writeStage9_Connection60();
writeStage9_Connection127();
writeStage10_Connection60();
writeStage10_Connection127();
writeStage11_Connection60();
writeStage11_Connection127();
writePwr_Control();
readStage_Complete_Int_Status();
writeStage_Low_Int_Enable();
writeStage_Hight_Int_Enable();
writeStage_Complete_Int_Enable();
writeStage_Cal_En();
readStage_Complete_Int_Status();
// print what is in the power control register this will be in decimal form, so convert it later
Serial.print("PWR_CONTROL""\t");
Serial.println(readByte(PWR_CONTROL));
Serial.print("\nSTAGE_CAL_EN""\t");
Serial.println(readByte(STAGE_CAL_EN));
Serial.print("\nStage0_Connection[6:0]""\t");
Serial.println(readByte(STAGE0_CONNECTION60));
Serial.print("\nStage0_Connection[12:7]""\t");
Serial.println(readByte(STAGE0_CONNECTION127));
Serial.print("\nStage1_Connection[6:0]""\t");
Serial.println(readByte(STAGE1_CONNECTION60));
Serial.print("\nStage1_Connection[12:7]""\t");
Serial.println(readByte(STAGE1_CONNECTION127));
Serial.print("\nStage2_Connection[6:0]""\t");
Serial.println(readByte(STAGE2_CONNECTION60));
Serial.print("\nStage2_Connection[12:7]""\t");
Serial.println(readByte(STAGE2_CONNECTION127));
Serial.println(readByte(STAGE0_AFE_OFFSET));
Serial.println(readByte(STAGE1_AFE_OFFSET));
Serial.println(readByte(STAGE2_AFE_OFFSET));
while (1) {
Serial.print(readByte(CDC_RESULT_S0));
Serial.print("\t");
Serial.print(readByte(CDC_RESULT_S1));
Serial.print("\t");
Serial.print(readByte(CDC_RESULT_S2));
Serial.print("\n");
}
return(0);
}