I have another noob question.
I am using the max31855 Thermocouple decoder that communicates over SPI with the FEZ panda ii. The total packet is 32 bits but the thermocouple reading is only the first 14 bits and the cold junction the lowest 12 bits. I have tried a number of different bit shifts to try and get at the 14 bits of data. Below is a basic scheme.
- readwrite to spi port, data goes into byte array
- take 1st byte and cast to short
- take 2nd byte and cast to short while shifting bit left 8 positions
- take short and shift it back 2 positions to the right
I have tried this numerous times and I am getting crazy readings on the VFD display. Is it a problem with endianness?
Here is the max31855 datasheet link:
And my code:
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
using System.Text;
namespace VFD
{
public class Program
{
public static void Main()
{
// Set SPI port settings for VFD
SPI.Configuration myVFDPortSettings = new SPI.Configuration((Cpu.Pin)FEZ_Pin.Digital.Di1, false, 0, 0, true, true, 2000, SPI.SPI_module.SPI1);
// Open SPI port for MAX31855
SPI.Configuration myMAXPortSettings = new SPI.Configuration((Cpu.Pin)FEZ_Pin.Digital.Di34, false, 0, 0, false, false, 2000, SPI.SPI_module.SPI2);
// Create SPI VFD port
SPI myVFDPort = new SPI(myVFDPortSettings);
// Create SPI MAX31855 Port
SPI myMAXPort = new SPI(myMAXPortSettings);
// Create byte array for temp data and read in 32bits
byte[] myTempsOut = new byte[4] { 0x00 ,0x00, 0x00, 0x00 };
byte[] myTempsIn = new byte[4];
// Initialize VFD
byte[] myVFDInitialize = new byte[] { 0xF8, 0x38 };
myVFDPort.Write(myVFDInitialize);
Thread.Sleep(6);
byte[] myVFDAddress = new byte[] { 0xF8, 0x00 };
byte[] myVFDClear = new byte[] { 0xF8, 0x01 };
byte[] myVFDMessageStart = new byte[] { 0xFA };
byte[] myVFDOn = new byte[] { 0xF8, 0xC };
while (true)
{
Thread.Sleep(1000);
myVFDPort.Write(myVFDClear);
myMAXPort.WriteRead(myTempsOut, myTempsIn);
Thread.Sleep(10);
short temp = (short)(myTempsIn[0]);
temp |= (short)(myTempsIn[1] <<6);
temp = (short)(temp >> 2);
//temp = (short)(temp >> 6);
double temperature = ((temp * 0.25) * 9 / 5) + 32;
string displaytemp = temperature.ToString();
// Set DDRAM address for text
myVFDPort.Write(myVFDAddress);
Thread.Sleep(10);
// Write to display
byte[] buffer = System.Text.UTF8Encoding.UTF8.GetBytes(displaytemp);
byte[] sendstring = new byte[myVFDMessageStart.Length + buffer.Length];
myVFDMessageStart.CopyTo(sendstring, 0);
buffer.CopyTo(sendstring, myVFDMessageStart.Length);
myVFDPort.Write(sendstring);
Thread.Sleep(6);
temp = 0;
temperature = 0;
// Turn on display
myVFDPort.Write(myVFDOn);
Thread.Sleep(6);
}
}
}
}