Other Parts Discussed in Thread: PGA460
Hi all, Hi Akeem,
Since last week I am experimenting with the PGA460. UART communication works fine but in the final circuit there is no UART left so I chose to use SPI/USART instead.
I set up the following test code for UART communication in Arduino IDE and used a Leonardo board:
byte th[] = {0x55, 0x10, 0xB0, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0xB4, 0xA6, 0x90, 0x07, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8}; // threshold values
byte tn[] = {0x55, 0x04 , 0x00 , 0xFB}; // temperature and noise
byte tnr[] = {0x55, 0x06, 0xF9}; // temperature and noise result
void setup() {
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N2);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
ms_uart();
Serial.println("");
}
while (Serial1.available()) {
byte b = Serial1.read();
Serial.print(b, HEX);
Serial.print(" ");
}
}
void ms_uart(){
Serial1.write(th, sizeof(th));
Serial1.write(tn, sizeof(tn));
delay(10);
Serial1.write(tnr, sizeof(tnr));
}
This code works fine and gives me the following result which is plausible and has a correct checksum:
40 73 2D 1F
Distance measurements and data dumps are also OK.
Now I modified the code to work via SPI and connected MOSI to RX, MISO to TX and CLK to CLK:
#include <SPI.h>
byte th[] = {0x55, 0x10, 0xB0, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0xB4, 0xA6, 0x90, 0x07, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8}; // threshold
byte tn[] = {0x55, 0x04 , 0x00 , 0xFB}; // temperature and noise
byte tnr[] = {0x55, 0x06, 0xF9}; // temperature and noise result
byte buf[4];
SPISettings pga460_spi(1000000, LSBFIRST, SPI_MODE2);
void setup() {
Serial.begin(115200);
SPI.begin();
}
void loop() {
ms_spi();
Serial.println("");
delay(1000);
}
void ms_spi(){
SPI.beginTransaction(pga460_spi);
SPI.transfer(th, sizeof(th));
SPI.transfer(tn, sizeof(tn));
delay(10);
SPI.transfer(tnr, sizeof(tnr));
for(uint8_t i = 0; i < 4; i++){
buf[i] = SPI.transfer(0xFF);
}
SPI.endTransaction();
print_buffer();
}
void print_buffer(){
for(uint8_t i = 0; i < 4; i++){
Serial.print(buf[i], HEX);
Serial.print(" ");
}
}
As a result I get this:
73 2D FE FF
which is not plausible and has a wrong checksum AND only the first time it is executed gives me this result. All following results in the loop are [0 0 0 0].
The hardware is identical, I also tried with other controller boards (ESP32, ESP8366, Arduino Mega, Arduino UNO) , all with the same problem.
Does anyone have an idea what I am doing wrong here? I tried the hole day with different tweaks and tricks but nothing helps.
Thanks a lot,
Peter