Writing const statements to an SPI bus

I have found out that I need to send some values to the breakout on the SPI bus.

const CONFIG_REG_W = 0x80; // Config register write addr
const CONFIG_VALUE = 0xC3; // Config register value (Vbias+Auto+FaultClear+50Hz, see pg 12 of datasheet)
const ADC_WORD_MSB = 0x01; // Addr of first byte of data (start reading at RTD MSB)
const ADC_FAULT_REG = 0x07; // Addr of the fault reg

The first three are the most important with the fourth being good once I add in any fault handling. Once I define the const statements, do I have to execute anything specific to write them to the device?

If you want to store your hex values in a string / write them to the SPI bus, you need to change them to look like this:

“\xFF”

So hardware.spi189.write(“0x80”); would become hardware.spi189.write("\x80");

Here is my current code.

`// Read data from MAX31855 chip

//Constants
const CONFIG_REG_W = 0x80;
const CONFIG_VALUE = 0xC3;
const ADC_WORD_MSB = 0x01;
const ADC_FAULT_REG = 0x07;

//Configure Pins
hardware.spi189.configure((CLOCK_IDLE_HIGH | CLOCK_2ND_EDGE), 100);
hardware.pinD.configure(DIGITAL_OUT); //chip select

// define variables
temp32 <- 0;
farenheit <- 0;
celcius <- 0;
resistance <- 0;
//Define functions
function readChip189(){
//Get SPI data
hardware.pinD.write(0); //pull CS low to start the transmission of temp data
// 0[31…24],1[23…16],2[15…8],3[7…0]
hardware.spi189.write(“0x80”);
hardware.spi189.read(1);
hardware.spi189.write(“0xD3”);
hardware.spi189.read(1);
hardware.spi189.write(“0x01”);
hardware.spi189.read(1);
temp32=hardware.spi189.readblob(4);//SPI read is totally completed here
hardware.pinD.write(1); // pull CS high
// Begin converting Binary data for chip 1
local tc = 0;
{
local highbyte =(temp32[0]<<6); //move 8 bits to the left 6 places
local lowbyte = (temp32[1]>>2); //move to the right two places
tc = highbyte | lowbyte; //now have right-justifed 14 bits but the 14th digit is the sign
//Shifting the bits to make sure negative numbers are handled
//Get the sign indicator into position 31 of the signed 32-bit integer
//Then, scale the number back down, the right-shift operator of squirrel/impOS
// tc = ((tc<<18)>>18);
// Convert to Celcius
resistance = ((tc * 400) / 32768);
celcius = (resistance / 32) - 256;
//celcius = (1.0* tc/4.0);
// Convert to Farenheit
//farenheit = (((celcius*9)/5)+32);
//server.log(temp32);
server.log("Raw " + tc);
server.log(temp32);
server.log(celcius + “C”);
// server.log(farenheit + “°F”);
//agent.send(“Xively”, farenheit);
imp.wakeup(10, readChip189); //Wakeup every 10 second and read data.
}
}

//Begin executing program
hardware.pinD.write(1); //Set the Chip Select pin to HIGH prior to SPI read
readChip189(); //Read SPI data`

OK like this I assume:
hardware.spi189.write("\\x80"); hardware.spi189.read(1); hardware.spi189.write("\\xD3"); hardware.spi189.read(1); hardware.spi189.write("\\x01"); hardware.spi189.read(1);

Is there still a requirement of a read following a write or did I misunderstand an older post?

You shouldn’t need to (although your device may require it?)

Beats me, I am flying blind learning as I go. It’s very similar to the MAX31865 thermocouple chip but designed for RTD’s. http://www.playingwithfusion.com/include/getfile.php?fileid=7020
I am getting readings from it but I can’t quite figure out the bit shifting part.

You don’t need to do reads if you’re throwing the data away; these are implicit.

eg:

hardware.spi189.write("\x80\xd3\x01");

…will do what you need there, I believe. If you do want to capture the data read concurrent with the writes, use writeread().

@Hugo thanks for the info! From there I would guess that a normal SPI read would be in order? I am following Makedeck’s tutorial for a thermocouple based MAX31855 methodology but I am not getting good data from the board. I may have to read a register rather than just the whole SPI output.