This would be somewhat easy to do.
The temperature is reported in 2 byte registers.
Register 011h is the temperature integer and is 2's complement form to support negative numbers.
Register 12h is the fractional portion register (where the fractional value is reported in the upper 2 most significant bits b7 and b6).
To read the temperature just define some variables and use I2Cread to read registers 11h and 12h from the chip.
Code:
TempInt var byte ' variable for temperature integer from register 11h
TempFrac var byte. ' variable for temperature fraction for register 12h
TempAddr var byte ' I2C slave address of the DS3231
TempRegister var byte ' variable for starting register to read
TempSign var bit ' Variable to hold sign bit
TempRegister = $11
TempAddr = $xx ' change xx to 8bit slave address of the DS3231
I2Cread SDA, SCL, TempAddr, TempRegister, [TempInt, TempFrac]
The fractional value is returned in the upper (b7:b6) of the TempFrac variable.
Shift the fractional value 6 bits to the right so that that b7 & b6 become b1 & b0.
Code:
TempFrac = (TempFrac >> 6)
The fractional value is in increments of .25 degrees.
So you will need to multiply the new shifted value in TempFrac by 25.
Code:
TempFrac = (TempFrac * 25)
You will need to test to see if the integer portion is a negative value by checking b7.
If b7 is a 1 then the temperature is negative and the number is a 2's complement of the absolute value, so you will have to convert it to a normal number.
Code:
TempSign = TempInt.7 ' save the sign bit for later
If TempSign = 1 then
' convert the 2's compliment number to a regular integer
TempInt = (TempInt XOR $FF) + 1 ' Google 2's compliment to get more info
Else
Endif
Now you have all the pieces you need for the temperature.
Sign Integer.Fraction
To print it on the LCD all you have to do is see if you need to print a "+" or "-" sign based on TempSign.
Code:
If TempSign = 0 then
LCDOUT $FE, $80, "+", dec TempInt, ".", TempFrac
Else
LCDOUT $FE, $80, "-", dec TempInt, ".", TempFrac
Endif
You can combine most of this into a smaller more compact series of statements, but you can see how it's done.
You want to read the Datasheet regarding the temperature conversion (when the chip reads its internal temperature sensor). It only does this process every so often so you will need to account for that timing in your program.
Bookmarks