Electronics > Beginners
solved:MCP2221 USB to I2C bridge with MCP9808 sensor conversion problem, Python.
(1/1)
lk:
Greetings,
just for the fun of it i wanted to see if i could make a small usb temperature sensor, using the MCP2221A Usb to I2C bridge and a MCP9808 temperature sensor. I think i have the hardware working, i can read the data for the temperature. However I have trouble with the conversion of the temperature, i have included my code and the schematic, and the output. The temperature output contains two bytes, a high and a low. The high byte contains alerts[b15-b13] and sign[b12] and the top part of the temperature is in [b11-b8], as far i can see, that is only used at temperatures over 30C. The lower byte is all temperature, and it should be set at 0.0625C per bit. But this seems odd, when i calculate, i get a value of 6.375 for a temperature of ~23C.

Anyone that can see what im doing wrong?

I have attached the schematic for the sensor board, and the datasheet for the MCP9808 sensor. Not for the MCP2221a as communication seems to work

br
Lasse


--- Code: ---#############################################################
#    MIT License                                            #
#    Copyright (c) 2017 Yuta KItagami                       #
#############################################################
from PyMCP2221A import PyMCP2221A

import time
print('-'*50)
print('MCP2221(A) with MCP9808 ')
print('-'*50)
mcp2221 = PyMCP2221A.PyMCP2221A()
mcp2221.Reset()
mcp2221 = PyMCP2221A.PyMCP2221A()

usbdata=[0] * 2
usbdata[0] = 0x05
#usbdata[1] = 0x05
addressdata=[0] * 2

mcp2221.I2C_Init()

print()

#Point to resolution register
#usbdata[0] = 0x08
#usbdata[0] = 0x03
#mcp2221.I2C_Write(0x18,usbdata)

#Setting resolution at 0.625 as per the datasheet page 29
usbdata[0] = 0x08
usbdata[1] = 0x03
mcp2221.I2C_Write(0x18,usbdata)
resolution = mcp2221.I2C_Read(0x18,1)
print("Resolution Data: " + str(resolution))

#check that the chip has the right Manfucaturer data, MCP9804 should be 0x54 or 84 in DEC
addressdata[0] = 0x06
mcp2221.I2C_Write(0x18,addressdata)
mfgdata = mcp2221.I2C_Read(0x18,3)
print("MFG Data: " + str(mfgdata))

# Point to temperature register and read temperature data

addressdata[0] = 0x05
mcp2221.I2C_Write(0x18,addressdata)

rdata = mcp2221.I2C_Read(0x18,3)

#Below is the dataconversion datasheet looks like C but i have "ported" it to Python

print("Read Data: " + str(rdata))
uppertempbyte = rdata[0]
#Zeroing Alert Bits.
print("Read Data Upper pre mask " + str(uppertempbyte))
uppertempbyte = uppertempbyte & 0x1f
print("Read Data Upper post mask: " + str(uppertempbyte))
lowertempbyte = rdata[1]

if ((uppertempbyte & 0x10) == 0x10):
   uppertempbyte = uppertempbyte & 0x0f
   tempperatureminus = 256 - ( uppertempbyte * 16 + lowertempbyte /16)
   print("Negative Temperature: " + str(tempperatureminus))
else:
   tempperatureplus = ( uppertempbyte * 16 + lowertempbyte /16)
   print("Positive Temperature: " + str(tempperatureplus))

# At ~23c i get a positive temp of 6.375



print("Read Data Upper: " + str(uppertempbyte))
print("Read Data Lower: " + str(lowertempbyte))


--- End code ---

--- Quote -----------------------------------------------------
MCP2221(A) with MCP9808
--------------------------------------------------
Reset

Resolution Data: [3]
MFG Data: [0, 84, 1]
Read Data: [192, 102, 1]
Read Data Upper pre mask 192
Read Data Upper post mask: 0
Positive Temperature: 6.375
Read Data Upper: 0
Read Data Lower: 102

Process finished with exit code 0
--- End quote ---

link to mcp9908 datasheet
http://ww1.microchip.com/downloads/en/DeviceDoc/25095A.pdf
lk:
I assembled 2 more boards, and now it works, looks like the sensor on the first board was defective.
Navigation
Message Index
There was an error while thanking
Thanking...

Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod