Menu

DS3231 Temperature.

mkstevo
2021-01-27
2021-01-28
  • mkstevo

    mkstevo - 2021-01-27

    I'm designing (yet) another clock.

    As this will have a "linear" display, I wondered if I could also use it as a thermometer. I'm using the DS3231 which has a temperature sensing module in it.

    Looking at the DS3231.h file, I see that two items are defined (DS3231_MSB_temp and DS3231_LSB_Temp) with the values of the two temperature registers 11h and 12h though are not mentioned further in the header.

    Would I be correct in assuming that if I call: function DS3231_ReadRegister ( in DS_Value ) in a similar way to this, I will get the two values from the temperature registers.

    Let TemperatureLowByte  = DS3231_ReadRegister(12h)
    Let TemperatureHighByte = DS3231_ReadRegister(11h)
    

    I'm also assuming that the temperature is stored in BCD, similar to the time values.

    {Update: The values are NOT in BCD, they are in pseudo "two's complement" decimal for the MSB and fractional representation using the uppermost two bits for the LSB}

     

    Last edit: mkstevo 2021-01-28
  • Anobium

    Anobium - 2021-01-27

    Should work as you describe.

    Try this....

    Dim TemperatureWord as Word
    Let TemperatureWord = DS3231_ReadRegister(12h)
    Let TemperatureWord_h = DS3231_ReadRegister(11h)

    This will load the low bytes into the lower byte of the word, and the high byte into the high (_h) byte of the word variable.

    Anobium

     
  • mkstevo

    mkstevo - 2021-01-27

    Many thanks. It does work much as anticipated.

    Dim TempMSB         As Byte
    Let TempMSB = 0
    Dim TempLSB         As Byte
    Let TempLSB = 0
    Dim Minus           As Bit
    Let Minus = 0
    
    Let TempMSB = DS3231_ReadRegister(0x11)
    Let TempLSB = DS3231_ReadRegister(0x12)
    
        If TempMSB > 127 Then 'Minus value
            Let Minus = 1
            Let TempMSB = TempMSB - 128
        Else
            Let Minus = 0
        End If
    
        Select Case TempLSB
        Case 0
            Let TempLSB = 0
        Case 64
            Let TempLSB = 25
        Case 128
            Let TempLSB = 50
        Case 192
            Let TempLSB = 75
        Case Else
            Let TempLSB = 0
        End Select
    
        If Minus = 1 Then
            Print "-"
        End If
        Print TempMSB
        Print "."
        Print TempLSB
        If TempLSB = 0 Then
            Print "0"
        End If
        LCDWriteChar(Degrees_Sym)
    

    It's telling me my office is 23.50' . Roughly correct.

     

    Last edit: mkstevo 2021-01-27
  • Anobium

    Anobium - 2021-01-27

    Hot stuff!!!!

    Well done

     
  • mkstevo

    mkstevo - 2021-01-28

    Just to clarify the operation of the temperature within the DS3231 should anyone be reading this in the future.

    Calling the function DS3231_ReadRegister() with a value of 11h (0x11) will return the temperature MSB value, as a byte, in decimal. The uppermost bit, Bit.7, of this value is set to either '0' or '1'. When Bit.7 is set to '0' the remaining values indicate a positive temperature value. When set to '1' the remaining values indicate a negative temperature value. Therefore values of temperature can be returned in the range +127 to -127 degrees centigrade. You will see that I have tested the value returned, if it is above 127(decimal) it is a negative value, deducting 128(decimal) from that value gives the negative temperature value.

    Calling the function DS3231_ReadRegister() with a value of 12h (0x12) gives a fractional value of the temperature. This value (oddly in my opinion) is placed in the uppermost two bits of the byte. Bits 5, 4, 3, 2, 1 and 0 are always zero and can be ignored.

    Bit.7   Bit.6   Fractional Value
    0        0           0.0
    0        1           0.25
    1        0           0.50
    1        1           0.75
    

    From my example, you can see I simply compared the decimal equivalents (0, 64, 128 and 192) and then converted those values into decimal representations (0, 25, 50 and 75) before printing them.

    Neither of the two values returned are BCD encoded as the time values are.

     
  • mkstevo

    mkstevo - 2021-01-28

    I've done that. Of course on my Mac I've had to import the .docx file into Pages, then export it out again (as a .docx and a .pdf). There is a chance that some of the formatting is different to the original.

    I have uploaded the updated files to GitHub.

     
  • Anobium

    Anobium - 2021-01-28

    Look great. Thank you.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.