Main Site Documentation

Thermocouple Module


#1

I am trying to read a thermocouple module. I am using tinyclr on a G80. The tutorial in docs on spi did not help me much. Is there a source of information on spi using tinyclr?


#2

To know how to setup the SPI bus in TinyCLR you need the information from the module’s datasheet. Can you link the sheet?


#3

Thanks for responding.


#4

Ok, so you hooked everything up and now you need c# code to run the SPI bus on TinyClr?


#5

the spi documentation http://docs.ghielectronics.com/tinyclr/tutorials/spi.html tells you everything you need to use SPI (of course if you are familiar with SPI)
what else do you need ?

could you ask a technically qualified question ?
that would help us to help you…


#6

I do have it connected. And yes, I am working on the c# code. It appears I need to set up the byte array to tell the chip what I want and then look at the response using -
device.TransferSequential(…)// this is good for reading registers -
from the docs that Kevin has pointed to.


#7

When I look at the netmf code:

		private uint ReadData() {
		uint data = 0;

		this.cs.Write(false);

		for (int i = 31; i >= 0; i--) {
			this.clk.Write(true);

			if (this.miso.Read())
				data |= (uint)(1 << i);

			this.clk.Write(false);
		}

		this.cs.Write(true);

		return data;
	}

I get a pretty good idea of what is happening. Much of this detail seems to have been hidden in the “tranfersequential” command. I am not sure how to set up the byte arrays for communication.


#8

You can’t tell the chip anything; it’s a read only device. :slight_smile:

“Drive CS low to output the first bit on the SO pin. A complete serial-interface read of the cold-junction compensated
thermocouple temperature requires 14 clock cycles. Thirty-two clock cycles are required to read both the thermocouple and reference junction temperatures”

EDIT: OK no, You have 2 choices to read the data. You can read 14 bits (which s 3 bytes) or you can read 32 bits (4 bytes) from the device at a time. I suggest 32 bits for completeness. So all you need to do is Transfer a byte array of 4 blank bytes, while reading in the 4 bytes of the data.


#9

Ok, the TransferFullDuplex method worked. I now have a byte array. Thank you for helping me through the problem. The TransferSequential and Read methods did not work for me.


#10

So you understand what to do from this point onward? Don’t forget the value of the reading is has negative values (14 bits with 2’s compliment).


#11

I will admit I am clueless at this point but again thank you for your help.


#12

Here is some code that works to get the external temperature. I refer to my coding style as “brute force” or “caveman”. Anyway, it works.

        private double ReadData()
    {
        byte[] bufferwrite = new byte[4];
        byte[] bufferread = new byte[4];
   
        _dev.TransferFullDuplex(bufferwrite, bufferread);
        double degC = 0;
        int temp = (int)bufferread[0];
        if (temp >= 128) temp = temp - 128;//strip off sign value
        if (temp >= 64) { temp = temp - 64; degC = degC + Math.Pow(2, 10); }
        if (temp >= 32) { temp = temp - 32; degC = degC + Math.Pow(2, 9); }
        if (temp >= 16) { temp = temp - 16; degC = degC + Math.Pow(2, 8); }
        if (temp >= 8) { temp = temp - 8; degC = degC + Math.Pow(2, 7); }
        if (temp >= 4) { temp = temp - 4; degC = degC + Math.Pow(2, 6); }
        if (temp >= 2) { temp = temp - 2; degC = degC + Math.Pow(2, 5); }
        if (temp == 1) {degC = degC + Math.Pow(2, 4); }

        temp = (int)bufferread[1];
        if (temp >= 128) { temp = temp - 128; degC = degC + Math.Pow(2, 3); }
        if (temp >= 64) { temp = temp - 64; degC = degC + Math.Pow(2, 2); }
        if (temp >= 32) { temp = temp - 32; degC = degC + Math.Pow(2, 1); }
        if (temp >= 16) { temp = temp - 16; degC = degC + Math.Pow(2, 0); }
        if (temp >= 8) { temp = temp - 8; degC = degC + Math.Pow(2, -1); }
        if (temp >= 4) { degC = degC + Math.Pow(2, -2); }
        return degC;
    }

#13

The Gadgeteer module code would have helped you here https://gadgeteer.codeplex.com/SourceControl/latest#Main/Modules/GHIElectronics/Thermocouple/Software/Thermocouple/Thermocouple_43/Thermocouple_43.cs
uint value = ReadData();
short temperature = 0;
byte[] data = new byte[4];
data[0] = (byte)(value >> 24);
data[1] = (byte)(value >> 16);
data[2] = (byte)(value >> 8);
data[3] = (byte)(value >> 0);
temperature = (short)(((data[1]) | (data[0] << 8)));
temperature = (short)(temperature >> 4);
return temperature;


#14

Thank you Brett, that does look a lot better.`

        private double ReadData()
    {
        byte[] bufferwrite = new byte[4];
        byte[] bufferread = new byte[4];
   
        _dev.TransferFullDuplex(bufferwrite, bufferread);

        double degC = 0;
        short temperature = 0;
        temperature = (short)(((bufferread[1]) | (bufferread[0] << 8)));
        temperature = (short)(temperature >> 4);
        degC = (double)temperature;
        return degC;
    }