DS 3231 temperature reading


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Oct 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: DS 3231 temperature reading

    Code:
     
     
           TempRegister CON $11        'temperature integer  REGISTER
           TempRegisterFRA CON $12   'temperature fraction REGISTER
           
           
    TempInt var byte             ' variable for temperature integer from register 11h
    TempFrac var byte           ' variable for temperature fraction for register 12h
    TempSign var bit              ' Variable to hold sign bit   
    
    
    main: 
    
    LCDOut $fe,1
    
           B0 = 0
           button Taster0, 0, 100, 10, B0, 1, meni0
    
    
               
           I2CRead SDA, SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr] 
    
    
            LCDOut $fe,2,     HEX2 hr, ":", HEX2 MINs, ":", HEX2 sec 
            LCDOut $fe,$c0, HEX2 date, "/", HEX2 mon, "/","20" ,HEX2 yr
            
            
            
                
           I2Cread SDA, SCL, rtc, TempRegister, [TempInt, TempFrac]
           
           TempFrac = (TempFrac >> 6)
           TempFrac = (TempFrac * 25)
           
           
           
           TempSign = TempInt.7   ' save the sign bit for later
    
    If TempSign = 1 then  TempInt = (TempInt XOR $FF) + 1
     
    
    
    
    If TempSign = 0 then
        LCDOUT $FE, $80+9,   "+", dec TempInt, ".",DEC TempFrac
    Else
        LCDOUT $FE, $80+9,   "-", dec TempInt, ".",DEC TempFrac
    Endif
             
           
        
           
           GoTo main


    The piece of main part of the code in which I read time, date and temperature.
    The example shows only the registers and variables to read temperature
    Last edited by visnja30; - 6th June 2015 at 15:45.

  2. #2
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: DS 3231 temperature reading

    Okay, there are a couple of issues.
    The strange numbers for negative temperatures are caused by (2) issues.
    1. The LCDOUT commands are being used with the DEC modifier.
    This will not always output the same number of characters and may leave previously printed characters on the screen.
    It will depend on the value to be displayed.
    Use the DEC2 modifier instead. This will force the output to always be (2) characters.
    Since th DS3231 only has a temperature range of -40 to +85, this will be fine.

    2. The DS3231 only uses a simple bit <b7> to indicate the sign. It does not really encode to 2s compliment.
    Adjusted the test and handling of negative values below.

    See if this works for you now.

    Code:
    ''You really should not use constants for the address parameter for I2CREAD & I2CWRITE
    '' From the PBP Manual
    ''Constants should not be used for the Address as the size can vary dependent on the size of the constant. 
    ''Also, expressions should not be used as they can cause an improper Address size to be sent.
    
    ''TempRegister CON $11      'temperature integer  REGISTER
    ''TempRegisterFRA CON $12   'temperature fraction REGISTER
    
    
    TempRegister var byte    ' variable to hold DS3231 register address to read/write
           
           
    TempInt var byte            ' Variable for temperature integer from register 11h
    TempFrac var byte           ' Variable for temperature fraction for register 12h
    TempSign var bit            ' Variable to hold sign bit   
    
    
    main: 
    
    LCDOut $fe,1
    
           B0 = 0
           button Taster0, 0, 100, 10, B0, 1, meni0
    
    
               
           I2CRead SDA, SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr] 
    
    
            LCDOut $fe,2,     HEX2 hr, ":", HEX2 MINs, ":", HEX2 sec 
            LCDOut $fe,$c0, HEX2 date, "/", HEX2 mon, "/","20" ,HEX2 yr
            
            
            
           TempRegister = $11h      ' DS3231 Temperature REgister High Byte (Integer portion)     
           I2Cread SDA, SCL, rtc, TempRegister, [TempInt, TempFrac]
           
           TempFrac = (TempFrac >> 6)   ' Shift <b7:b6> to <b1:b0>
           TempFrac = (TempFrac * 25)   ' Fractional portion is in .25 increments. Increment values are 0-3 (.00, .25, .50, .75)
           
           
           
           TempSign = TempInt.7   ' save the sign bit for later
    
           '''If TempSign = 1 then  TempInt = (TempInt XOR $FF) + 1     'Do not use this, the DS3231 uses simple <b7> sign with <b6:b0> as absolute temperature value
     
    
    
    
           If TempSign = 0 then     ' Positive temperature
               LCDOUT $FE, $80+9,   "+", DEC2 TempInt, ".",DEC2 TempFrac    'Change to DEC2 to make sure LCD does not leave characters from previous temperature printout
           Else                     ' Negative temperature
               TempInt.7 = 0   ' Clear <b7> and use only the absolute value of 11h register value <b6:b0>
               LCDOUT $FE, $80+9,   "-", DEC2 TempInt, ".",DEC2 TempFrac    'Change to DEC2 to make sure LCD does not leave characters from previous temperature printout
           Endif
             
           
        
           
           GoTo main
    Regards,
    TABSoft

  3. #3
    Join Date
    Oct 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: DS 3231 temperature reading

    Now I can not try the code because I ran out of frezze spray. For a day or two when a freez spray come to me, I will tried it.

  4. #4
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: DS 3231 temperature reading

    Once you get your freeze spray.

    Take a reading but don't convert the TempInt byte.
    Just read it and display it on the LCD in Hex2 format.
    That way you can see the raw value from the 3231.

    Do this when the 3231 is at room temperature and then again after lightly applying the spray.

    Once you have both hex values, post them here and we can take a look.
    Regards,
    TABSoft

Similar Threads

  1. Reading temperature using multi DS18B20
    By KVLV in forum Code Examples
    Replies: 16
    Last Post: - 3rd November 2017, 20:48
  2. Max31855 Thermocouple Temperature Reading
    By Lefteris in forum Code Examples
    Replies: 5
    Last Post: - 21st January 2015, 04:20
  3. Thermistor based temperature reading short code required
    By asifiqbal in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th November 2014, 09:18
  4. Dallas 3231
    By jpeakall in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 5th May 2012, 16:10
  5. DS18S20 reading negative temperature
    By srob in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 28th December 2007, 22:21

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts