I2C and the MCP9800 Temperature sensor

Forum for problems or queries regarding Flowcode Comms Components. Eg LIN, I2C, SPI, RS232, CAN, IrDA etc

Moderators: Benj, Mods

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Creative25 »

Hi Enamul.
This is excellent now it only takes a quarter of the space.
Is there a way of rounding the numbers so it will increase in steps of 0.05 degrees?
Thanks
Uli

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Enamul »

Is there a way of rounding the numbers so it will increase in steps of 0.05 degrees?
I am afraid this might not be possible as this device has maximum resolution of 0.0625 which we are using now..we can't go beyond that. 0.05 is smaller than 0.0625.
Enamul
University of Nottingham
enamul4mm@gmail.com

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Creative25 »

Ok I understand.
You can not make it more precise than the resolution.
Except If one takes multiple samples.
I think then rounding to 0.1 would be more appropriate.
Could I do this simply by dividing by then and then shifting the decimal point?
Best regards:
Uli

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Enamul »

Hi Uli,

I have an idea..think if it suits you...we will read as it is but round in the following way:
TEMP.....original...rounded
0..........0.00......0.00
1..........0.0625...0.05
2..........0.1250...0.10
3..........0.1875...0.15
4..........0.2500...0.25
5..........0.3125...0.30
Do you like that?
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Enamul »

Hi Uli,

I have an idea..think if it suits you...we will read as it is but round in the following way:
TEMP.....original...rounded
0..........0.00......0.00
1..........0.0625...0.05
2..........0.1250...0.10
3..........0.1875...0.15
4..........0.2500...0.25
5..........0.3125...0.30
Do you like that?
Enamul
University of Nottingham
enamul4mm@gmail.com

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Creative25 »

Hi Enamul.
I think it makes no sense to round to A higher resolution.
I would rather to round it to steps of 0.1

I have another question In the Data sheet of the MCP9800 it says that your pull up resistors have a too low value then the sensor can heat itself up.
However in the data sheet it does not give a good idea what pull up resistors are recommended for the data lines.
At the Moment I use two 10K resistors. Could I go even higher?
Best Regards:
Uli

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Benj »

Hi Uli,

This article on I2C pull up resistors may help.
http://www.dsscircuits.com/articles/eff ... stors.html

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Creative25 »

Hi Benj,
Thanks for the Link.
My question is this do you need to get a square wave in order for I2c bus to be stable?

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Enamul »

Yes Uli. Otherwise PIC might not recognize that as high state..so you see from the graphs upto 10 K it's quite has better slew rate..
Enamul
University of Nottingham
enamul4mm@gmail.com

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Creative25 »

Hi Benj,
Ok so the More square the wave is the more time the voltage is above the threshold voltage, the more time the pic has available to read the signal.
So ideal would be between 3.3 and 4.7K assuming the capacitance of the test circuit is the same as on my board.
Do you think self heating of the temperature sensor is an issue with any of those resistor values?
Best Regards:
Uli

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Enamul »

In most of the I2C devices datasheet the supplier recommend to use 4.7 K, although this is not the case in this device. I mean the supplier didn't mention anything. But output low current for SDA is 3 mA which indicates 1-2 Kohm of resistance value should be for pull up at least. So I would use 4.7 K as pull-up.
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
petesmart
Valued Contributor
Valued Contributor
Posts: 395
Joined: Thu May 06, 2010 11:42 am
Location: Sydney, Australia
Has thanked: 187 times
Been thanked: 140 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by petesmart »

I agree with Enamul, 4k7 is very common.

Here is another good article on how to clculate the pull up resisitor values http://www.edn.com/design/analog/437129 ... unications. I t describes the imapct of capacitance and bus speed vs resistor values.

It allso provides the Math to verify the values... It refrences Microchip which is helpful

all the best

Pete
sorry about that Chief!

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Creative25 »

Hi Enamul.
I have been trying to figure out the formulas you are using to calculate the temperature.
But after long study I do still not fully understand some items.
Could you explain what this formula does?

"TEMP = (Temp_H << 8 ) OR Temp_L"
"TEMP = (TEMP >> 4)"
I think it combines the higher bytes with the lower bytes.
Is that right?
Could you explain what the OR does?
Best Regards:
Uli

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Enamul »

Hi Uli,
TEMP = (Temp_H << 8 ) OR Temp_L
(Temp_H << 8 ) shifts 8 bit left to make higher 8-bit of 16-bit data and then OR with Temp_L to make complete 16-bit
For example, you have Temp_H = 0xFF and Temp_L = 0xF0
in binary, Temp_H = 11111111; Temp_L= 11110000
so after (Temp_H << 8 ) we get 1111111100000000 = 0xFF00
which ORed with Temp_L so 0xFF00 OR 0xF0 = 0xFFF0
This is how the bytes(8-bit) are combined to produce INT(16-bit).
TEMP = (TEMP >> 4)
As for 12-bit resolution, we don't need first 4 bit out of 16-bit data, I have to remove those from calculation. Shifting right 4 time knock out 4 bits from front..That is how I got desired 12 bit remaining in the TEMP variable.
Enamul
University of Nottingham
enamul4mm@gmail.com

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Creative25 »

Thanks for the Info.
This is a very basic question.
What does the OR function mean? I thought it means OR like in a or gate, but in this context it must have a different function.
Could you explain to me?
Can the AND function do the same thing?
Uli

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Enamul »

Hi Uli,
This OR actually doing bitwise OR operation like the gates. Same as OR gate not different in this context rather we have used to perform the same.
OR function:
1111111100000000 (0xFF00)
0000000011110000 (0xF0)
----------------------
1111111111110000 (so you see normal OR)
AND operation:
1111111100000000 (0xFF00)
0000000011110000 (0xF0)
----------------------
0000000000000000(so you see normal AND)
So you can see OR is serving our purpose here not AND but in other case like finding out one bit from one byte of data AND operation is useful. For example,
Temp_H = 0xFF which is 11111111.
I want to find whether MSB (7th bit from left) is 0 or 1. What I can do...I can check whether the Temp_H AND 0x80 equal to 0x80 (128 in decimal) or not?
so why 0x80...0x80 in binary 10000000
AND operation
11111111
10000000
-----------
10000000 = 0x80
So, if (Temp_H AND 0x80=0x80 ) is true that means MSB is 1 otherwise MSB is 0.
Enamul
University of Nottingham
enamul4mm@gmail.com

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Creative25 »

Hi Enamul,
Thanks for the explanation, now I understand.
OR affects the numbers in bit form.
Could you explain what the second formula does?

Temp_L = Temp_L AND 0xF0
TEMP = (Temp_H << 8 ) OR Temp_L
TEMP = (NOT TEMP) + 1

Best Regards:
Uli

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Enamul »

Hi Uli,
This is for the negative temperature where I have make sure that there is nothing but zeros in first 4 bits by..

Code: Select all

Temp_L = Temp_L AND 0xF0
This is done by ANDing 0xF0 with Temp_L. If Temp_L = 0xXX here X may be 0 to F. So after the AND operation which make Temp_L = 0xX0.
Normally in 12-bit resolution first 4-bit should be zeros by default but for safety I did that.
Then I have combined low and high bytes to make 16-bit TEMP (TEMP = (Temp_H << 8 ) OR Temp_L)as mentioned in last post.
The issue in negative temperature is not same as positive any more as the TEMP needs to be 2's complemented. For 2's complementing we have to invert the TEMP and then add 1 with it which is done in the following..

Code: Select all

TEMP = (NOT TEMP) + 1
Enamul
University of Nottingham
enamul4mm@gmail.com

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Creative25 »

Hi Enamul,
I still do not understand.
On TEMP_H if the temperature is minus bit 8 is high?
On TEMP_H if the temperature is plus bit 8 is low?
Or is it the opposite?
How did you remove that bit?

Now if the temperature goes to minus. Does it change bit 8 in TEMP_H and start to count from the highest number and go down, as temperature goes down?

Best Regards:
Uli

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Enamul »

On TEMP_H if the temperature is minus bit 8 is high?
On TEMP_H if the temperature is plus bit 8 is low?
As bit starts from 0 so last bit is 7th.
Temp_H<7> is high if temp negative..and low if temp is positive.
How did you remove that bit?
I didn't remove that bit and don't need to do that...according to datasheet.
Now if the temperature goes to minus. Does it change bit 8 in TEMP_H and start to count from the highest number and go down, as temperature goes down?
this is somewhat the case is in 2's complement numbering system and the sensor returns temp data in 2's complement system.
1111111111110000 = -0.0625 which is -1 in decimal.
So I have inverted the data...
0000000000001111
Add 1 with that..
0000000000010000 = 0x0010
Shifted 4 bit right...
0x001 which is decimal 1..while displaying the number I put (-) in front as I already know this is negative from decision logic.
Enamul
University of Nottingham
enamul4mm@gmail.com

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: I2C and the MCP9800 Temperature sensor

Post by Creative25 »

Hi
I have modified this code so that I can have a plain number.
Because I want to use the tempf variable in a calculation in order to switch a transistor.
I have used this code for several months now but somehow I suspect there is something wrong.
It seems that minus temperatures are not displayed correctly. On some minus temperatures it changes the wrong digit. If temperature increases by one step ich channges the second last digit, instead of the last one, this happens at random. Sometimes it works correctly.
As mentioned above this happens only with negative numbers.

Can someone have a look at the code and tell me if there is someting wrong?
Or could it be a fault with the sensor itself?

Best Regards:
Uli
Attachments
Mcp9800_Macro.fcf
(14 KiB) Downloaded 294 times

Post Reply