Load Cell AD7730+SPI

Hi, I’m using the Duemilanove board, a AD7730 and a load cell(made by my project group :grin:) with spi interface.
The conections are listed below:

AD7730 Atmega328 Others
1 13 sck
2 Crystal 4.9152MHz
3
4 pol Gnd
5 sync/ Vdd
6 reset/ 8
7 Open
8 Agnd Gnd
9 AVdd Vdd
10 AIN1(+) S+ (load cell)
11 AIN1(-) S- (load cell)
12 AIN2(+)/D1 Open
13 AIN2(+)/D0 Open
14 ref in(+) Vdd
15 ref in(-) Gnd
16 Open
17 Open
18 standby/ Vdd
19 chipselect/ 10 ss (slave select)
20 ready/ 9
21 Dout 12 miso
22 Din 11 mosi
23 DVdd Vdd
24 Dgnd Gnd

LCD Atmega328 Others
D7 HEF4094 → 14
D6 HEF4094 → 13
D5 HEF4094 → 12
D4 HEF4094 → 11
D3
D2
D1
D0
E HEF4094 → 7
RW/ HEF4094 → 6
RS HEF4094 → 5
V0 Middle pin of the potenciometer
Vdd Vdd
Vss GND
15(V+) Vdd
16(V-) GND
5 HEF4094 → 1 strobe
6 HEF4094 → 2 digital in
7 HEF4094 → 3 clock pin
2(interruption) Tare extern
3(interruption)
0(analog in as digital out) Step motor ci uln2004 pino 14
1(analog in as digital out) Step motor ci uln2004 pino 15
2(analog in as digital out) Step motor ci uln2004 pino 16
3(analog in as digital out) Step motor ci uln2004 pino 17

code:

#define reset 8
#define rdy 9
#define ss 10 //slave select
#define miso 12 
#define mosi 11

#define lcd_linhas 2  // number of lines in your display
#define dout_pin  6  // Dout pin
#define strobe_pin 5  // Strobe pin
#define clock_pin 7  // Clock pin

#include <SPI.h>
#include <LCD3Wire.h>
#include <Stepper.h>

// comandos do AD7730 datasheet
const byte comm_reg_mode = 0x02; //select mode register
const byte comm_reg_data = 0x11; //continuos read data register
//const byte comm_filter= 0x03;//select filter register
const byte comm_reg_gain = 0x06;//select gain register
const byte mode_reg1_tara_int = 0x91; //tare intern + 24bits +unipolar 
const byte mode_reg1_tara_ext = 0xD1; //tare extern + 24bits +unipolar
const byte mode_reg2_calib = 0x80; //0mV -10mV range + AIN1 register 0
const byte mode_reg1_conv = 0x31; //continuos conversion 24bits +unipolar 

//const byte filter_reg1 = 0x20; // 512 of filter
//const byte filter_reg2 = 0x01;//fast mode without skip (with skip 0x03)
//const byte filter_reg3 = 0x00;//no chop no delay

const byte gain_reg1 = 0x00;//gain=1000
const byte gain_reg2 = 0x03;
const byte gain_reg3 = 0xE8;

const int passos_revolucao=200;//steps per revolution

Stepper motor_passos(passos_revolucao, 15, 17, 16, 14);            
LCD3Wire lcd = LCD3Wire(lcd_linhas, dout_pin, strobe_pin, clock_pin);

const int taraExtpin = 2; 
const int resetPin = 3;
const int motorPassoA0 = 14; 
const int motorPassoA1 = 15; 
const int motorPassoA2 = 16; 
const int motorPassoA3 = 17; 
 

int del = 100; // delay time

void setup() {
  lcd.init(); 

  Serial.begin(9600);
  pinMode(rdy , INPUT);
  pinMode(reset , OUTPUT);
  pinMode(ss , OUTPUT);
  pinMode(miso , INPUT);
  pinMode(mosi , OUTPUT);
  digitalWrite(reset , HIGH);
  digitalWrite(ss , LOW);

  pinMode(taraExtpin, INPUT);
  pinMode(resetPin, INPUT);
  pinMode(motorPassoA0, OUTPUT);
  pinMode(motorPassoA1, OUTPUT);
  pinMode(motorPassoA2, OUTPUT);
  pinMode(motorPassoA3, OUTPUT);

//-----------------------------------------------------------------------------------
//SPI
  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST); //
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  delay(del);

//-----------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------
//ad7730
digitalWrite(ss, LOW);
  SPI.transfer(comm_reg_mode); // seleciona o mode register
  SPI.transfer(mode_reg1_tara_int);//tara a balança(interno)
  SPI.transfer(mode_reg2_calib);// calibra o ad7730 0mV -10mV range de entrada+AIN1 registro 0 
 while(digitalRead(rdy) != LOW) {} //pino rdy/ vai para alto e depois para baixo quando a operação é concluida
digitalWrite(ss, HIGH);

digitalWrite(ss, LOW);
  SPI.transfer(comm_reg_mode); // seleciona o mode register
  SPI.transfer(mode_reg1_conv);// ad7730 vai ficar convertendo continuamente a saída
  SPI.transfer(mode_reg2_calib);// calibra o ad7730 0mV -10mV range de entrada+AIN1 registro 0 
while(digitalRead(rdy)!= LOW) {}
digitalWrite(ss, HIGH);

digitalWrite(ss, LOW);
  SPI.transfer(comm_reg_gain); // seleciona o gain register
  SPI.transfer(gain_reg1);// ganho de 1000
  SPI.transfer(gain_reg2);
  SPI.transfer(gain_reg3);
while(digitalRead(rdy)!= LOW) {}
digitalWrite(ss, HIGH);

digitalWrite(ss, LOW);
  SPI.transfer(comm_reg_data); //leitura continua do data register
digitalWrite(ss, HIGH);

//-----------------------------------------------------------------------------------
}
unsigned long result;

void loop()
{
attachInterrupt(0, tara_ext, FALLING); //digital pin 2 -> 0
attachInterrupt(1, reseta_adc7730, FALLING); //digital pin 3 -> 1
  
  int entradaA4 = analogRead(A4);
  int veloMotor = map(entradaA4, 0, 1023, 0, 100);

digitalWrite(ss, LOW);
  SPI.transfer(comm_reg_data);
//result=SPI.transfer(comm_reg_data); how to get the 24 bits from data register?
while(digitalRead(rdy)!= LOW) {}  
digitalWrite(ss, HIGH);
result=result*2+result;

lcd.clear();
lcd.print(result);
lcd.printIn(" grams");
delay(100);
Serial.print(result)

if (result!=0x000)
  {
    motor_passos.setSpeed(veloMotor);
    motor_passos.step(passos_revolucao);
  }
delay(200); //delay 200ms


}

//---------------------------------------------------------------------------------------
//subroutines
void tara_ext()
  {
  digitalWrite(ss, LOW);
   SPI.transfer(comm_reg_mode);    
   SPI.transfer(mode_reg1_tara_ext);
  while(digitalRead(rdy)!= LOW) {}
  digitalWrite(ss, HIGH);

  digitalWrite(ss, LOW);
   SPI.transfer(comm_reg_mode);  
   SPI.transfer(mode_reg1_conv);
   SPI.transfer(mode_reg2_calib);  
   while(digitalRead(rdy)!= LOW) {}
  digitalWrite(ss, HIGH);

  digitalWrite(ss, LOW);
   SPI.transfer(comm_reg_data); 
   result=SPI.transfer(miso);
  while(digitalRead(rdy)!= LOW) {}
  digitalWrite(ss, HIGH);
  }
  
  void reseta_adc7730()
  {
  digitalWrite(reset,LOW); 
  delay(del);
  digitalWrite(reset,HIGH);
  }

We read the data sheet, but we didn’t understood how to get the 24 bits from data register, we known, that we need to send a byte to communication register “asking” to read the data register, but we don’t know how to get it out of AD7730. The ready pin looks like is not working propely and it takes age to “escape” , and ir “escape”, from while(digitalRead(rdy)!= LOW) {}, and the lst thing we noticed is in serial monitor doesn’t appear the value of result :~
Any help will be very apreciated.
It’s all I can remenber for now :smiley:
sorry for my bad english XD
And many thanks.

UP!
Hello everyone!
I’ve done the code from the data sheet (page 37), the simple one without the filter and dac register, but nothing happens =(, the ready pin is always in high state.
I forced pin 3(arduino pin, which receive the ready signal from ad7730) to go low, but nothing happens too. anyone, who worked with this IC could help me, please!

#define ss 10 //slave select
#define miso 12 
#define mosi 11
#define rdy 3


#include <SPI.h>

void setup()
{
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1);  
SPI.setClockDivider(SPI_CLOCK_DIV4); 
Serial.begin(9600);
delay(10);
}

void loop()
{
unsigned long result = 0;
SPI.transfer(0x02);
SPI.transfer(0x21); 
SPI.transfer(0x80); 
SPI.transfer(0x21); 
    
do
{
result = SPI.transfer(miso);
result = result<<8;
result = SPI.transfer(miso)+result ;
result = result<<8;
result = SPI.transfer(miso)+result ;
}while(rdy!=LOW);

Serial.print( result);
Serial.print("\n");
}

You need to read 24 bits from the data register to get the conversion result. That's 4 bytes of SPI, one to setup the transfer, 3 to read the register, all in the same SPI transaction. Forget the ready pin I think. You need to pass the correct value as the argument to spi.transfer (not miso!!!!).

MarkT:
You need to read 24 bits from the data register to get the conversion result. That's 4 bytes of SPI, one to setup the transfer, 3 to read the register, all in the same SPI transaction. Forget the ready pin I think. You need to pass the correct value as the argument to spi.transfer (not miso!!!!).

Many thanks I'm going try your tip right now!

Those still looking for information check out my post here Arduino and the AD7730 - Science and Measurement - Arduino Forum