您好!
我正在尝试读取与 Arduino Uno 连接的 ads1218 ADC 的数据。 我已经在 AIN0 (+ve)和 AIN1 (-ve)中连接了5V 直流输入、4Mhz 晶体振荡器(2个22pF 电容器并联)用于 XIN 和 XOUT 引脚、fosc=4Mhz、fMOD =31.25kHz、PGA=1。 我的连接如下:
由于 Arduino 时钟为16MHz 且 fosc 为4Mhz、因此根据数据表、SCLK 周期可用于高达4tosc 周期(高达1MHz)、因此使用 SPI 时钟分频函数(SPI.setClockDivider(SPI_CLOCK_DIV16)、Arduino SPI 时钟可分频为1MHz 电平。
DRDY:引脚6
DIN:引脚11
DOUT:引脚12
SCLK:引脚13
CS:引脚7
#include <SPI.h>
#define RESET 0x06 //Send the RESET command (06h) to make sure the ADS1220 is properly reset after power-up
#define START 0x08 //Send the START/SYNC command (08h) to start converting in continuous conversion mode
// define the ads1218 command
#define RDATA 0x01 // read the latest ad conversion data
#define WREG 0x50 // 2 byte command, write data to register 0-15
#define DSYNC 0xFC // Sync DRDY
#define RESET 0xFE // reset the register to the power of the data, stop the continuous read mode, does not affect the ram data
#define ADS1218_CS_PIN 7
#define ADS1218_DRDY_PIN 6
int32_t stat;
void syncSerialData(void)
{
noInterrupts();
digitalWrite(ADS1218_CS_PIN, LOW);
delayMicroseconds(500);
SPI.transfer(DSYNC);
delayMicroseconds(500);
digitalWrite(ADS1218_CS_PIN, HIGH);
interrupts();
delay(2000);
}
void waitForDataReady(int timeOut) //wait for data ready
{
// wait for /DRDY = 1
while ((digitalRead(ADS1218_DRDY_PIN)) != LOW);
// wait for /DRDY = 0
while ((digitalRead(ADS1218_DRDY_PIN)) == LOW);
}
void setup()
{
pinMode(ADS1218_CS_PIN, OUTPUT);
digitalWrite(ADS1218_CS_PIN, HIGH);
Serial.begin(9600);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1);
SPI.setClockDivider(SPI_CLOCK_DIV16); // SCLK upto 4tosc periods
// setup register (00)
SPI.transfer(0x50);
SPI.transfer(0x1E); //speed:4Mhz ; fmod:fosc/256,ref enable,2.5Vref,,buffer enable,bit order:0 MSB first
// Setting the MUX register channel requirement
SPI.transfer(0x51);// write command: 0101 mux address(01)
SPI.transfer(0x05); // wreg -1(6 register to write due to 6 channels)
SPI.transfer(0x01); // ain0 and ain1
// Write command to address 02
SPI.transfer(0x52); // analog control register
SPI.transfer(0x00);// PGA set to 1
syncSerialData(); // Sync Data Ready with Serial clock edge
delay(50);
}
void loop()
{
digitalWrite(ADS1218_CS_PIN, LOW); //Take CS low
SPI.transfer(RDATA);
delayMicroseconds(12.5); // used:4Mhz (50tosc))
stat= SPI.transfer(0) ;
stat = (stat<<8) | SPI.transfer(0);
stat = (stat<<8) | SPI.transfer(0);
digitalWrite(ADS1218_CS_PIN, HIGH); // Clear CS to high
if (stat & 0x800000 )
{
stat = (stat | 0xFF000000);
}
Serial.println(stat);
delay(50); //
}