EmbeddedRelated.com
Forums
Memfault Beyond the Launch

RTC resolution less than a second ?

Started by r_bottleneck August 30, 2006
Hello,

I would like to get a more or less precise time between two events
(0 to some seconds with 1 or 10 ms resolution would be fine), but I
have no timer left and the WDT is also in use.

So I just could use the RTC of the LPC2138/48.

Has anybody experiences using the RTC and CTCR to get a better
resolution than a second ? I think it's a bit more work to manage the
overflow of the RTC registers because both important registers can not
be read at the same time .

Thanks

An Engineer's Guide to the LPC2100 Series

> Hello,
>
> I would like to get a more or less precise time between two events
> (0 to some seconds with 1 or 10 ms resolution would be fine), but I
> have no timer left and the WDT is also in use.
>
> So I just could use the RTC of the LPC2138/48.
>
> Has anybody experiences using the RTC and CTCR to get a better
> resolution than a second ? I think it's a bit more work to manage the
> overflow of the RTC registers because both important registers can not
> be read at the same time .
>

I am using RTC to get 50ms interval.

RTCPREINTE;
// RTCPREFRAC=0x86C0;
while(RTCSEC==0);
RTCCCR = CTCRST;
RTCSEC=0;
RTCCCR = CLKEN;

This will wait for about 50 ms.
You can fine tune with RTCPREFRAC also.
Yes the clock is 12 Mhz and not 32 Khz.
Best Regards,

Mukund Deshmukh.
Beta Computronics Pvt Ltd
10/1, IT Park, Parsodi,
Nagpur-440022
Cell - 9422113746
Thanks,

But I need the RTC as well and it's running with 32KHz . My
intention was to read the time if the first event occurs and
read again if the second event occurs. Then I simply plan to
calculate the difference.

Regards

> I am using RTC to get 50ms interval.
>
> RTCPREINTE;
> // RTCPREFRAC=0x86C0;
> while(RTCSEC==0);
> RTCCCR = CTCRST;
> RTCSEC=0;
> RTCCCR = CLKEN;
>
> This will wait for about 50 ms.
> You can fine tune with RTCPREFRAC also.
> Yes the clock is 12 Mhz and not 32 Khz.
> Best Regards,
>
> Mukund Deshmukh.
> Beta Computronics Pvt Ltd
> 10/1, IT Park, Parsodi,
> Nagpur-440022
> Cell - 9422113746
>
> -----Original Message-----
> From: l...
> [mailto:l...]On Behalf
> Of r_bottleneck
> Sent: Wednesday, August 30, 2006 8:40 AM
> To: l...
> Subject: [lpc2000] Re: RTC resolution less than a second ?
> Thanks,
>
> But I need the RTC as well and it's running with 32KHz . My
> intention was to read the time if the first event occurs and
> read again if the second event occurs. Then I simply plan to
> calculate the difference.
>
> Regards
>
>

You could just read one of the standard timer count registers
directly and then do the math, providing they don't overflow
more than once within your measurement period.

Mike
I assume that your interrupt code would not steal the processor
form more than a small fraction of a second. (Otherwise it
is pointless reading the prescaler at all).

I haven't verified the code, but the way I would approach it is:
1) read the prescaler => low1
2) read the consolidated time register 0 => rest2
3) read the prescaler => low3
4) read the consolidated time register 0 => rest4

If low3 >= low1 then (low1, rest2) is a valid time set, as is (low3, time2)

If low3 < low1 then there was overflow between them.
So the next question is which combination of low and rest is good.
If rest2 == rest4 then (low3, rest4) is good.
if rest2 != rest4 then both (low1, rest2) and (low3, rest4) are good.
So in either case (low3, rest4) is good. So that is what I would return.

This can be extended to consolidated time register 1 as well, but
it gets harder to explain all cases.

Hope this helps,
Danish

--- In l..., "r_bottleneck" wrote:
>
> Hello,
>
> I would like to get a more or less precise time between two events
> (0 to some seconds with 1 or 10 ms resolution would be fine), but I
> have no timer left and the WDT is also in use.
>
> So I just could use the RTC of the LPC2138/48.
>
> Has anybody experiences using the RTC and CTCR to get a better
> resolution than a second ? I think it's a bit more work to manage the
> overflow of the RTC registers because both important registers can not
> be read at the same time .
>
> Thanks
>
>
> But I need the RTC as well and it's running with 32KHz . My
> intention was to read the time if the first event occurs and
> read again if the second event occurs. Then I simply plan to
> calculate the difference.
>

If you operate RTC at 32Khz minimum period will be 1 sec only.
To get smaller period you have to increase clock.

Best Regards,

Mukund Deshmukh.
Beta Computronics Pvt Ltd
10/1, IT Park, Parsodi,
Nagpur-440022
Cell - 9422113746
--- In l..., Mukund Deshmukh wrote:
>
> >
> > But I need the RTC as well and it's running with 32KHz . My
> > intention was to read the time if the first event occurs and
> > read again if the second event occurs. Then I simply plan to
> > calculate the difference.
> > If you operate RTC at 32Khz minimum period will be 1 sec only.
> To get smaller period you have to increase clock.
>
> Best Regards,
>
> Mukund Deshmukh.
> Beta Computronics Pvt Ltd
> 10/1, IT Park, Parsodi,
> Nagpur-440022
> Cell - 9422113746
>

I think you are not quite right. Please consider, I don't want to get
any interrupts by the RTC. I just want to get the time between two
events, calculated by SW.
I'm currently do some tests with an EVB. I'll post the result.

Regards
> -----Original Message-----
> From: l...
> [mailto:l...]On Behalf
> Of r_bottleneck
> Sent: Friday, September 01, 2006 1:30 PM
> To: l...
> Subject: [lpc2000] Re: RTC resolution less than a second ?
> --- In l..., Mukund Deshmukh
> wrote:
> >
> > >
> > > But I need the RTC as well and it's running with 32KHz . My
> > > intention was to read the time if the first event occurs and
> > > read again if the second event occurs. Then I simply plan to
> > > calculate the difference.
> > >
> >
> > If you operate RTC at 32Khz minimum period will be 1 sec only.
> > To get smaller period you have to increase clock.
> >
> > Best Regards,
> >
> > Mukund Deshmukh.
> > Beta Computronics Pvt Ltd
> > 10/1, IT Park, Parsodi,
> > Nagpur-440022
> > Cell - 9422113746
> > I think you are not quite right. Please consider, I don't want to get
> any interrupts by the RTC. I just want to get the time between two
> events, calculated by SW.
> I'm currently do some tests with an EVB. I'll post the result.
>
> Regards

Then why don't you just read one of the other timer count
register, instead of using the RTC? It is easy to calculate the
time from any free-running counter.

Mike
--- In l..., "Michael Anton" wrote:
>
> > -----Original Message-----
> > From: l...
> > [mailto:l...]On Behalf
> > Of r_bottleneck
> > Sent: Friday, September 01, 2006 1:30 PM
> > To: l...
> > Subject: [lpc2000] Re: RTC resolution less than a second ?
> >
> >
> > --- In l..., Mukund Deshmukh
> > wrote:
> > >
> > > >
> > > > But I need the RTC as well and it's running with 32KHz . My
> > > > intention was to read the time if the first event occurs and
> > > > read again if the second event occurs. Then I simply plan to
> > > > calculate the difference.
> > > >
> > >
> > > If you operate RTC at 32Khz minimum period will be 1 sec only.
> > > To get smaller period you have to increase clock.
> > >
> > > Best Regards,
> > >
> > > Mukund Deshmukh.
> > > Beta Computronics Pvt Ltd
> > > 10/1, IT Park, Parsodi,
> > > Nagpur-440022
> > > Cell - 9422113746
> > >
> >
> > I think you are not quite right. Please consider, I don't want to get
> > any interrupts by the RTC. I just want to get the time between two
> > events, calculated by SW.
> > I'm currently do some tests with an EVB. I'll post the result.
> >
> > Regards
> >
> > Then why don't you just read one of the other timer count
> register, instead of using the RTC? It is easy to calculate the
> time from any free-running counter.
>
> Mike
>
They are simply used for other stuff and do not run all the time.
As I promised , below the result of a quick test during the weekend
with the RTC.

Test conditions:
Time between the rising edges of an async. 1 KHz signal should be
checked as precise as possible using the RTC CTIME0 and CTCR
registers. Connected to INT2 of a LPC2148 (running with around
60 MHz). RTC using a 32 KHz crystal.
Time for readout CTCR , twice CTIME0 and time calculation around 17 us.

Result:
Precision better than 30 us between two edges. I did not see jumping
time for more than 30us. It seems CTCR is running with 64 KHz (?).

Below the ISR just for time differences up to 59 seconds:
(It's just an example and I'm not sure if all overflow conditions are
properly managed yet)

// EXTINT2 interrupt SR
void isr_ext2(void)__attribute__ ((interrupt("IRQ")));
void isr_ext2(void)
{
float x,y;
u_int tdiff, time_new_b;
test_on; // test on (trigger for analyzer)

time_new = RTC_CTIME0; // read consolidated time
tick_new = RTC_CTC; // read CTC value
time_new_b = RTC_CTIME0; // read consolidated time again

time_new &= 0x3F; // discard not use bits
time_new_b &= 0x3F;
tick_new &= 0x7FFF;

if (tick_new > 2)
time_new = time_new_b; // use last consol. time
else
tick_new = 59 - tick_new; // use first consol. time and

// correct CTC value
if (time_old > time_new)
tdiff = (60 - time_old) + time_new; // seconds as int
else
tdiff = time_new - time_old; // seconds as int

x = (float)tdiff; // seconds as float
x += ((float)tick_new * 0.000015258); // * 1/65536 -> total time
// as a single value
y = x - x_old; // time difference

time_result[tr_point++] = y; // save result for evaluation
if (tr_point > 1000)
tr_point = 0;
time_old = time_new;
x_old = x;
test_off; // test off (trigger for analyzer)

SCB_EXTINT |= 0x04; // Clear EINT2 interrupt flag
VICVectAddr = 0; // Acknowledge Interrupt
}

B::var.watch_time_result
time_result = (0.001007028, 0.001007028, 0.001007028, 0.001007028,
0.001007028, 976.5122e-6, 0.001007028, 0.001007028, 0.001007028,
0.00100702, 0.001007028, 976.5122e-6, 0.001007028, 0.001007028,
0.001007028, 0.001007028, 0.001007028, 0.00100702, .......

That's much better as I expected.

Memfault Beyond the Launch