Using RTC

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Using RTC

2,768 次查看
vítorpereira
Contributor III

Hi, I'm tring to use the RTC module on the KL25Z, and I began with the instructions on this page: Using RTC module on FRDM-KL25Z . I have already configured the RTC_LDD component and the clock settings, and now I have to write the C code. There's a fuction in the code example of this post that I didn't totaly understand:

void RTC_isr_Seconds(void)

{

   interrupt = 1;

   seconds = RTC_TSR;

    if (seconds >59){

    minutes++;

    RTC_SR &= ~RTC_SR_TCE_MASK;

    RTC_TSR = 0x00; //Reset counter

    seconds = RTC_TSR;

    RTC_SR |= RTC_SR_TCE_MASK;

    }

}

What are these RTC_TSR and RTC_SR? Where did they come from? I didn't understand why "seconds" is equal to RTC_TSR, and neither any of the last four lines.

Also, he doesn't even seems to use the "GetTime" method. Why? How is he getting the time, then?

And last but not least, this is the function executed when the interrupt occurs, right? Is it automatically created or I must write it myself (or even something different of both)?

Could anybody help me on this, please?

Vítor Pereira

标签 (1)
标记 (1)
3 回复数

1,323 次查看
vítorpereira
Contributor III

Thanks, I'm gonna check out both tips!

Vítor Pereira

0 项奖励

1,322 次查看
mjbcswitzerland
Specialist V

Hi

Take a look at http://www.utasker.com/docs/uTasker/uTasker_Time.pdf

and the links in it. It explains how the RTC in the Kinetis works and points out its strengths and weaknesses, as well as giving a complete Gregorian calendar (with time zone and day-light saving operation) solution with regular periodic alarms capabilities and wake-up from low power modes.

Regards

Mark

1,323 次查看
egoodii
Senior Contributor III

RTC register and bit names come from the header file for your CPU.

In a 'larger picture' I am 'quite baffled' by this isr implementation for the use of the RTC 'seconds' register.  Stopping it every minute to reset it to zero, and letting it thus ONLY contain the 'seconds fraction' of current time (although it is not clear WHY they re-read it after setting it to zero?), sounds like an 'error term' in and of itself, but more importantly means that 'time more than one minute' will ONLY keep up while the entire CPU is powered and running.  MOST of us let the RTC 'seconds' register hold the WHOLE time information (even in battery mode!), as in 'total count of seconds since 1/1/1970' or some similar, common basis point.  32bits UNSIGNED will hold for 136 years (twice the 'UNIX rollover concern' of 2038) -- so all you have to do is use proper unsigned math from the 'raw seconds' in TSR and you are 'good past year 2100'.

Note that the RTC subsystem is built from 'slow digital logic', and for exacting guaranteed readings you have to take certain 'double check' precautions, so that you don't get a 'partial read' during an actual increment event.  There are other posts on THIS topic:

RTC and Sub second Time

Conversion from 'seconds since epoch' to m/d/y/h/m/s is a common-enough function, such as here:

c++ - Converting epoch time to "real" date/time - Stack Overflow