0% found this document useful (0 votes)
218 views3 pages

AD7730 Code

This code configures and initializes an AD7730 analog-to-digital converter (ADC) chip to take continuous readings from a sensor and save the readings to an SD card file every 5 seconds. It sets up the SPI communication and pin connections, performs internal calibration on the ADC, puts it in continuous reading mode, reads the output data bytes during each conversion, converts it to a long integer and string, and writes the string to an SD card file.

Uploaded by

MikelDonosti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
218 views3 pages

AD7730 Code

This code configures and initializes an AD7730 analog-to-digital converter (ADC) chip to take continuous readings from a sensor and save the readings to an SD card file every 5 seconds. It sets up the SPI communication and pin connections, performs internal calibration on the ADC, puts it in continuous reading mode, reads the output data bytes during each conversion, converts it to a long integer and string, and writes the string to an SD card file.

Uploaded by

MikelDonosti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <SPI.

h>
#include <SdFat.h>
SdFat sd;

SdFile myFile;

#define RESET 8
#define RDY 9
const int sdchipSelect = 4;

byte firstByte;
byte secondByte;
byte thirdByte;
unsigned long Dout = 0;
char charbuff[10];

void setup() {

Serial.begin(9600);
/*---////////// Initialize SD Card ///////////---*/
if (!sd.begin(sdchipSelect, SPI_HALF_SPEED)) {
Serial.println("SD Initialization failed.");
}
else {
Serial.println("SD Initialization done.");
}

/*---////////// Set up AD7730 ///////////---*/


Serial.println("Initializing AD7730 Shield...");
pinMode(RDY, INPUT);
pinMode(RESET, OUTPUT);

SPI.begin();
SPI.setDataMode(SPI_MODE3);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV16);

// Reset AD7730
digitalWrite(RESET, LOW);
delay(200);
digitalWrite(RESET, HIGH);
delay(200);
// Set next action to Write to Filter Register
digitalWrite(SS, LOW); // AD7730 CS' = SS = Pin 10 - Arduino
SPI.transfer(0x03);
digitalWrite(SS, HIGH);
// Write Values to Filter Register
digitalWrite(SS, LOW);
SPI.transfer(0x80);
SPI.transfer(0x00);
SPI.transfer(0x10);
digitalWrite(SS, HIGH);
delay(30);
// Set next action to Write to DAC Register
digitalWrite(SS, LOW);
SPI.transfer(0x04);
digitalWrite(SS, HIGH);
// Write Values to DAC Register
digitalWrite(SS, LOW);
SPI.transfer(0x00);
digitalWrite(SS, HIGH);
delay(30);
// Set next action to Write to Mode Register
digitalWrite(SS, LOW);
SPI.transfer(0x02);
digitalWrite(SS, HIGH);
// Internal Full Calibration
digitalWrite(SS, LOW);
SPI.transfer(0xB1);
SPI.transfer(0x80);
digitalWrite(SS, HIGH);
while (digitalRead(RDY) != LOW) {
} // Wait until AD7730 is ready.
// Set next action to Write to Mode Register
digitalWrite(SS, LOW);
SPI.transfer(0x02);
digitalWrite(SS, HIGH);
// Internal Zero Calibration
digitalWrite(SS, LOW);
SPI.transfer(0x91);
SPI.transfer(0x80);
digitalWrite(SS, HIGH);
while (digitalRead(RDY) != LOW) {
} // Wait until AD7730 is ready.
// Set next action to Write to Mode Register
digitalWrite(SS, LOW);
SPI.transfer(0x02);
digitalWrite(SS, HIGH);
// Write Continuous Mode
digitalWrite(SS, LOW);
SPI.transfer(0x31); // 0x21 for bipolar
SPI.transfer(0x80);
digitalWrite(SS, HIGH);
while (digitalRead(RDY) != LOW) {
} // Wait until AD7730 is ready.
// Set next action is to read data continuosly from Data Register
digitalWrite(SS, LOW);
SPI.transfer(0x21);
digitalWrite(SS, HIGH);
digitalWrite(MOSI, LOW);
Serial.println("AD7730 initialization done.");
}

void loop() {
while (digitalRead(RDY) != LOW) {
}
digitalWrite(SS, LOW); // Select AD7730
firstByte = SPI.transfer(0);
secondByte = SPI.transfer(0);
thirdByte = SPI.transfer(0);
digitalWrite(SS, HIGH); // Deselect AD7730
Dout = thirdByte + secondByte * 256L + firstByte * 256L * 256L;
Serial.println(Dout);

float fDout = Dout;


dtostrf(fDout, 4, 0, charbuff);

if (myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {


Serial.print("Writing to test.txt...");
myFile.println(charbuff);
myFile.close();
Serial.println("done.");
}
delay(5000);
}

/ Internal Full Calibration


digitalWrite(SS, LOW);
SPI.transfer(0xB1);
SPI.transfer(0x80);
digitalWrite(SS, HIGH);
while (digitalRead(RDY) != LOW) {
} // Wait until AD7730 is ready

You might also like