Вс фев 05, 2017 14:21:51
#define RESET 8
#define RDY_PIN 9
#define DEBUG 1
#define SPI_DELAY 10
#define READ_ONLY 0xFF
//Communication Register Values
#define CR_SINGLE_WRITE 0x00
#define CR_SINGLE_READ 0x10
#define CR_CONTINUOUS_READ_START 0x20
#define CR_CONTINUOUS_READ_STOP 0x30
#define CR_COMMUNICATION_REGISTER 0x00 //Write only
#define CR_STATUS_REGISTER 0x00 //Read only
#define CR_DATA_REGISTER 0x01
#define CR_MODE_REGISTER 0x02
#define CR_FILTER_REGISTER 0x03
#define CR_DAC_REGISTER 0x04
#define CR_OFFSET_REGISTER 0x05
#define CR_GAIN_REGISTER 0x06
#define CR_TEST_REGISTER 0x07
//Mode Register Values
#define MR1_MODE_IDLE 0x00
#define MR1_MODE_CONTINUOUS 0x20 //Standard Operation
#define MR1_MODE_SINGLE 0x40
#define MR1_MODE_STANDBY 0x60
#define MR1_MODE_INTERNAL_ZERO_CALIBRATION 0x80
#define MR1_MODE_INTERNAL_FULL_CALIBRATION 0xA0
#define MR1_MODE_SYSTEM_ZERO_CALIBRATION 0xC0
#define MR1_MODE_SYSTEM_FULL_CALIBRATION 0xE0
#define MR1_BU_BIPOLAR 0x00 //+- voltage defined by MR0_RANGE
#define MR1_BU_UNIPOLAR 0x10 //0 to voltage deifined by MRO_RANGE
#define MR1_WL_24_BIT 0x01
#define MR1_WL_16_BIT 0x00
#define MR0_HIREF_5V 0x80
#define MR0_HIREF_2P5V 0x00
#define MR0_RANGE_10MV 0x00
#define MR0_RANGE_20MV 0x01
#define MR0_RANGE_40MV 0x02
#define MR0_RANGE_80MV 0x03
#define MR0_CHANNEL_1 0x00
#define MR0_CHANNEL_2 0x01
#define MR0_CHANNEL_SHORT_1 0x02 //Used for internal noise check
#define MR0_CHANNEL_NEGATIVE_1_2 0x03 //Unknown use
#define MRO_BURNOUT_ON 0x04 //Advanced, to check if loadcell is burnt out
//Filter Register Values
#define FR2_SINC_AVERAGING_2048 0x80 //Base sample rate of 50 Hz
#define FR2_SINC_AVERAGING_1024 0x40 //Base sample rate of 100 Hz
#define FR2_SINC_AVERAGING_512 0x20 //Base sample rate of 200 Hz
#define FR2_SINC_AVERAGING_256 0x10 //Base sample rate of 400 Hz
#define FR1_SKIP_ON 0x02 //the FIR filter on the part is bypassed
#define FR1_SKIP_OFF 0x00
#define FR1_FAST_ON 0x01 //FIR is replaced with moving average on large step, sinc filter averages are used to compensate
#define FR1_FAST_OFF 0x00
#define FR0_CHOP_ON 0x10 //When the chop mode is enabled, the part is effectively chopped at its input and output to remove all offset and offset drift errors on the part.
#define FR0_CHOP_OFF 0x00 //Increases sample rate by x3
//DAC Register Values
#define DACR_OFFSET_SIGN_POSITIVE 0x00
#define DACR_OFFSET_SIGN_NEGATIVE 0x20
#define DACR_OFFSET_40MV 0x10
#define DACR_OFFSET_20MV 0x08
#define DACR_OFFSET_10MV 0x04
#define DACR_OFFSET_5MV 0x02
#define DACR_OFFSET_2P5MV 0x01
#define DACR_OFFSET_NONE 0x00
//current settings
#define CURRENT_MODE_1_SETTINGS (MR1_BU_UNIPOLAR | MR1_WL_24_BIT)
#define CURRENT_MODE_0_SETTINGS (MR0_HIREF_5V | MR0_RANGE_10MV | MR0_CHANNEL_1)
#include <SPI.h>
byte result1;
byte result2;
byte result3;
unsigned long result;
long time;
void reset() {
digitalWrite(RESET, LOW);
delay(200);
digitalWrite(RESET, HIGH);
delay(200);
}
void sendByte(byte toSend) {
if (DEBUG) {
Serial.print(" -> Transmitting 0x");
Serial.println(toSend, HEX);
}
digitalWrite(SS, LOW);
SPI.transfer(toSend);
digitalWrite(SS, HIGH);
}
void send2Bytes(byte first, byte second) {
if (DEBUG) {
Serial.print(" -> Transmitting 0x");
Serial.print(first, HEX);
Serial.print(" 0x");
Serial.println(second, HEX);
}
digitalWrite(SS, LOW);
SPI.transfer(first);
SPI.transfer(second);
digitalWrite(SS, HIGH);
}
void send3Bytes(byte first, byte second, byte third) {
if (DEBUG) {
Serial.print(" -> Transmitting 0x");
Serial.print(first, HEX);
Serial.print(" 0x");
Serial.print(second, HEX);
Serial.print(" 0x");
Serial.println(third, HEX);
}
digitalWrite(SS, LOW);
SPI.transfer(first);
SPI.transfer(second);
SPI.transfer(third);
digitalWrite(SS, HIGH);
}
void waitForReady() {
while (digitalRead(RDY_PIN) != LOW) {
if (DEBUG) {
Serial.print(".");
delay(50);
}
}
if (DEBUG)
Serial.print("\n");
}
void readBytes(int number) {
//Used when not in continuous mode, we send READ_ONLY (0xFF) to ensure that the first bit is 1 and will not update the AD7730 write's register
//(If you don't know what I am talking about seriously just read the datasheet once through)
digitalWrite(SS, LOW);
Serial.print(" <- Recieved");
for (int i = 0; i < number; i++) {
Serial.print(" 0x");
Serial.print(SPI.transfer(READ_ONLY), HEX);
}
Serial.print("\n");
digitalWrite(SS, HIGH);
}
void setup() {
Serial.begin(9600);
if (DEBUG)
Serial.println("Welcome");
pinMode(RDY_PIN , INPUT);
pinMode(MISO , INPUT);
pinMode(RESET , OUTPUT);
SPI.begin();
SPI.setDataMode(SPI_MODE3);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV16);
if (DEBUG)
Serial.println("Reseting AD7730");
time = millis();
reset();
if (DEBUG)
Serial.println("Beggining Setup");
if (DEBUG) {
Serial.println("Filter Default");
sendByte(CR_SINGLE_READ | CR_FILTER_REGISTER);
readBytes(3);
Serial.println("Setting Up Filter");
}
sendByte(0x3);
send3Bytes(0x80, 0x00, 0x10);
if (DEBUG) {
Serial.println("Filter set to ");
sendByte(CR_SINGLE_READ | CR_FILTER_REGISTER);
readBytes(3);
}
delay(30);
if (DEBUG) {
Serial.println("DAC Default");
sendByte(CR_SINGLE_READ | CR_DAC_REGISTER);
readBytes(1);
Serial.println("Setting Up DAC");
}
sendByte(0x04);
sendByte(0x23);
if (DEBUG) {
Serial.println("DAC set to ");
sendByte(CR_SINGLE_READ | CR_DAC_REGISTER);
readBytes(1);
}
delay(30);
if (DEBUG) {
Serial.println("MODE Default");
sendByte(CR_SINGLE_READ | CR_MODE_REGISTER);
readBytes(1);
Serial.println("Setting Up MODE. Internal full-scale calibration 0-10mV");
}
sendByte(0x02);
sendByte(0xb180);
waitForReady();
sendByte(0x02);
sendByte(0x9180);
waitForReady();
//---------- CONTINOUS MODE-----------------------------
if(DEBUG)
Serial.println("Starting Continous Mode");
sendByte(CR_SINGLE_WRITE | CR_MODE_REGISTER);
send2Bytes(MR1_MODE_CONTINUOUS | CURRENT_MODE_1_SETTINGS, CURRENT_MODE_0_SETTINGS);
while(digitalRead(RDY_PIN) != LOW) {
}
if(DEBUG)
Serial.println("Continuous conv. started");
//----------
//---------- CONTINUOUS READ--------------------------
if(DEBUG)
Serial.println("Starting Continuous Read");
sendByte(CR_CONTINUOUS_READ_START | CR_DATA_REGISTER);
if(DEBUG)
Serial.println("Reading started");
//----------
}
void loop() {
waitForReady();
//From the data sheet, we need to read bytes continously but hold the AD7730 DIN pin (Arduino MOSI) low, so we send 0b0000 0000 three times
digitalWrite(SS,LOW);
result1 = SPI.transfer(0);
result2 = SPI.transfer(0);
result3 = SPI.transfer(0);
digitalWrite(SS,HIGH);
if(DEBUG){
Serial.println("Data Registers (hex) ");
Serial.print(result1, HEX);
Serial.print(" ");
Serial.print(result2, HEX);
Serial.print(" ");
Serial.print(result3, HEX);
Serial.println();
}
result = result3 + result2 * 256L + result1 * 256L * 256L;
Serial.println(result);
}
Чт мар 02, 2017 14:00:59