I have an Arudino that is reading temperature data from an EMC1182 temperature sensor.
The code is working as expected (go me, it's been 10 years since I coded), and I can display the current temperature to the serial monitor.
The problem is this:
I am reading the internal diode temperature which is located in two registers. A high byte and a low byte. The high byte can easily be displayed in decimal to the serial monitor. For every binary count, the temperature is 1 degree. No need for any kind of conversion here.
However, the low byte is actually fractional. The first 3 MSBs of the byte are used, all other bits are 0. So bit 7 has 0.5 weight, bit 6 has 0.25 weight, and finally bit 5 has 0.125 weight. So 10000000b is actually 0.5 degrees. If I display this to the serial monitor in decimal it will simply say 128. This is correct, but how do I go about creating a decimal fraction from this?
If someone can point to a reference I would be grateful. No need to give me the answer.
Here is my code:
Some of the data:
The code is working as expected (go me, it's been 10 years since I coded), and I can display the current temperature to the serial monitor.
The problem is this:
I am reading the internal diode temperature which is located in two registers. A high byte and a low byte. The high byte can easily be displayed in decimal to the serial monitor. For every binary count, the temperature is 1 degree. No need for any kind of conversion here.
However, the low byte is actually fractional. The first 3 MSBs of the byte are used, all other bits are 0. So bit 7 has 0.5 weight, bit 6 has 0.25 weight, and finally bit 5 has 0.125 weight. So 10000000b is actually 0.5 degrees. If I display this to the serial monitor in decimal it will simply say 128. This is correct, but how do I go about creating a decimal fraction from this?
If someone can point to a reference I would be grateful. No need to give me the answer.
Here is my code:
Rich (BB code):
//I2C Master
#include <Wire.h>
#define ADDR 0x4C
#define confreg 0x03
#define Internal_High_Byte 0x00
#define Internal_Low_Byte 0x29
void setup()
{
Wire.begin(); //start I2C Master mode
Serial.begin(9600);
}
void loop()
{
byte temp_high;
byte temp_low;
byte fraction_temp;
//begin I2C
Wire.beginTransmission(ADDR); //select device to read from
Wire.write(Internal_High_Byte); //select internal high byte register
Wire.endTransmission(); //end I2C before restart
Wire.requestFrom(ADDR, 1); //request data from the Internal High Byte register we just pointed to
while(Wire.available()) //capture the byte
{
temp_high = Wire.read(); //assign it as the low temp high byte
}
delay(1000);
Wire.beginTransmission(ADDR); //select device to read from
Wire.write(Internal_Low_Byte); //select internal high low register
Wire.endTransmission(); //end I2C before restart
Wire.requestFrom(ADDR, 1); //request data from the Internal Low Byte register we just pointed to
while(Wire.available()) //capture the byte
{
temp_low = Wire.read(); //assign it as the low temp low byte
}
Serial.print("Temp = ");
Serial.print(temp_high, DEC); //print the integer temp
Serial.print(".");
Serial.println(temp_low, DEC); //print the fractional temp
delay(1000);
}
Rich (BB code):
Temp = 28.128
Temp = 27.224
Temp = 27.224
Temp = 27.192