Hello!
I am developing a project that uses the Arduino UNO communicating with an Analog Devices ADE7753 chip.
I'm having some doubts related to data types and the SPI communication at all.
I'll post my code:
/*
ANALOG DEVICES ADE7753
*Circuito*
SCLK: pin 13
MOSI-DIN: pin 11
MISO-DOUT: pin 12
SS: pin 10
*/
#include <SPI.h>
//REGISTRADORES ADE7753
#define MODE 0x09
#define STATUS 0x0B
#define RSTSTATUS 0x0C
#define IRMS 0x16
#define VRMS 0x17
const byte READ = 0x00; // ADE7753 read command
const byte WRITE = 0x80; // ADE7753 write command
const int chipSelectPin = 10;
void setup() {
Serial.begin(9600);
// start the SPI library:
SPI.begin();
SPI.setDataMode(SPI_MODE1);
// initalize the data ready and chip select pins:
pinMode(chipSelectPin, OUTPUT);
// give the sensor time to set up:
delay(100);
}
void loop() {
int i;
long v=0;
int resultado;
float real;
readRegister (VRMS, 3);
for (i=0;i<50;i++){
readRegister (RSTSTATUS, 2);
while( ! (readRegister (STATUS, 2) & 0x0010) ){
// Serial.println("Esperando...");
}
v = v + readRegister (VRMS, 3);
}
resultado = v/50;
real = resultado / 12.551569;
Serial.print("Resultado[V]: ");
Serial.println(real);
/*
readRegister (IRMS, 3);
for (i=0;i<50;i++){
readRegister (RSTSTATUS, 2);
while( ! (readRegister (STATUS, 2) & 0x0010) ){
// Serial.println("Esperando...");
}
v = v + readRegister (IRMS, 3);
}
resultado = v/50;
real = resultado / 1605.5;
Serial.print("Resultado[A]: ");
Serial.println(real);
*/
}
unsigned long readRegister(byte thisRegister, int bytesToRead) {
byte inByte = 0; // incoming byte from the SPI
unsigned long result = 0; // result to return
// Serial.print("Antes: ");
// Serial.println(thisRegister, BIN);
byte dataToSend = thisRegister | READ;
digitalWrite(chipSelectPin, LOW);
SPI.transfer(dataToSend);
// send a value of 0 to read the first byte returned:
result = SPI.transfer(0x00);
// decrement the number of bytes left to read:
bytesToRead--;
// if you still have another byte to read:
if (bytesToRead > 0) {
// shift the first byte left, then get the second byte:
result = result << 8;
inByte = SPI.transfer(0x00);
// combine the byte you just got with the previous one:
result = result | inByte;
// decrement the number of bytes left to read:
bytesToRead--;
}
// take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH);
// return the result:
return (result);
}
void writeRegister8(byte reg, byte data){
reg |= WRITE;
delayMicroseconds(5);
digitalWrite(chipSelectPin, LOW);
SPI.transfer((byte)reg); //register selection
delayMicroseconds(5);
SPI.transfer((byte)data);
delayMicroseconds(5);
digitalWrite(chipSelectPin, HIGH);
}
void writeRegister16(byte reg, unsigned int data){
byte data0=0,data1=0;
reg |= WRITE;
//split data
data0 = (byte)data;
data1 = (byte)(data>>8);
Serial.print("data0: ");
Serial.println(data0, BIN);
Serial.print("data1: ");
Serial.println(data1, BIN);
delayMicroseconds(5);
digitalWrite(chipSelectPin, LOW);
SPI.transfer((byte)reg);
delayMicroseconds(5);
//data send, MSB first
SPI.transfer((byte)data1);
delayMicroseconds(5);
SPI.transfer((byte)data0);
delayMicroseconds(5);
digitalWrite(chipSelectPin, HIGH);
}
It seems that i am reading the right values... Is there anything wrong?
Is there a way to write a unique function like "readRegister", but with the write command?
Thanks!!