Pages: [1]   Go Down
Author Topic: Need help for reading data from ade7753 by using arduino uno  (Read 3688 times)
0 Members and 2 Guests are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi  i am doing my project on enery metering ic ade7753 but now i am stuck and not able to get data from ic i constantly get either 6 or 86 ot some time variable so please help me in so iam able to complete  it .


Code:
// spi master
//arduino spi pin
//10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK)

#include <SPI.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include "pins_arduino.h"
#include <LiquidCrystal.h>
#define SDRDY 9 // slave data ready
#define mackPin PINB3
 LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
byte t=0;
signed char AE=0;
unsigned char RE=0;
unsigned char Ipk=0;
long Irms=0;
unsigned char Vpk=0;
long Vrms=0;
int tmp=0;
byte rtnVal[2];
byte rtn1Val[1];
byte rtn2Val[2];
//byte r1=0;
//short j;
void setup() {
  pinMode(SDRDY, INPUT);
  SPI_MasterInit();
  setClockDivider(SPI_CLOCK_DIV16); 
  lcd.begin(16,2);
  Serial.begin(9600);
}

void loop()
{
 
 
   //---------------------------------------------- Vrms--------------------------------------------------------------------//
  digitalWrite(SS, LOW);
 
 
   t=SPI_MasterTransmit(0x17);
   
  //block when slave data is not ready (not write to SPDR yet) 
  while (digitalRead(SDRDY) == LOW) //;
{
//Vrms = SPI_MasterReceive();

rtn2Val[0] = SPI_MasterReceive();
  rtn2Val[1] = SPI_MasterReceive();
 rtn2Val[2] = SPI_MasterReceive();
}

  // assemble into long type
  Vrms = Vrms | rtn2Val[0];
  Vrms = Vrms >> 8;
  Vrms = Vrms | rtn2Val[1];
  Vrms = Vrms >> 8;
  Vrms = Vrms | rtn2Val[2];
 
//Serial.write(firstSensor);
  digitalWrite(SS, HIGH);
  Serial.print("  ,Vrms");
  // Serial.print(Vrms);
Serial.println(Vrms, DEC);
  delay(100);
 
     
  lcd.setCursor(6,1);
    lcd.print("Vr=");
   lcd.print(Vrms);

 
  delay(100);
 
 
     //----------------------------------------------irms--------------------------------------------------------------------//
  digitalWrite(SS, LOW);
 
 
   t=SPI_MasterTransmit(22);
   
  //block when slave data is not ready (not write to SPDR yet) 
  while (digitalRead(SDRDY) == LOW) ;

//Vrms = SPI_MasterReceive();

rtnVal[0] = SPI_MasterReceive();
  rtnVal[1] = SPI_MasterReceive();
  rtnVal[2] = SPI_MasterReceive();


  // assemble into long type
  Irms = Irms | rtnVal[2];
  Irms = Irms >> 8;
  Irms = Irms | rtnVal[1];
  Irms = Irms >> 8;
  Irms = Irms | rtnVal[0];

//Serial.write(firstSensor);
  digitalWrite(SS, HIGH);
  Serial.print("  ,Irms");
  // Serial.print(Vrms);
Serial.println(Irms, DEC);
  delay(100);
 
     
  lcd.setCursor(1,0);
    lcd.print("Vr=");
   lcd.print(Irms);

 
  delay(100);
 delay(100);
 
 
      //---------------------------------------------- temp--------------------------------------------------------------------//
  digitalWrite(SS, LOW);
 
 
   t=SPI_MasterTransmit(61);
   
  //block when slave data is not ready (not write to SPDR yet) 
  while (digitalRead(SDRDY) == LOW) ;

//Vrms = SPI_MasterReceive();

rtn1Val[0] = SPI_MasterReceive();
  rtn1Val[1] = SPI_MasterReceive();
 // rtnVal[2] = SPI_MasterReceive();


  // assemble into long type
 // tmp = tmp | rtn1Val[0];
 //tmp = tmp >> 8;
  //tmp = tmp | rtn1Val[1];
 // Vrms = Vrms >> 8;
 // Vrms = Vrms | rtnVal[2];
 
//Serial.write(firstSensor);
  digitalWrite(SS, HIGH);
  Serial.print("  ,tmp");
  // Serial.print(Vrms);
Serial.println(tmp, DEC);
  delay(100);
 
     
  lcd.setCursor(6,1);
    lcd.print("Vr=");
   lcd.print(Vrms);

 
  delay(100);

}

// copy from http://arduino.googlecode.com/svn
// SPI.cpp, SPI.h
void SPI_MasterInit(void)
{
  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(SS, OUTPUT);

  digitalWrite(SCK, LOW);
  digitalWrite(MOSI, LOW);
  digitalWrite(SS, HIGH);

  // Warning: if the SS pin ever becomes a LOW INPUT then SPI
  // automatically switches to Slave, so the data direction of
  // the SS pin MUST be kept as OUTPUT.
  SPCR |= _BV(MSTR);
  SPCR |= _BV(SPE);
 
  //default SPI speed is osc/2 (in arduino uno, 4Mhz, too fast)
  //change it to osc/16
  setClockDivider(SPI_CLOCK_DIV16); 
}

byte SPI_MasterTransmit(byte cData)
{
  /* Start transmission */
  SPDR = cData;
  /* Wait for transmission complete */
  while(!(SPSR & (1<<SPIF)))
  ;
  return SPDR;
}

byte SPI_MasterReceive(void)
{
  SPDR = 0x11;
  /* Wait for reception complete */
  while(!(SPSR & (1<<SPIF)))
  ;
   
  return SPDR; 
}

void setClockDivider(byte rate)
{
  SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
  SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | (rate & SPI_2XCLOCK_MASK);
}

please tell me where i am making mistake
thanks in advance
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23718
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Got a link to a datasheet?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.analog.com/static/imported-files/data_sheets/ADE7753.pdf
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23718
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code is too complicated.
Use this approach, make it function if you want.
See pages 49 to 58 for the write & write/read sequence, and the meaning of the registers.
commandByte will have the info needed to read or write a register, and which register is being accessed.
Code:
void setup(){
SPI.begin();
SPI.divisor(4); //  or whatever the command is to use SPI = 2 MHz or 4 MHz
}

void loop(){
digitaWrite (ssPin, LOW);
SPI.transfer (commandByte);  // has bits set for a read or write command, and the register address
regRead = SPI.transfer(dummyByte); // read a register
//SPI.transfer (registerData); // or data to write into a register
digitalWrite (ssPin, HIGH);
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for helping crossroads
 i changed my code to this but not get correct values but i am unable to read 12bit ,24 bit from spi
 
Code:
#include <SPI.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include "pins_arduino.h"
#include <LiquidCrystal.h>
#define SDRDY 9 // slave data ready
#define mackPin PINB3
 LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
byte t=0;
signed char AE=0;
unsigned char RE=0;
unsigned char Ipk=0;
long Irms=0;
unsigned char Vpk=0;
long Vrms=0;
int tmp=0;
byte rtnVal[2];
byte rtn1Val[1];
byte rtn2Val[2];
//byte r1=0;
//short j;
void setup() {
  pinMode(SDRDY, INPUT);


  lcd.begin(16,2);
  Serial.begin(9600);
  SPI.begin();

SPI.setClockDivider(4) ;
}


void loop(){
digitalWrite(SS, HIGH);  
SPI.transfer (0x17);  // has bits set for a read or write command, and the register address
rtnVal[0] = SPI.transfer(0);
rtnVal[1] = SPI.transfer(0);
rtnVal[2] = SPI.transfer(0);// read a register
//SPI.transfer (registerData); // or data to write into a register
digitalWrite(SS, HIGH);  
Vrms = Vrms | rtn2Val[0];
  Vrms = Vrms >> 8;
  Vrms = Vrms | rtn2Val[1];
  Vrms = Vrms >> 8;
  Vrms = Vrms | rtn2Val[2];
 Serial.print("  ,Vrms");
  // Serial.print(Vrms);
Serial.println(Vrms, DEC);
  delay(100);
  
}
out like this
  ,Vrms15
  ,Vrms13
  ,Vrms111
  ,Vrms209
  ,Vrms51
  ,Vrms149
  ,Vrms247
  ,Vrms89
  ,Vrms187
  ,Vrms29
  ,Vrms127
  ,Vrms225
  ,Vrms67
  ,Vrms164
  ,Vrms6
« Last Edit: April 13, 2014, 11:28:18 am by AA_KHAN » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23718
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
digitalWrite(SS, HIGH); 
SPI.transfer (0x17);  // has bits set for a read or write command, and the register address
rtnVal[0] = SPI.transfer(0);
rtnVal[1] = SPI.transfer(0);
rtnVal[2] = SPI.transfer(0);// read a register
//SPI.transfer (registerData); // or data to write into a register
digitalWrite(SS, HIGH);
Need to start with
digitalWrite(SS, LOW); // Low starts SPI transfer,  High ends it.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

but i still got same result
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

one more thing if i changed these lines to
Code:
Vrms = Vrms | rtn2Val[2];
  Vrms = Vrms << 8;
  Vrms = Vrms | rtn2Val[1];
  Vrms = Vrms << 8;
  Vrms = Vrms | rtn2Val[0];
i got this type of result
 ,Vrms9961600
  ,Vrms16384128
  ,Vrms14418048
  ,Vrms12517504
  ,Vrms10551424
  ,Vrms8585344
  ,Vrms15007872
  ,Vrms13041792
  ,Vrms11141248
  ,Vrms9175168
  ,Vrms15597696
  ,Vrms13631616
  ,Vrms11665536
  ,Vrms9764992
 its strange for me  i will be thankfull if you help in this matter
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23718
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try adding this between these two lines:

SPI.transfer (0x17);  // has bits set for a read or write command, and the register address
delayMicroseconds(4); // t9, page 6
rtnVal[0] = SPI.transfer(0);

Other than that, the datasheet is quite long - there are things to set up for calibration, gains, etc.
You need to understand if those need to be setup for whatever else you have connected.  I doubt I can do that in one afternoon not knowing anything else about your project.

Look at the three bytes you get back and put them together by hand, see if the result makes sense, and then check that your math is doing the same.  I like to look at the three values returned in HEX format to start, makes it a lot easier to see if the shifting and ORing is going correct when the three bytes are put together.
Code:
unsigned long Vrms = (rtn2Val[2]<<16) | (rtn2Val[1]<<8) | rtn2Val[0]; // extra ( ) ensure it does what you expect
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your time i made changes in code now it give some thing like it
Vrms4294967054//dec
E//byte one in hex
FF//byte 2 in hex
81//byte 3 in hex

with this code
Code:
Vrms = (rtnVal[2]<<16) | (rtnVal[1]<<8) | rtnVal[0];
 Serial.print("  ,Vrms");
  // Serial.print(Vrms);
Serial.println(Vrms, DEC);
Serial.println(rtnVal[0], HEX);
Serial.println(rtnVal[1], HEX);
Serial.println(rtnVal[2], HEX);
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23718
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try the same delay between each SPI transfer. I don't think its needed with 125nS clock period, but it might help. I don't why 2nd byte is 0 all the time.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
SPI.transfer (0x17);  // has bits set for a read or write command, and the register address
delayMicroseconds(4);
rtnVal[0] = SPI.transfer(0);
delayMicroseconds(2);
rtnVal[1] = SPI.transfer(0);
delayMicroseconds(2);
rtnVal[2] = SPI.transfer(0);// read a register
//SPI.transfer (registerData); // or data to write into a register
digitalWrite(SS, HIGH); 
 Vrms = (rtnVal[2]<<16) | (rtnVal[1]<<8) | rtnVal[0];
 Serial.print("  ,Vrms");
  // Serial.print(Vrms);
Serial.println(Vrms, HEX);
Serial.println(rtnVal[0], HEX);
Serial.println(rtnVal[1], HEX);
Serial.println(rtnVal[2], HEX);
now i got this result
VrmsFFFFFF0E
E
FF
80

Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23718
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

These need to be (4)

delayMicroseconds(2);

That's the min that works.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi thanks again it gives same result even if i changed to 4
by looking binary out put its like that this time its cintantly 255 for midlr byte
Vrms11111111111111111111111100001110
1110//1
11111111//2
10000010//3
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23718
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like you have leftover data in Vrms. Try this:

digitalWrite(SS, HIGH); 
unsigned long Vrms = 0;
 Vrms = (rtnVal[2]<<16) | (rtnVal[1]<<smiley-cool | rtnVal[0];

May need to make these unsigned long also before shifting them:
rtnVal[2]  at least this one
rtn(Val[1] maybe an int
rtnVal[0]
Or let the code do it:
Vrms = (rtnVal[2] * 16) + (rtnVal[1] * smiley-cool + rtnVal[0];

I never get that right the first time or two, takes a me couple tries with type casting & stuff.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: