-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadc_ADS114S0.cpp
More file actions
118 lines (98 loc) · 2.14 KB
/
adc_ADS114S0.cpp
File metadata and controls
118 lines (98 loc) · 2.14 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
#include "adc_ADS114S0.h"
ADS114S0::ADS114S0(const uint8_t CS)
: m_CS(CS)
{
SPI.begin();
pinMode(m_CS, OUTPUT);
endSPI();
}
void ADS114S0::beginSPI()
{
SPI.beginTransaction(SPISettings(SPI_CLOCK, MSBFIRST, SPI_MODE1));
digitalWrite(m_CS, LOW);
}
void ADS114S0::endSPI()
{
digitalWrite(m_CS, HIGH);
SPI.endTransaction();
}
void ADS114S0::reset()
{
beginSPI();
SPI.transfer(RESET);
endSPI();
delay(10);
writeRegisters(STATUS, DEFAULTS, 16);
}
void ADS114S0::writeRegister(const Register reg, const uint8_t value)
{
beginSPI();
SPI.transfer16(WREG | reg<<16);
SPI.transfer(value);
endSPI();
}
void ADS114S0::writeRegisters(const Register reg, const uint8_t values[], const uint8_t num)
{
beginSPI();
SPI.transfer16(WREG | reg<<16 | (num - 1)<<8);
for (uint8_t i = 0; i < num; i++)
{
SPI.transfer(values[i]);
}
endSPI();
}
uint8_t ADS114S0::readRegister(const Register reg)
{
beginSPI();
SPI.transfer16(RREG | reg<<16);
uint8_t value = SPI.transfer(0x00);
endSPI();
return value;
}
void ADS114S0::readRegisters(const Register reg, uint8_t values[], const uint8_t num)
{
beginSPI();
SPI.transfer16(RREG | reg<<16 | (num - 1)<<8);
for (uint8_t i = 0; i < num; i++)
{
values[i] = SPI.transfer(0x00);
}
endSPI();
}
void ADS114S0::setMux(const InputMux mux)
{
writeRegister(INPMUX, mux);
}
void ADS114S0::setPGAGain(const PGAGain gain)
{
writeRegister(PGA, gain);
}
void ADS114S0::setMode(const DataRate rate, const ClockSource clock, const ConversionMode mode)
{
writeRegister(DATARATE, rate | clock | mode);
}
void ADS114S0::setConversionMode(const ConversionMode mode)
{
uint8_t currentSettings = readRegister(DATARATE);
writeRegister(DATARATE, currentSettings | mode);
}
void ADS114S0::startConversion()
{
beginSPI();
SPI.transfer(START);
endSPI();
}
void ADS114S0::stopConversion()
{
beginSPI();
SPI.transfer(STOP);
endSPI();
}
uint16_t ADS114S0::readData()
{
beginSPI();
SPI.transfer(RDATA);
uint16_t data = SPI.transfer16(0x0000);
endSPI();
return data;
}