AnsweredAssumed Answered

AD7799 read value from loadcell using Arduino mega 2560

Question asked by Zinedine on Nov 12, 2014
Latest reply on Nov 20, 2014 by jcolao

Hi everyone,

I'm a new fresher. My project is read value from loadcell using AD7799, I'm using Arduino control everything. But I have many trouble. I hope everyone can help me.

- AD7799 datasheet: http://www.analog.com/static/imported-files/data_sheets/AD7798_7799.pdf

- This is a analog device 24 bit.I'm using SPI communication.

- Attach is shematic board AD7799 that I designed

Below is my project in arduino: how about display or print value read in to LCD 16x2 or window arduino?

 

#include <Stdio.h>

#include<stdlib.h>

#include<SPI.h>

#include <LiquidCrystal.h>

 

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

 

 

#define ulong unsigned long

#define uint unsigned int

#define uchar unsigned char

 

 

#define AD_CS 6

#define AD_SCK 7

#define AD_DIN 8

#define AD_DOUT 9

#define AD_RDY 9

 

 

void WriteByteToAd7799(unsigned char WriteData)

{

  pinMode(AD_SCK,OUTPUT);

  pinMode(AD_CS,OUTPUT);

  pinMode(AD_DIN,OUTPUT);

    unsigned char i;

    digitalWrite(AD_CS,LOW);//reset bit latch

    for(i=0;i<8;i++)  

    {  

       // delayMicroseconds(10);  

        digitalWrite(AD_SCK,LOW);

        //delayMicroseconds(10);  

        if(WriteData&0x80)

        digitalWrite(AD_DIN,1);

        else digitalWrite(AD_DIN,0);  

        WriteData=WriteData<<1 ;  

        //delayMicroseconds(10); 

        digitalWrite(AD_SCK,HIGH);

        delayMicroseconds(10); 

    }  

     digitalWrite(AD_CS,HIGH);//set bit latch

   

}

unsigned char ReadByteFromAd7799(void)

{

  pinMode(AD_CS,OUTPUT);

  pinMode(AD_SCK,OUTPUT);

  //pinMode(AD_DOUT,INPUT);

  unsigned char i;

  unsigned char ReadData;

  digitalWrite(AD_CS,LOW);

  ReadData=0;

  for(i=0;i<8;i++)

    {

      //delayMicroseconds(10);

      digitalWrite(AD_SCK,LOW);

      //delayMicroseconds(10);

      ReadData=ReadData<<1;

        if(AD_DOUT)ReadData=ReadData+1;

      //delayMicroseconds(10);

      digitalWrite(AD_SCK,HIGH);

      //delayMicroseconds(10);

      }

     // digitalWrite(AD_DOUT,1);

      digitalWrite(AD_CS,HIGH);

      return(ReadData);

}

void WaiteRDY(void)

{

  unsigned int iint;

  iint=0;

  while(AD_RDY)

  {

    iint++;

    if(iint>65530)

    {

      //reset Ad7799

            WriteByteToAd7799(0xff);  

            WriteByteToAd7799(0xff);  

            WriteByteToAd7799(0xff);  

            WriteByteToAd7799(0xff);  

            AD7799_Ini();  

            break ;

    }

 

  }

}

void AD7799_Ini(void)

{

  digitalWrite(AD_CS,LOW);

  //gain the 128

  WriteByteToAd7799(0x10);//communication register setting

  WriteByteToAd7799(0x37);//gain of 128

  WriteByteToAd7799(0x30);//channel 0

  //write mode register

  WriteByteToAd7799(0x08);//write to the communications register set operation for write mode register

  WriteByteToAd7799(0x80);

  WriteByteToAd7799(0x0A);

  WaiteRDY();//wait for RDY pin to go low

  //Write mode register is initialized to the full value of the calibration

  WriteByteToAd7799(0x08);//write to the communications register set operation for write mode register

  WriteByteToAd7799(0xA0);

  WriteByteToAd7799(0x0A);

  WaiteRDY();//wait for RDY pin to go Low

  //Mode 0, continuous Mode frequency 16.7Hz

  WriteByteToAd7799(0x08);

  WriteByteToAd7799(0x00);

  WriteByteToAd7799(0x0A);

}

unsigned long ReadAd7799ConversionData(void)

{

  unsigned long ConverData;

  unsigned char ADSAT;

  unsigned char ErrNUM=0;

  WaiteRDY();

  WriteByteToAd7799(0x40);//write to the communications register set an operating STATUS register read status

  //WriteByteToAd7799(0xff);

  ADSAT = ReadByteFromAd7799();

  while((ADSAT&0x40)||(!ADSAT&0x08))

  {

    WriteByteToAd7799(0xff);

    WriteByteToAd7799(0xff);

    // Prevent confusion timing resynchronization    

    WriteByteToAd7799(0xff);  

    WriteByteToAd7799(0xff);  

    AD7799_Ini();

    WaiteRDY();

    WriteByteToAd7799(0x40);//write to the communications register set an operating STATUS register read status

    WriteByteToAd7799(0xff);//pseudo write to the communications register clock for the Read Status Register

    //ADSAT=ReadByteFromAd7799();//read the rêcived data

    ADSAT=ReadByteFromAd7799();

    ErrNUM++;

    if(ErrNUM>5)break;

  }

    WriteByteToAd7799(0x58);// write to the communications register set an operating continuously read data register

    WaiteRDY();

  if(!AD_RDY)

  {

  ConverData=0;

  //Read Conversion Result from AD7799's Data Register

  ConverData=ReadByteFromAd7799();

  ConverData=ConverData<<8;

  //WriteByteToAd7799 (0xff); // pseudo write to the communications register clock for the read data register Register

  ConverData=ReadByteFromAd7799()+ConverData;

  //ConverData=ConverData+SPDR;

  ConverData=ConverData<<8;

  //WriteByteToAd7799 (0xff); //pseudo write to the communications register clock for the read data register Register

  ConverData=ReadByteFromAd7799()+ConverData;

  //ConverData=ConverData+SPDR;

  }

  if(ErrNUM>5)return(0);

  else return ConverData;

}

void setup() {

   Serial.begin(115200);

  //SPCR = 0x5E ;//SPI control registers, interrupt disable the SPI is enabled, the previous high, host mode, the clock idles high, trailing edge trigger Shift, clock divider 64

  //SPSR = 0x00 ;//clear interrupt flag

  //lcd.begin(16, 2);

  // Print a message to the LCD.

  //lcd.print("ANALOG READING:");

}

//UCIL temp;

unsigned char temp1;

void loop() {

while(1)

{

temp1=ReadAd7799ConversionData();

//lcd.setCursor(1,0);

//lcd.print(temp1);

//lcd.print("         ");

Serial.println(temp1);

}

}

Attachments

Outcomes