forked from chepo92/ADS1256
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ADS1256.h
160 lines (144 loc) · 4.07 KB
/
ADS1256.h
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
/*
ADS1256.h - Arduino Library for communication with Texas Instrument ADS1256 ADC
Written by Adien Akhmad, August 2015
Modfified Jan 2019 by Axel Sepulveda for ATMEGA328
*/
#ifndef ADS1256_h
#define ADS1256_h
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
#define _DRDY PB1
#define _CS PB2
#define _RESET PB0
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define _DRDY PL0
#define _CS PB0
#define _RESET PL1
// Contributions are welcome
#elif defined(ARDUINO_ARCH_ESP32)
#error "Oops! ESP32 architecture not supported yet"
// Contributions are welcome
#elif defined(ESP8266)
#define _CS D1
#define _DRDY D2
#define _RESET D3
#define NOP __asm__ __volatile__ ("nop\n\t");
#if F_CPU == 80000000UL
//11 nops with 80 mhz
#define DELAY1 NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
#elif F_CPU == 160000000UL
//21 nops with 160mhz
#define DELAY1 NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
#endif
#else
// Contributions are welcome
#error "Oops! Your board architecture is not supported yet'"
#endif
#ifndef DELAY1
#define DELAY1 __builtin_avr_delay_cycles(3);
#endif
//#define ADSCLOCK 7680000UL
// ADS1256 Register
#define ADSSTATUS 0x00
#define MUX 0x01
#define ADCON 0x02
#define DRATE 0x03
#define IO 0x04
#define OFC0 0x05
#define OFC1 0x06
#define OFC2 0x07
#define FSC0 0x08
#define FSC1 0x09
#define FSC2 0x0A
// ADS1256 Command
#define WAKEUP 0x00
#define RDATA 0x01
#define RDATAC 0x03
#define SDATAC 0x0f
#define RREG 0x10
#define WREG 0x50
#define SELFCAL 0xF0
#define SELFOCAL 0xF1
#define SELFGCAL 0xF2
#define SYSOCAL 0xF3
#define SYSGCAL 0xF4
#define SYNC 0xFC
#define STANDBY 0xFD
#define RESET 0xFE
// define multiplexer codes
#define ADS1256_MUXP_AIN0 0x00
#define ADS1256_MUXP_AIN1 0x10
#define ADS1256_MUXP_AIN2 0x20
#define ADS1256_MUXP_AIN3 0x30
#define ADS1256_MUXP_AIN4 0x40
#define ADS1256_MUXP_AIN5 0x50
#define ADS1256_MUXP_AIN6 0x60
#define ADS1256_MUXP_AIN7 0x70
#define ADS1256_MUXP_AINCOM 0x80
#define ADS1256_MUXN_AIN0 0x00
#define ADS1256_MUXN_AIN1 0x01
#define ADS1256_MUXN_AIN2 0x02
#define ADS1256_MUXN_AIN3 0x03
#define ADS1256_MUXN_AIN4 0x04
#define ADS1256_MUXN_AIN5 0x05
#define ADS1256_MUXN_AIN6 0x06
#define ADS1256_MUXN_AIN7 0x07
#define ADS1256_MUXN_AINCOM 0x08
// define gain codes
#define ADS1256_GAIN_1 0x00
#define ADS1256_GAIN_2 0x01
#define ADS1256_GAIN_4 0x02
#define ADS1256_GAIN_8 0x03
#define ADS1256_GAIN_16 0x04
#define ADS1256_GAIN_32 0x05
#define ADS1256_GAIN_64 0x06
// define drate codes
/*
NOTE : Data Rate vary depending on crystal frequency. Data rates
listed below assumes the crystal frequency is 7.68Mhz
for other frequency consult the datasheet.
*/
#define ADS1256_DRATE_30000SPS 0xF0
#define ADS1256_DRATE_15000SPS 0xE0
#define ADS1256_DRATE_7500SPS 0xD0
#define ADS1256_DRATE_3750SPS 0xC0
#define ADS1256_DRATE_2000SPS 0xB0
#define ADS1256_DRATE_1000SPS 0xA1
#define ADS1256_DRATE_500SPS 0x92
#define ADS1256_DRATE_100SPS 0x82
#define ADS1256_DRATE_60SPS 0x72
#define ADS1256_DRATE_50SPS 0x63
#define ADS1256_DRATE_30SPS 0x53
#define ADS1256_DRATE_25SPS 0x43
#define ADS1256_DRATE_15SPS 0x33
#define ADS1256_DRATE_10SPS 0x23
#define ADS1256_DRATE_5SPS 0x13
#define ADS1256_DRATE_2_5SPS 0x03
#include "Arduino.h"
#include "SPI.h"
class ADS1256 {
public:
ADS1256(float clockspdMhz, float vref, bool useresetpin);
void writeRegister(unsigned char reg, unsigned char wdata);
unsigned char readRegister(unsigned char reg);
void sendCommand(unsigned char cmd);
float readCurrentChannel();
void setConversionFactor(float val);
void setChannel(byte channel);
void setChannel(byte AIP, byte AIN);
void begin(unsigned char drate, unsigned char gain, bool bufferenable);
void waitDRDY();
void setGain(uint8_t gain);
void readTest();
private:
void _delay4(); // t11 delay (4*tCLKIN)
void _delay50(); // t6 delay (50*tCLKIN)
void CSON();
void CSOFF();
unsigned long read_uint24();
long read_int32();
float read_float32();
byte _pga;
float _VREF;
float _conversionFactor;
};
#endif