-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathads12xx.cpp
More file actions
167 lines (140 loc) · 4.67 KB
/
ads12xx.cpp
File metadata and controls
167 lines (140 loc) · 4.67 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
#include "ads12xx.h"
volatile int DRDY_state = HIGH;
// Waits untill DRDY Pin is falling (see Interrupt setup).
// Some commands like WREG, RREG need the DRDY to be low.
bool waitforDRDY() {
int timeout=0;
timeout = millis();
while (DRDY_state) {
//Serial.println("Wait for DRDY");
//if(millis()-timeout >10000){
// return 0;
//}
continue;
}
noInterrupts();
DRDY_state = HIGH;
interrupts();
}
//Interrupt function
void DRDY_Interuppt() {
DRDY_state = LOW;
}
// ads12xx setup
ads12xx::ads12xx() {}
void ads12xx::begin(int CS, int START, int DRDY, int _RESET) {
pinMode(CS, OUTPUT); // set the slaveSelectPin as an output:
digitalWriteFast(CS, HIGH); // CS HIGH = nothing selected
pinMode(DRDY, INPUT); // DRDY read
pinMode(START, OUTPUT);
digitalWriteFast(START, HIGH);
pinMode(RESET, OUTPUT); // set the slaveSelectPin as an output:
digitalWriteFast(RESET, HIGH);
_CS = CS;
_DRDY = DRDY;
delay(500);
SPI.begin();
attachInterrupt(digitalPinToInterrupt(_DRDY), DRDY_Interuppt, FALLING); //Interrupt setup for DRDY detection
delay(500);
}
// function to get a 3byte conversion result from the adc
long ads12xx::GetConversion() {
int32_t regData;;
if(waitforDRDY()==0){ // Wait until DRDY is LOW
Serial.println("Wait for DRDY Timeout");
return 0;
};
SPI.beginTransaction(SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE1));
digitalWriteFast(_CS, LOW); //Pull SS Low to Enable Communications with ADS1247
delayMicroseconds(10); // RD: Wait 25ns for ADC12xx to get ready
SPI.transfer(RDATA); //Issue RDATA
delayMicroseconds(10);
regData |= SPI.transfer(NOP);
//delayMicroseconds(10);
regData <<= 8;
regData |= SPI.transfer(NOP);
//delayMicroseconds(10);
regData <<= 8;
regData |= SPI.transfer(NOP);
delayMicroseconds(10);
digitalWriteFast(_CS, HIGH);
SPI.endTransaction();
return regData;
}
// function to write a register value to the adc
// argumen: adress for the register to write into, value to write
void ads12xx::SetRegisterValue(uint8_t regAdress, uint8_t regValue) {
#ifdef ADS1248
if (regAdress == IDAC0) {
regValue = regValue | IDAC0_ID; // add non 0 non-write register value IDAC0_ID
}
#endif
uint8_t regValuePre = ads12xx::GetRegisterValue(regAdress);
if (regValue != regValuePre) {
//digitalWriteFast(_START, HIGH);
delayMicroseconds(10);
//waitforDRDY();
SPI.beginTransaction(SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE1)); // initialize SPI with SPI_SPEED, MSB first, SPI Mode1
digitalWriteFast(_CS, LOW);
delayMicroseconds(10);
//SPI.transfer(SDATAC); //Issue SDATAC
delayMicroseconds(10);
SPI.transfer(WREG | regAdress); // send 1st command byte, address of the register
SPI.transfer(0x00); // send 2nd command byte, write only one register
SPI.transfer(regValue); // write data (1 Byte) for the register
delayMicroseconds(10);
digitalWriteFast(_CS, HIGH);
//digitalWriteFast(_START, LOW);
if (regValue != ads12xx::GetRegisterValue(regAdress)) { //Check if write was succesfull
Serial.print("Write to Register 0x");
Serial.print(regAdress, HEX);
Serial.println(" failed!");
}
SPI.endTransaction();
}
}
//function to read a register value from the adc
//argument: adress for the register to read
unsigned long ads12xx::GetRegisterValue(uint8_t regAdress) {
uint8_t bufr;
//digitalWriteFast(_START, HIGH);
//waitforDRDY();
SPI.beginTransaction(SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE1)); // initialize SPI with 4Mhz clock, MSB first, SPI Mode0
digitalWriteFast(_CS, LOW);
delayMicroseconds(10);
SPI.transfer(RREG | regAdress); // send 1st command byte, address of the register
SPI.transfer(0x00); // send 2nd command byte, read only one register
delayMicroseconds(10);
bufr = SPI.transfer(NOP); // read data of the register
delayMicroseconds(10);
digitalWriteFast(_CS, HIGH);
//digitalWriteFast(_START, LOW);
SPI.endTransaction();
return bufr;
}
/*
Sends a Command to the ADC
Like SELFCAL, GAIN, SYNC, WAKEUP
*/
void ads12xx::SendCMD(uint8_t cmd) {
waitforDRDY();
SPI.beginTransaction(SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE1)); // initialize SPI with 4Mhz clock, MSB first, SPI Mode0
digitalWriteFast(_CS, LOW);
delayMicroseconds(10);
SPI.transfer(cmd);
delayMicroseconds(10);
digitalWriteFast(_CS, HIGH);
SPI.endTransaction();
}
// function to reset the adc
void ads12xx::Reset() {
SPI.beginTransaction(SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE1)); // initialize SPI with clock, MSB first, SPI Mode1
digitalWriteFast(_CS, LOW);
delayMicroseconds(10);
SPI.transfer(RESET); //Reset
delay(2); //Minimum 0.6ms required for Reset to finish.
SPI.transfer(SDATAC); //Issue SDATAC
delayMicroseconds(100);
digitalWriteFast(_CS, HIGH);
SPI.endTransaction();
}