I'm trying to combine a few libraries I have to save space, and do a little optimizing in the process with direct port access instead of digitalReads, etc...
The library provided for the Max31856 works, but it doesn't use the SPI library at all, so to start with, I'm trying to just do the bare bones... Write to the registers, and read back from them...
Datasheet here https://datasheets.maximintegrated.com/en/ds/MAX31856.pdf
The long and the short of it is if bit 7 of the first byte is set, you have a write to the address at bits 6:0.. if it's unset, you will read from that address
What am I doing wrong here? The hardware side works fine, but using this code I'm only getting 0xFF back when I read from any register
Here's the code
#include "SPI.h"
#define Pin0 22 //Cable select pins for each channel
#define Pin1 24
#define Pin2 26
#define Pin3 28
#define NumPins 4
int Pins[] = {Pin0, Pin1, Pin2, Pin3};
#define NumRegisters 12
byte RegisterValues[] =
{0x90, 0x03, 0xFF, 0x7F, 0xC0, 0x07, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00};
//Values I want to put in each register
void setup() {
// put your setup code here, to run once:
InitializePins(); //EDITED TO ADD, fixed 0xFF output, but still not getting what I put into the registers
SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE0));
Serial.begin(115200);
for (int i = 0; i < 1; i++) { //only using one channel for now
InitializeChannel(Pins[i]);
VerifyData(Pins[i]);
}
}
void InitializePins() {
for (int i = 0; i < NumPins; i++) {
pinMode(Pins[i], OUTPUT);
digitalWrite(Pins[i], HIGH);
}
}
void InitializeChannel(int Pin) {
digitalWrite(Pin, LOW);
Serial.print("Set pin to low.. #");
Serial.println(Pin);
SPI.transfer(0x80); //Write command starting at Register 0x00
for (int i = 0; i < NumRegisters; i++) {
SPI.transfer(RegisterValues[i]);
Serial.print("Writing to register 0x");
Serial.print(i, HEX);
Serial.print(" value 0x");
Serial.println(RegisterValues[i], HEX);
}
digitalWrite(Pin, HIGH);
}
void VerifyData(int Pin) {
digitalWrite(Pin, LOW);
SPI.transfer(0x00); //Read command starting at Register 0x00
for (int i = 0; i < NumRegisters; i++) {
byte RegVal = SPI.transfer(0);
Serial.print("Register has 0x");
Serial.print(RegVal, HEX);
Serial.print(" and should have 0x");
Serial.println(RegisterValues[i], HEX);
}
digitalWrite(Pin, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
}
And here's the result of when I run it
Set pin to low.. #22
Writing to register 0x0 value 0x90
Writing to register 0x1 value 0x3
Writing to register 0x2 value 0xFF
Writing to register 0x3 value 0x7F
Writing to register 0x4 value 0xC0
Writing to register 0x5 value 0x7
Writing to register 0x6 value 0xFF
Writing to register 0x7 value 0x80
Writing to register 0x8 value 0x0
Writing to register 0x9 value 0x0
Writing to register 0xA value 0x0
Writing to register 0xB value 0x0
Register has 0xFF and should have 0x90
Register has 0xFF and should have 0x3
Register has 0xFF and should have 0xFF
Register has 0xFF and should have 0x7F
Register has 0xFF and should have 0xC0
Register has 0xFF and should have 0x7
Register has 0xFF and should have 0xFF
Register has 0xFF and should have 0x80
Register has 0xFF and should have 0x0
Register has 0xFF and should have 0x0
Register has 0xFF and should have 0x0
Register has 0xFF and should have 0x0
I must just be missing something and I can't see what it is