No se quien me pregunto hace tiempo por privado el como se podia conectar una celula de carga al arduino. Hasta ahora lo que se me podia ocurrir era usar un operacional tipo INA125 junto con ADC de 24 bit. Pues bien otra solucion seria usar un CS5532 con interfaz SPI.
Os dejo el datasheet y ejemplo de uso.
#include <SPI.h>
const byte CHANNELREG011 = 0b00110000;
const byte CHANNELREG012 = 0b01000000;
const byte CHANNELREG013 = 0b00000000;
const byte CHANNELREG014 = 0b00000000;
const byte CHANNELREG021 = 0b01110000;
const byte CHANNELREG022 = 0b01000000;
const byte CHANNELREG023 = 0b00000000;
const byte CHANNELREG024 = 0b00000000;
const byte CONFIGREG11 = 0b00000010;
const byte CONFIGREG12 = 0b00000000;
const byte CONFIGREG13 = 0b00000000;
const byte CONFIGREG14 = 0b00000000;
const byte READ01 = 0b10000000;
const byte READ02 = 0b10001000;
const byte CONF_RESET1 = 0b00100000;
const byte CONF_RESET2 = 0b00000000;
const byte CONF_RESET3 = 0b00000000;
const byte CONF_RESET4 = 0b00000000;
const byte COMMAND_CONFGREG = 0b00000011;
const byte COMMAND_CHANNELREG1 = 0b00000101;
const byte COMMAND_CHANNELREG2 = 0b00010101;
const byte COMMAND_READCONF = 0b00001011;
byte CONF_READED = 0;
byte CONF_READED2 = 0;
byte CONF_READED3 = 0;
byte CONF_READED4 = 0;
byte CONF_TMP1 = 0, CONF_TMP2 = 0, CONF_TMP3=0;
void ADreset(){
//digitalWrite(SS,LOW);
for(int i = 0;i<15;i++){
SPI.transfer(0xFF);}
SPI.transfer(0xFE);
delay(50);
SPI.transfer(COMMAND_CONFGREG);
SPI.transfer(CONF_RESET1);
SPI.transfer(CONF_RESET2);
SPI.transfer(CONF_RESET3);
SPI.transfer(CONF_RESET4);
SPI.transfer(CONF_RESET2);
SPI.transfer(CONF_RESET2);
SPI.transfer(CONF_RESET2);
SPI.transfer(CONF_RESET2);
SPI.transfer(COMMAND_READCONF);
CONF_READED2 = SPI.transfer(0x00);
CONF_TMP1 = SPI.transfer(0x00);
CONF_TMP2 = SPI.transfer(0x00);
CONF_TMP3 = SPI.transfer(0x00);
CONF_READED2 = CONF_READED2 << 8;
CONF_READED = CONF_READED2 | CONF_TMP1;
CONF_READED = CONF_READED << 8;
CONF_READED = CONF_READED | CONF_TMP2;
CONF_READED = CONF_READED << 8;
CONF_READED = CONF_READED | CONF_TMP3;
Serial.println(CONF_READED,BIN);
// CONF_READED = CONF_READED << 8;
// CONF_READED = CONF_READED | CONF_TMP;
// CONF_READED = CONF_READED << 8;
// CONF_READED = CONF_READED | CONF_TMP;
// CONF_TMP = SPI.transfer(0x00);
// CONF_READED = CONF_READED << 8;
// CONF_READED = CONF_READED | CONF_TMP;
// digitalWrite(SS,HIGH);
}
void ADConfig(){
// digitalWrite(SS, LOW);
SPI.transfer(COMMAND_CONFGREG);
SPI.transfer(CONFIGREG11);
SPI.transfer(CONFIGREG12);
SPI.transfer(CONFIGREG13);
SPI.transfer(CONFIGREG14);
delay(10);
SPI.transfer(COMMAND_CHANNELREG1);
SPI.transfer(CHANNELREG011);
SPI.transfer(CHANNELREG012);
SPI.transfer(CHANNELREG013);
SPI.transfer(CHANNELREG014);
delay(10);
SPI.transfer(COMMAND_CHANNELREG2);
SPI.transfer(CHANNELREG021);
SPI.transfer(CHANNELREG022);
SPI.transfer(CHANNELREG023);
SPI.transfer(CHANNELREG024);
delay(10);
// digitalWrite(SS, HIGH);
CONF_READED2 = SPI.transfer(COMMAND_READCONF);
CONF_TMP1 = SPI.transfer(0x00);
CONF_TMP2 = SPI.transfer(0x00);
CONF_TMP3 = SPI.transfer(0x00);
CONF_READED = CONF_READED2 << 8;
CONF_READED = CONF_READED | CONF_TMP1;
CONF_READED = CONF_READED << 8;
CONF_READED = CONF_READED | CONF_TMP2;
CONF_READED = CONF_READED << 8;
CONF_READED = CONF_READED | CONF_TMP3;
Serial.println(CONF_TMP1,BIN);
Serial.println(CONF_TMP2,BIN);
Serial.println(CONF_READED,BIN);
}
// pins used for the connection with the sensor
// the other you need are controlled by the SPI library):
void setup() {
Serial.begin(9600);
// start the SPI library:
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI.setDataMode(SPI_MODE2);
// initalize the data ready and chip select pins:
ADreset();
ADConfig();
delay(100);
}
int SerialRead = 0;
int SerialTMP = 0;
int incomingByte = 0;
void loop() {
Serial.println("AD CS5532 - Programa de teste ");
Serial.println("Para ler canal AD 01, precione 1 ");
Serial.println("Para ler canal AD 02, precione 2 ");
Serial.println("Para ler REG_CONF, precione 3 ");
while(SerialRead == 0){
//Serial.println(SerialTMP);
if (Serial.available() > 0) {
SerialRead = Serial.read();
Serial.print("I received: ");
Serial.println(SerialRead, DEC);
}
switch(SerialRead){
case 49:
readAD1();
delay(100);
break;
case 50:
readAD2();
delay(100);
break;
case 51:
Serial.println(CONF_READED,BIN);
Serial.println(CONF_READED2,BIN);
break;
}
SerialRead = 0;
}
}
//Read from or write to register from the SCP1000:
unsigned int readAD1() {
byte inByte = 0; // incoming byte from the SPI
unsigned int result = 0; // result to return
unsigned int result_blank = 0; // result to return
Serial.print("Comando Enviado: ");
Serial.println(READ01, BIN);
// digitalWrite(SS, LOW);
SPI.transfer(READ01);
// send a value of 0 to read the first byte returned:
result_blank = SPI.transfer(0x00);
// decrement the number of bytes left to read:
result = SPI.transfer(0x00);
// if you still have another byte to read:
for(int i = 0;i<2;i++) {
// 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:
}
// take the chip select high to de-select:
//digitalWrite(SS, HIGH);
// return the result:
Serial.print("Valor recebido: ");
Serial.println(result,BIN);
;
return(result);
}
unsigned int readAD2() {
byte inByte = 0; // incoming byte from the SPI
unsigned int result = 0; // result to return
unsigned int result_blank = 0; // result to return
Serial.print("Comando Enviado: ");
Serial.println(READ02, BIN);
// digitalWrite(SS, LOW);
SPI.transfer(READ02);
// send a value of 0 to read the first byte returned:
result_blank = SPI.transfer(0x00);
// decement the number of bytes left to read:
result = SPI.transfer(0x00);
// if you still have another byte to read:
for(int i = 0;i<2;i++) {
// 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:
}
// take the chip select high to de-select:
//digitalWrite(SS, HIGH);
// return the result:
Serial.print("Valor recebido: ");
Serial.println(result,BIN);
return(result);
}
Saludos.
CS5532.pdf (731 KB)