Hello,
unfortunately I didn't find any introuction thread for new members, therefore I hope it' OK to directly ask my question.
I' like to interface via SPI with the following sensor PIHER PST360-1S-C0000-ERA05K
(Instructions for use are linked)
I already made a shield to be able to connect MOSI/MISO according to the datasheet.
Now I need the Code istelf, unfortunately it seems that no example code really fits.
(of those I found)
My first problem in writing the code is, that I don't exactly know how to transform this requirement from the Manual in Code :
"The Master should send AAh (55h in case of inverting transistor) followed by 9 bytes FFh. The Slave will answer with two bytes FFh followed by
4 data bytes and 4 bytes FFh."
I'm quite sure the data should be sent via SPI.transfer(buffer, size) in the buffer-Array, but how can I be sure that it is one byte per transfer. Next I wonder which kind of data "AAh" is, it rather looks like ASCII Code than a HEX-Number which I would assume to be sent
It would be great if you could help me a bit with my confusion
Many thanks in advance
Regards
digitalWrite (ssPin, LOW);
byte0back = SPI.transfer (0xAA); // AA out, FF back
byte1back = SPI.transfer (0xFF); // FF out, FF back
byte2back = SPI.transfer (0xFF); // FF out, databyte0 back?
byte3back = SPI.transfer (0xFF); // FF out, FF back?
byte4back = SPI.transfer (0xFF); // FF out, databyte1 back?
byte5back = SPI.transfer (0xFF); // FF out, FF back
byte6back = SPI.transfer (0xFF); // FF out, databyte2 back?
byte7back = SPI.transfer (0xFF); // FF out, FF back
byte8back = SPI.transfer (0xFF); // FF out, databyte3 back?
byte9back = SPI.transfer (0xFF); // FF out, FF back
digitalWrite (ssPin, HIGH);
You'll have to review the bytes coming back to see what that actual order is.
You can use
Serial.println (byte0back, HEX); // 1 to FF will be printed.
I think you'll find leading 0s will get dropped.
c_makes:
Next I wonder which kind of data "AAh" is, it rather looks like ASCII Code than a HEX-Number which I would assume to be sent
It would be great if you could help me a bit with my confusion
Many thanks in advance
Regards
You'll have to understand the below:
In mathematics and computing, hexadecimal (also base 16, or hex) is a positional system that represents numbers using a base of 16. Unlike the common way of representing numbers with ten symbols, it uses sixteen distinct symbols, most often the symbols "0"–"9" to represent values zero to nine, and "A"–"F" (or alternatively "a"–"f") to represent values ten to fifteen. Hexadecimal - Wikipedia
before you can understand this:
"The Master should send AAh (55h in case of inverting transistor) followed by 9 bytes FFh. The Slave will answer with two bytes FFh followed by
4 data bytes and 4 bytes FFh."
Many thanks for your answers.
I hope my questions aren't too stupid
I gave it a try according to crossroads advice and somehow it seems to work.
Before I only got 0 printed in the serial Monitor, now that changed to a repeating:
AA
FF
FF
FF
FF
FF
FF
FF
FF
FF
I think that's the data sent , cause disconnecting the mosi line brings me back to 0s printed on the serial monitor.
My first thought was that it might be a problem with the ss-pin, cause in the instructions that pin is constantly set low during operation, regardless of which side is transmitting.
Therefore I checked the wiring again and changed to a digitalWrite (ssPin, LOW) in my loop with no change on the readings.
Unfortunately I've no protocol analyzer to see what's transmitted on the miso line, to see if the slave really reacts.
Do you have any further idea ?
c_makes:
Do you have any further idea ?
Posting the code you are currently using could be helpful...
Yeah, might be a really good advice 
#include <SPI.h>
const int ssPin = 53;
void setup() {
delay(20);
Serial.begin(9600);
pinMode(ssPin, OUTPUT);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV4);
SPI.setDataMode(SPI_MODE1);
SPI.setBitOrder(MSBFIRST);
}
void loop() {
digitalWrite (ssPin, LOW);
delay(20);
digitalWrite (ssPin, HIGH);
delay(20);
digitalWrite (ssPin, LOW);
byte byte0back = SPI.transfer (0xAA);
byte byte1back = SPI.transfer (0xFF);
byte byte2back = SPI.transfer (0xFF);
byte byte3back = SPI.transfer (0xFF);
byte byte4back = SPI.transfer (0xFF);
byte byte5back = SPI.transfer (0xFF);
byte byte6back = SPI.transfer (0xFF);
byte byte7back = SPI.transfer (0xFF);
byte byte8back = SPI.transfer (0xFF);
byte byte9back = SPI.transfer (0xFF);
Serial.println (byte0back, HEX);
Serial.println (byte1back, HEX);
Serial.println (byte2back, HEX);
Serial.println (byte3back, HEX);
Serial.println (byte4back, HEX);
Serial.println (byte5back, HEX);
Serial.println (byte6back, HEX);
Serial.println (byte7back, HEX);
Serial.println (byte8back, HEX);
Serial.println (byte9back, HEX);
}
I hope you have gone through the datasheet you shared yourself coz it has quite a few timing requirements:
No idea if this will work but based of those timing I think something like this might just work IMHO:
#include <SPI.h>
const int ssPin = 53;
uint8_t command[10] = {0xAA,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
uint8_t reply[10];
void setup() {
delay(20); //give time for slave to start-up
Serial.begin(9600);
SPI.beginTransaction(SPISettings(125000, MSBFIRST, SPI_MODE1)); //125kHz SCLK (taking t1 as 8us)
pinMode(ssPin, OUTPUT);
digitalWrite(ssPin, HIGH);
}
void loop() {
delayMicroseconds(1600); //t5: ssPin=Hi time where it’s guaranteed that a frame re-synchronizations will be started.
digitalWrite (ssPin, LOW);
delayMicroseconds(8); //t6 timing.
for(uint8_t i=0; i<10;++i){
reply[i] = SPI.transfer (command[i]);
delayMicroseconds(48); //t2/t7 the minimum time between any other byte..
}
digitalWrite (ssPin, HIGH);
for(uint8_t i=0; i<10;++i){
Serial.println (reply[i], HEX);
}
delay(1000); //arbitrary timing to give time to read serial monitor! ;)
}
hope that helps....