168 lines
4.7 KiB
Dart
168 lines
4.7 KiB
Dart
import 'package:dart_periphery/dart_periphery.dart';
|
|
|
|
class Ads1256 {
|
|
static const int RADD_STATUS = 0x00;
|
|
static const int RADD_MUX = 0x01;
|
|
static const int RADD_ADCON = 0x02;
|
|
static const int RADD_DRATE = 0x03;
|
|
static const int RADD_IO = 0x04;
|
|
static const int RADD_OFC0 = 0x05;
|
|
static const int RADD_OFC1 = 0x06;
|
|
static const int RADD_OFC2 = 0x07;
|
|
static const int RADD_FSC0 = 0x08;
|
|
static const int RADD_FSC1 = 0x09;
|
|
static const int RADD_FSC2 = 0x0A;
|
|
|
|
static const int CMD_WAKEUP = 0x00;
|
|
static const int CMD_RDATA = 0x01;
|
|
static const int CMD_RDATAC = 0x03;
|
|
static const int CMD_SDATAC = 0x0f;
|
|
static const int CMD_RREG = 0x10;
|
|
static const int CMD_WREG = 0x50;
|
|
static const int CMD_SELFCAL = 0xF0;
|
|
static const int CMD_SELFOCAL = 0xF1;
|
|
static const int CMD_SELFGCAL = 0xF2;
|
|
static const int CMD_SYSOCAL = 0xF3;
|
|
static const int CMD_SYSGCAL = 0xF4;
|
|
static const int CMD_SYNC = 0xFC;
|
|
static const int CMD_STANDBY = 0xFD;
|
|
static const int CMD_RESET = 0xFE;
|
|
|
|
static const int MUXP_AIN0 = 0x00;
|
|
static const int MUXP_AIN1 = 0x10;
|
|
static const int MUXP_AIN2 = 0x20;
|
|
static const int MUXP_AIN3 = 0x30;
|
|
static const int MUXP_AIN4 = 0x40;
|
|
static const int MUXP_AIN5 = 0x50;
|
|
static const int MUXP_AIN6 = 0x60;
|
|
static const int MUXP_AIN7 = 0x70;
|
|
static const int MUXP_AINCOM = 0x80;
|
|
|
|
static const int MUXN_AIN0 = 0x00;
|
|
static const int MUXN_AIN1 = 0x01;
|
|
static const int MUXN_AIN2 = 0x02;
|
|
static const int MUXN_AIN3 = 0x03;
|
|
static const int MUXN_AIN4 = 0x04;
|
|
static const int MUXN_AIN5 = 0x05;
|
|
static const int MUXN_AIN6 = 0x06;
|
|
static const int MUXN_AIN7 = 0x07;
|
|
static const int MUXN_AINCOM = 0x08;
|
|
|
|
static const int GAIN_1 = 0x00;
|
|
static const int GAIN_2 = 0x01;
|
|
static const int GAIN_4 = 0x02;
|
|
static const int GAIN_8 = 0x03;
|
|
static const int GAIN_16 = 0x04;
|
|
static const int GAIN_32 = 0x05;
|
|
static const int GAIN_64 = 0x06;
|
|
|
|
static const int DRATE_30000SPS = 0xF0;
|
|
static const int DRATE_15000SPS = 0xE0;
|
|
static const int DRATE_7500SPS = 0xD0;
|
|
static const int DRATE_3750SPS = 0xC0;
|
|
static const int DRATE_2000SPS = 0xB0;
|
|
static const int DRATE_1000SPS = 0xA1;
|
|
static const int DRATE_500SPS = 0x92;
|
|
static const int DRATE_100SPS = 0x82;
|
|
static const int DRATE_60SPS = 0x72;
|
|
static const int DRATE_50SPS = 0x63;
|
|
static const int DRATE_30SPS = 0x53;
|
|
static const int DRATE_25SPS = 0x43;
|
|
static const int DRATE_15SPS = 0x33;
|
|
static const int DRATE_10SPS = 0x23;
|
|
static const int DRATE_5SPS = 0x13;
|
|
static const int DRATE_2_5SPS = 0x03;
|
|
|
|
final SPI _spi; //SPI
|
|
final GPIO _drdy; //GPIO
|
|
final GPIO? _cs; //GPIO
|
|
|
|
Ads1256(this._spi, this._drdy, this._cs) {}
|
|
|
|
void _sendCommand(int reg) {
|
|
_CSON();
|
|
_waitDRDY();
|
|
_spi.transfer([reg], false);
|
|
_CSON();
|
|
}
|
|
|
|
void _writeRegister(int reg, int wdata) {
|
|
_CSON();
|
|
_spi.transfer([CMD_WREG, reg, 0, wdata], false);
|
|
_CSOFF();
|
|
}
|
|
|
|
int _readRegister(int reg) {
|
|
var val = _spi.transfer([CMD_RREG, 0, 0], true);
|
|
return val[2];
|
|
}
|
|
|
|
void _waitDRDY() {
|
|
while (_drdy.read()){}
|
|
}
|
|
|
|
void _CSON() {
|
|
//PORT_CS &= ~(1 << PINDEX_CS);
|
|
_cs?.write(false);
|
|
}
|
|
|
|
void _CSOFF() {
|
|
//PORT_CS |= (1 << PINDEX_CS);
|
|
_cs?.write(true);
|
|
}
|
|
|
|
void begin(int drate, int gain, bool buffen) {
|
|
_sendCommand(
|
|
CMD_SDATAC); // send out ADS1256_CMD_SDATAC command to stop continous reading mode.
|
|
_writeRegister(RADD_DRATE, drate); // write data rate register
|
|
int bytemask = 7;
|
|
int adcon = _readRegister(RADD_ADCON);
|
|
int byte2send = (adcon & ~bytemask) | gain;
|
|
_writeRegister(RADD_ADCON, byte2send);
|
|
if (buffen) {
|
|
setBufferEnable(_readRegister(RADD_STATUS));
|
|
}
|
|
_sendCommand(CMD_SELFCAL); // perform self calibration
|
|
_waitDRDY();
|
|
}
|
|
|
|
/* STATUS : STATUS REGISTER (ADDRESS 00h)
|
|
Reset Value = x1h
|
|
Bit 3 ORDER: Data Output Bit Order
|
|
0 = Most Significant Bit First (default)
|
|
1 = Least Significant Bit First
|
|
Bit 2 ACAL: Auto-Calibration
|
|
0 = Auto-Calibration Disabled (default)
|
|
1 = Auto-Calibration Enabled
|
|
Bit 1 BUFEN: Analog Input Buffer Enable
|
|
0 = Buffer Disabled (default)
|
|
1 = Buffer Enabled
|
|
Bit 0 DRDY: Data Ready (Read Only)
|
|
*/
|
|
int getStatus() {
|
|
_sendCommand(CMD_SDATAC);
|
|
return _readRegister(RADD_STATUS);
|
|
}
|
|
|
|
void setBufferEnable(int status) {
|
|
_writeRegister(RADD_STATUS, (status | (0x01 << 1)));
|
|
}
|
|
|
|
void resetBufferEnable(int status) {
|
|
_writeRegister(RADD_STATUS, (status & ~(0x01 << 1)));
|
|
}
|
|
|
|
void readTest() {
|
|
_CSON();
|
|
var _hml =
|
|
_spi.transfer([CMD_RDATA, CMD_WAKEUP, CMD_WAKEUP, CMD_WAKEUP], false);
|
|
print(_hml);
|
|
_CSOFF();
|
|
}
|
|
void dispose(){
|
|
_spi.dispose(); //SPI
|
|
_drdy.dispose(); //GPIO
|
|
_cs?.dispose(); //GPIO
|
|
}
|
|
}
|