Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : more on the DS3231 Real Time Clock
Author | Message | ||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
Back in November and December 2011 DuinoMiteMegaAndy ran a thread on his Precsion Real Time Clock project. Since then I have bought a Chronodot RTC and got it running using some code fragments from Andy's project. I was much less ambitious than Andy so all I do so far is read and set the RTC and use it to set the Maximite clock. As well as keeping time the DS3231 also measures temperature. Today I thought I would have a go at reading the teperature. It turned out to be easier than I thought. Here is the code I used to read it. It is MMBasic V3.1 ' ===========================================
sub getRTC_Temperature RTCregister=&H11 ' first Temperature register DataLength=2 I2Caddr = &h68 ' DS3231 I2C address 0x68 I2CEN 100,100 ' Enable I2C at speed 100Khz 100ms timeout I2CRCV I2Caddr, 0, DataLength, RTCdata(RTCregister), 1, RTCregister ' insert error checking here if required, read var MM.I2C I2CDIS if RTCdata(RTCregister) < &H80 then ' temperature is not negative ' bits 6 & 7 in register &H12 are fractional degrees where ' &H40 is 0.25 degrees, &H80 is 0.5 degrees and &HC0 is 0.75 degrees Temperature = RTCdata(RTCregister) + (RTCdata(RTCregister + 1) / 256) else ' for our friends in the cool countries, if the temperature is negative then ' the first temperature register has a negative twos complement integer temperature = -(((RTCdata(RTCregister) xor &HFF) + 1) + (RTCdata(RTCregister +1) / 256)) endif end sub ' =========================================== DIM RTCdata(19) getRTC_Temperature ? Temperature The code can handle a temperature range of +127.75 to -127.75. If the MM gets anywhere near those values I don't think we'll care what temperature it is. One point I wasn't sure of was how to compute a negative temperature. The DS3231 datasheet didn't help much. The question was should I convert the first temperature register to a negative value and then add the fraction register or should I add the absolute values of both registers and then convert to negative. My code does the second method. Anyone else know how it should be? I also had trouble with the I2CRCV command. I couldn't be sure from the MMBasic manual and Andy's code exactly how to read a DS3231 register in isolation. If anyone else had that trouble I hope that my code helps a bit. Next is to see how accurate the the DS3231 is and if necessary adjust the Aging Offset Register (10h) to make it more accurate. Anyone had a go at that yet? Bob |
||||
Print this page |