-
Notifications
You must be signed in to change notification settings - Fork 1
/
ADS1230.c
416 lines (398 loc) · 12.2 KB
/
ADS1230.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**
**********************************************************************************
* @file ADS1230.c
* @author Ali Moallem (https://github.com/AliMoal)
* @brief For working with ADS1230 IC
**********************************************************************************
*
*! Copyright (c) 2021 Mahda Embedded System (MIT License)
*!
*! Permission is hereby granted, free of charge, to any person obtaining a copy
*! of this software and associated documentation files (the "Software"), to deal
*! in the Software without restriction, including without limitation the rights
*! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*! copies of the Software, and to permit persons to whom the Software is
*! furnished to do so, subject to the following conditions:
*!
*! The above copyright notice and this permission notice shall be included in all
*! copies or substantial portions of the Software.
*!
*! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*! SOFTWARE.
*!
**********************************************************************************
**/
//* Private Includes -------------------------------------------------------------- //
#include "ADS1230.h"
//* Private Defines and Macros ---------------------------------------------------- //
#define ADCReadOneBit(x) ADC_Handler->ADC_SCLK_HIGH(); \
ADC_Handler->ADC_Delay_US(1); \
ADC_Handler->ADC_SCLK_LOW(); \
ADC_Handler->ADCDataVal.Part##x = ADC_Handler->ADC_Read_DRDY_DOUT(); \
ADC_Handler->ADC_Delay_US(1)
//* Others ------------------------------------------------------------------------ //
#ifdef Debug_Enable
#include <stdio.h> // for debug
#define PROGRAMLOG(arg...) printf(arg)
#else
#define PROGRAMLOG(arg...)
#endif
/**
** ==================================================================================
** ##### Public Functions #####
** ==================================================================================
**/
/**
* @brief Initializes The ADC and Library
* @note User MUST call this at the the begining of the program ONCE!
* @param ADC_Handler: Pointer Of Library Handler
* @param ADC_Gain: ADC Gain
* @note Not Important if user handles ADC_Speed
* - 0: 64
* - 1: 128
* @param ADC_Speed: ADC Speed
* @note Not Important if user handles ADC_Speed
* - 0: 10SPS
* - 1: 80SPS
* @param ADC_Blocking: ADC Blocking
* - 0: non-Blocking Mode - User Have to check DRDY Pin for Data Ready then use Read Functions
* - 1: Blocking Mode - All Read Functions check DRDY then Read Data
* @param UseSPI: using SPI or GPIO to communicate with ADC
* - 0: Use GPIO for reading data
* - 1: Use SPI for reading data
* @retval None
*/
void
ADS1230_Init(ADS1230_Handler_t *ADC_Handler, bool ADC_Gain, bool ADC_Speed, bool ADC_Blocking, bool UseSPI)
{
if (!ADC_Handler)
return;
if (ADC_Gain)
{
if (ADC_Handler->ADC_Gain_HIGH)
ADC_Handler->ADC_Gain_HIGH();
}
else
{
if (ADC_Handler->ADC_Gain_LOW)
ADC_Handler->ADC_Gain_LOW();
}
ADC_Handler->ADC_Delay_US(1);
if (ADC_Speed)
{
if (ADC_Handler->ADC_Speed_HIGH)
ADC_Handler->ADC_Speed_HIGH();
}
else
{
if (ADC_Handler->ADC_Speed_LOW)
ADC_Handler->ADC_Speed_LOW();
}
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_LOW();
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->UseSPI = UseSPI;
ADC_Handler->SpeedMode = ADC_Speed;
ADC_Handler->BlockingMode = ADC_Blocking;
if (ADC_Handler->ADC_PDWN_HIGH)
{
ADC_Handler->ADC_PDWN_HIGH();
if (ADC_Handler->SpeedMode)
ADC_Handler->ADC_Delay_MS(55);
else
ADC_Handler->ADC_Delay_MS(410);
}
}
/**
* @brief Enables Power-Down mode Of ADC
* @note If you want use this function, You have to configure ADC_PDWN_LOW in ADS1230_Handler
* @param ADC_Handler: Pointer Of Library Handler
* @retval None
*/
void
ADS1230_PowerDown_Enable(ADS1230_Handler_t *ADC_Handler)
{
if (ADC_Handler->ADC_PDWN_LOW)
ADC_Handler->ADC_PDWN_LOW();
ADC_Handler->ADC_Delay_US(30);
}
/**
* @brief Disables Power-Down mode Of ADC
* @note If you want use this function, You have to configure ADC_PDWN_HIGH in ADS1230_Handler
* @param ADC_Handler: Pointer Of Library Handler
* @retval None
*/
void
ADS1230_PowerDown_Disable(ADS1230_Handler_t *ADC_Handler)
{
if (ADC_Handler->ADC_PDWN_HIGH)
ADC_Handler->ADC_PDWN_HIGH();
if (ADC_Handler->SpeedMode)
ADC_Handler->ADC_Delay_MS(50);
else
ADC_Handler->ADC_Delay_MS(400);
}
/**
* @brief Reads data Then Enables Standby Mode of ADC
* @param ADC_Handler: Pointer Of Library Handler
* @retval ADC Raw Data
*/
int32_t
ADS1230_Standby_Enable(ADS1230_Handler_t *ADC_Handler)
{
if (ADC_Handler->BlockingMode)
while (ADC_Handler->ADC_Read_DRDY_DOUT()); // For checking DRDY
if (ADC_Handler->UseSPI)
{
uint8_t Buffer[3];
ADC_Handler->ADC_SPI_READ_24bit(Buffer);
ADC_Handler->ADCDataVal.Byte1 = Buffer[0];
ADC_Handler->ADCDataVal.Byte2 = Buffer[1];
ADC_Handler->ADCDataVal.Byte3 = Buffer[2];
ADC_Handler->ADCDataVal.Zero = 0;
}
else
{
ADCReadOneBit(20);
ADCReadOneBit(19);
ADCReadOneBit(18);
ADCReadOneBit(17);
ADCReadOneBit(16);
ADCReadOneBit(15);
ADCReadOneBit(14);
ADCReadOneBit(13);
ADCReadOneBit(12);
ADCReadOneBit(11);
ADCReadOneBit(10);
ADCReadOneBit(9);
ADCReadOneBit(8);
ADCReadOneBit(7);
ADCReadOneBit(6);
ADCReadOneBit(5);
ADCReadOneBit(4);
ADCReadOneBit(3);
ADCReadOneBit(2);
ADC_Handler->ADC_SCLK_HIGH();
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADCDataVal.Part1 = ADC_Handler->ADC_Read_DRDY_DOUT();
}
ADC_Handler->ADC_Delay_US(25);
return ADC_Handler->ADCDataVal.INT32Value / 4096;
}
/**
* @brief Disables Standby Mode Of ADC
* @param ADC_Handler: Pointer Of Library Handler
* @retval None
*/
void
ADS1230_Standby_Disable(ADS1230_Handler_t *ADC_Handler)
{
ADC_Handler->ADC_SCLK_LOW();
if (ADC_Handler->SpeedMode)
ADC_Handler->ADC_Delay_MS(55);
else
ADC_Handler->ADC_Delay_MS(410);
}
/**
* @brief Reads data Then Enables Standby Mode of ADC With Offset Calibration
* @param ADC_Handler: Pointer Of Library Handler
* @retval ADC Raw Data
*/
int32_t
ADS1230_StandbyWithOffsetCalibration_Enable(ADS1230_Handler_t *ADC_Handler)
{
if (ADC_Handler->BlockingMode)
while (ADC_Handler->ADC_Read_DRDY_DOUT()); // For checking DRDY
if (ADC_Handler->UseSPI)
{
uint8_t Buffer[3];
ADC_Handler->ADC_SPI_READ_24bit(Buffer);
ADC_Handler->ADCDataVal.Byte1 = Buffer[0];
ADC_Handler->ADCDataVal.Byte2 = Buffer[1];
ADC_Handler->ADCDataVal.Byte3 = Buffer[2];
ADC_Handler->ADCDataVal.Zero = 0;
}
else
{
ADCReadOneBit(20);
ADCReadOneBit(19);
ADCReadOneBit(18);
ADCReadOneBit(17);
ADCReadOneBit(16);
ADCReadOneBit(15);
ADCReadOneBit(14);
ADCReadOneBit(13);
ADCReadOneBit(12);
ADCReadOneBit(11);
ADCReadOneBit(10);
ADCReadOneBit(9);
ADCReadOneBit(8);
ADCReadOneBit(7);
ADCReadOneBit(6);
ADCReadOneBit(5);
ADCReadOneBit(4);
ADCReadOneBit(3);
ADCReadOneBit(2);
ADCReadOneBit(1);
ADC_Handler->ADC_SCLK_HIGH(); // 21st
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_LOW();
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_HIGH(); // 22nd
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_LOW();
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_HIGH(); // 23rd
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_LOW();
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_HIGH(); // 24th
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_LOW();
ADC_Handler->ADC_Delay_US(1);
}
ADC_Handler->ADC_SCLK_HIGH(); // 25th
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_LOW();
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_HIGH(); // 26th
ADC_Handler->ADC_Delay_US(25);
return ADC_Handler->ADCDataVal.INT32Value / 4096;
}
/**
* @brief Disables Standby Mode Of ADC With Offset Calibration
* @param ADC_Handler: Pointer Of Library Handler
* @retval None
*/
void
ADS1230_StandbyWithOffsetCalibration_Disable(ADS1230_Handler_t *ADC_Handler)
{
ADC_Handler->ADC_SCLK_LOW();
if (ADC_Handler->SpeedMode)
ADC_Handler->ADC_Delay_MS(110);
else
ADC_Handler->ADC_Delay_MS(810);
}
/**
* @brief Reads Data Regularly
* @param ADC_Handler: Pointer Of Library Handler
* @retval ADC Raw Data
*/
int32_t
ADS1230_RegularRead(ADS1230_Handler_t *ADC_Handler)
{
if (ADC_Handler->BlockingMode)
while (ADC_Handler->ADC_Read_DRDY_DOUT()); // For checking DRDY
if (ADC_Handler->UseSPI)
{
uint8_t Buffer[3];
ADC_Handler->ADC_SPI_READ_24bit(Buffer);
ADC_Handler->ADCDataVal.Byte1 = Buffer[0];
ADC_Handler->ADCDataVal.Byte2 = Buffer[1];
ADC_Handler->ADCDataVal.Byte3 = Buffer[2];
ADC_Handler->ADCDataVal.Zero = 0;
}
else
{
ADCReadOneBit(20);
ADCReadOneBit(19);
ADCReadOneBit(18);
ADCReadOneBit(17);
ADCReadOneBit(16);
ADCReadOneBit(15);
ADCReadOneBit(14);
ADCReadOneBit(13);
ADCReadOneBit(12);
ADCReadOneBit(11);
ADCReadOneBit(10);
ADCReadOneBit(9);
ADCReadOneBit(8);
ADCReadOneBit(7);
ADCReadOneBit(6);
ADCReadOneBit(5);
ADCReadOneBit(4);
ADCReadOneBit(3);
ADCReadOneBit(2);
ADCReadOneBit(1);
ADC_Handler->ADC_SCLK_HIGH(); // 21st SCLK to force DRDY/Dout to HIGH
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_LOW();
}
return ADC_Handler->ADCDataVal.INT32Value / 4096;
}
/**
* @brief Reads Data Then Offset Calibration
* @param ADC_Handler: Pointer Of Library Handler
* @retval ADC Raw Data
*/
int32_t
ADS1230_OffsetCalibration(ADS1230_Handler_t *ADC_Handler)
{
if (ADC_Handler->BlockingMode)
while (ADC_Handler->ADC_Read_DRDY_DOUT()); // For checking DRDY
if (ADC_Handler->UseSPI)
{
uint8_t Buffer[3];
ADC_Handler->ADC_SPI_READ_24bit(Buffer);
ADC_Handler->ADCDataVal.Byte1 = Buffer[0];
ADC_Handler->ADCDataVal.Byte2 = Buffer[1];
ADC_Handler->ADCDataVal.Byte3 = Buffer[2];
ADC_Handler->ADCDataVal.Zero = 0;
}
else
{
ADCReadOneBit(20);
ADCReadOneBit(19);
ADCReadOneBit(18);
ADCReadOneBit(17);
ADCReadOneBit(16);
ADCReadOneBit(15);
ADCReadOneBit(14);
ADCReadOneBit(13);
ADCReadOneBit(12);
ADCReadOneBit(11);
ADCReadOneBit(10);
ADCReadOneBit(9);
ADCReadOneBit(8);
ADCReadOneBit(7);
ADCReadOneBit(6);
ADCReadOneBit(5);
ADCReadOneBit(4);
ADCReadOneBit(3);
ADCReadOneBit(2);
ADCReadOneBit(1);
ADC_Handler->ADC_SCLK_HIGH(); // 21st SCLK to force DRDY/Dout to HIGH
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_LOW();
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_HIGH(); // 22nd
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_LOW();
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_HIGH(); // 23rd
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_LOW();
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_HIGH(); // 24th
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_LOW();
ADC_Handler->ADC_Delay_US(1);
}
ADC_Handler->ADC_SCLK_HIGH(); // 25th
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_LOW();
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_HIGH(); // 26th for Offset Calibration
ADC_Handler->ADC_Delay_US(1);
ADC_Handler->ADC_SCLK_LOW();
if (ADC_Handler->SpeedMode)
ADC_Handler->ADC_Delay_US(110000);
else
ADC_Handler->ADC_Delay_US(810000);
return ADC_Handler->ADCDataVal.INT32Value / 4096;
}