Hello!
I am interested to friend ESP8266 with chip ATT7053BU. ATT7053BU is a chip that measure voltage, power of electricity. It has SPI interface and internal registers. Internal register 0x1B of the chip contains info about chip and it should be ATT7053BU. According docs for communication we need provide diagram like in the attachment ATT7053_Com_png.
I have decided to use standard library arduino and redefine pins.
Unfortunately I can't read the data. I always retrieve zero.
Try to understand what is the problem and maybe my code is not correct for ESP8266. Could you please give me direction to figire out what is the problem? Is my code correct for the SPI communication with the chip?
PS. ATT7053 always set 1 on the 17 pin that means that something wrong.
#include<SPI.h>
#define D0 16
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0
#define D4 2 // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3 // RX0 (Serial console)
#define D10 1 // TX0 (Serial console)
const int chipSelectPin = 15;
SPISettings settings(6000000, MSBFIRST, SPI_MODE1);
void setup() {
Serial.begin(9600);
pinMode(chipSelectPin, OUTPUT);
digitalWrite(chipSelectPin, HIGH);
}
void loop() {
Serial.println("read register 0x1B");
Serial.println(readRegister(0x1B, 3), BIN);
delay(10000);
}
unsigned int readRegister(byte thisRegister, int bytesToRead) {
byte inByte = 0; // incoming byte from the SPI
unsigned int result = 0; // result to return
Serial.print(thisRegister, BIN);
Serial.print("\t");
// SPI.beginTransaction(settings);
SPI.setFrequency(6000000);
SPI.begin();
// take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW);
delayMicroseconds(5);
// send the device the register you want to read:
SPI.transfer(thisRegister);
delayMicroseconds(5);
// send a value of 0 to read the first byte returned:
result = SPI.transfer(0x00);
// decrement the number of bytes left to read:
delayMicroseconds(5);
bytesToRead--;
// if you still have another byte to read:
while (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--;
delayMicroseconds(5);
}
// take the chip select high to de-select:
SPI.endTransaction();
digitalWrite(chipSelectPin, HIGH);
// return the result:
return (result);
}
ATT7053BU.pdf (543 KB)