I am using eval STPM34 for energy meter project. I am connecting ESP32 to STPM34 using UART. i.e. only Tx and Rx are used. I am writing ESP32 code using Arduino IDE. I cannot read any other register data
I am using eval STPM34 for energy meter project. I am connecting ESP32 to STPM34 using UART. i.e. only Tx and Rx are used.
I am writing ESP32 code using Arduino IDE.
When I use Serial2.read() function to read any register data, I always get "160 0 0 4" i.e. "A0 00 00 04"... These are contents of 0x00 register..
I can not read any other register, evenif I give right address.
Code is given below for immediate reference :-
(Note: using SPI, i am able to read all register, but using UART, I failed)
#define RXD2 16
#define TXD2 17
#define SYN 5 // SYN pin
#define CS 18 // Chip select pin
#define READ_DELAY 500 // delay after reading. value in MS
byte tabByte[4];
static long total;
int cnt;
void ActiveSync() {
/* digitalWrite(SYN, LOW);
delayMicroseconds(4); // waits for a 1 millisecond
digitalWrite(SYN, HIGH);
delay(1); */
}
//READING FROM STPM
void sendBufferContent(){
//knowing that it returns the LSByte first
Serial.println("Reading registers");
if(Serial2.available())
{
// while(Serial2.available() > 0 ) {
for (int i=0;i<4;i++){
tabByte[i]=Serial2.read();
Serial.println(tabByte[i]);
}
while(Serial2.available() > 0 ) {
Serial2.read();
}
//}
delay(READ_DELAY);
}
else{
Serial.println("NOT READING");
}
}
/*
format :
<Read Address><Write Address><bit[7-0]><bit[15-8]>
*/
void UART_Send_Data(int One, int Two, int Three, int Four) {
Serial2.write(One);
Serial2.write(Two);
Serial2.write(Three);
Serial2.write(Four);
//Serial2.write(0xFF);
// Serial2.write(0xff);
delay(READ_DELAY);
}
/*
DSCR: Making SPI con reg 2 to skip CRC check and rest values are default
0xFF : Read nothing
0x24 : Select SPI Control register 1 address lower word
0x25 : Select SPI Control register 1 address higher word
*/
void configUS_REG1(){
UART_Send_Data(0xFF, 0x24, 0x00, 0x00);
UART_Send_Data(0xFF, 0x25, 0x00, 0x00);
delay(100);
}
/*
DSCR : Making SPI con reg 1 to set baud 115200 and rest values are default
0xFF : Read nothing
0x26 : Select SPI Control register 2 address lower word
0x27 : Select SPI Control register 2 address higher word
0x8B : Baud rate is 115200
*/
void configUS_REG2(){
//UART_Send_Data(0xFF, 0x26, 0x8B, 0x00);
UART_Send_Data(0xFF, 0x26, 0x83, 0x06);
UART_Send_Data(0xFF, 0x27, 0x00, 0x00);
delay(100);
}
/*
DSCR : Configuration _ Reg DSP_CR3
0x04 : Read nothing
0x05 : Write in DSP CR3 higher word
0xE0 : Autolatch every 128 milli secs (at 7.8125khz)
0x44 : Remaining are default values
0x81 : Autolatch every 128 milli secs (at 7.8125khz)
*/
void configDSP_CR3(){
UART_Send_Data(0x04, 0x05, 0xE0, 0x44);
delay(100);
UART_Send_Data(0x05, 0x05, 0x81, 0x00);
delay(100);
}
void STPMReset(){
UART_Send_Data(0x05, 0x05, 0x00, 0x10);
/* Clearing all status resisters */
UART_Send_Data(0x20, 0x20, 0x00, 0x00);
UART_Send_Data(0x21, 0x21, 0x00, 0x00);
UART_Send_Data(0x22, 0x22, 0x00, 0x00);
UART_Send_Data(0x23, 0x23, 0x00, 0x00);
delay(100);
}
/*
DSCR : Configuration _ Reg DSP_CR1
0x00 : write in DSP CR1 lower word
0xA0 : internal reference enabled, default temperature compensation
*/
void configDSP_CR1(){
UART_Send_Data(0x00, 0x00, 0xA0, 0x00);
delay(100);
}
/*
DSCR : Configuration _ Reg DFE_CR1
0x18 : write in DFE_CR1 lower word
0x27 :
0x03 :
*/
void configDFE_CR1() {
UART_Send_Data(0x18, 0x00, 0x27, 0x03);
delay(100);
UART_Send_Data(0x19, 0x00, 0x27, 0x03);
delay(100);
}
/*
addr : Active register address
0xFF : Write nothing
*/
void readReg(int addr){
Serial.print("cnt = ");
Serial.println(cnt);
cnt++;
Serial.print("addess = ");
Serial.println(addr);
UART_Send_Data(addr, 0xFF, 0xFF, 0xFF);
delay(1000);
//MISO sends answer in next time frame
sendBufferContent();//reading now
ActiveSync();
addr += 2;
Serial.print("cnt = ");
Serial.println(cnt);
cnt++;
Serial.print("addess = ");
Serial.println(addr);
UART_Send_Data(addr, 0xFF, 0xFF, 0xFF);
delay(1000);
//MISO sends answer in next time frame
sendBufferContent();//reading now
ActiveSync();
}
/* Init STPM34 */
void STPMInitReset() {
pinMode(SYN, OUTPUT); // sets the digital pin SYN as output
pinMode(CS, OUTPUT); // sets the digital pin SYN as output
digitalWrite(SYN, LOW);
delay(1); // waits for a 1 millisecond
digitalWrite(SYN, HIGH);
delay(1); // waits for a 1 millisecond
digitalWrite(SYN, LOW);
delay(1); // waits for a 1 millisecond
digitalWrite(SYN, HIGH);
delay(1); // waits for a 1 millisecond
digitalWrite(SYN, LOW);
delay(1); // waits for a 1 millisecond
digitalWrite(SYN, HIGH);
delay(1); // waits for a 1 millisecond
digitalWrite(CS, LOW);
delay(1); // waits for a 1 millisecond
digitalWrite(CS, HIGH);
delay(1); // waits for a 1 millisecond
}
/* SETUP */
void setup() {
Serial.begin(115200);
Serial.println("Starting code 2");
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
cnt = 0;
// Reset STPM
STPMInitReset();
STPMReset();
/* Do register settings */
configUS_REG1(); // US_REG1
configUS_REG2(); // US_REG2
configDSP_CR1(); // DSP_CR1
configDSP_CR3(); // DSP_CR3
configDFE_CR1(); // DFE_CR1 = 0x03270327
// Delay before reading current register
delay(3000);
}
//SPI CONFIG AND DATA TRANSFER
void loop() {
static int ad = 0;
ad += 2;
readReg(ad);
delay(1000); // add delay of 10 minutes
}
