52259 counter

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

52259 counter

Jump to solution
1,047 Views
salman83
Contributor II

Hi,

 

I want to measure in milliseconds on mcf52259. I can't use RTC, so I am left with GPT. How do i do it.

 

Thanks & Regards.

Labels (1)
0 Kudos
Reply
1 Solution
844 Views
TomE
Specialist II

> I want a counter which i can use to calculate the start and end time

Which the DMA timers are perfect for. There's no need for overflow interrupts or anything.

> I do not want any interrupts at all.

What makes you think you even need an interrupt? What broken timers have you used in the past that need this (maybey timers on 8-bitters).

I guess this might be a little "tricky", but I've known about this use of simple binary arithmetic for so long it just seems an utterly obvious way to do this.

You set up the DMA Timer to free-run instead of resetting at a reference value, so it counts from zero to 0xffffffff AND THEN ROLLS OVER TO ZERO. This is incredibly powerful and simple. To use this, all you have to do is set the timer running and then:

unsigned long nTime1 = DTCn;

... Do something that you want to time ...

unsigned long nTime2 = DTCn;

unsigned long nElapsedTime = nTimer2 - nTime1;

THAT'S IT. You're subtracting two 32-bit numbers. No need to worry about rollover or anything. As long as the thing you're timing is less than the roll time (4,294,967,296 microseconds, or 71 minutes, 34 seconds) you end up with an unsigned duration in microseconds.

But make sure you always SUBTRACT the two values before comparing them to anything. If you write code like "if (nTime2 > nTime1 + 1000)" it won't work when it wraps (once every 71 minutes) and those bugs are very hard to track down.

You can have longer times by making the timer run slower, or measure finer times by making it run faster, remembering the maximum duration is now less.


I find that using 1MHz makes coding very easy when everything is in microseconds and there's no need to convert to some weird "tick value".


Tom


View solution in original post

0 Kudos
Reply
7 Replies
844 Views
TomE
Specialist II

> How do I do it.

Is this homework?

Read the manual. Write some code.

Or search for some sample code and App Notes. Type "GPT sample code" into the "Search" box on this page for starters. Or type register names into Google.

have you gone to the MCF52259 Product Page and downloaded "MCF5225X_SAMPLE_CODE:"? It may not help but you should try that first.

You don't say WHAT you want to measure. If you want to accurately measure an external signal then you want to use "Capture" mode of the GPT. if you want to measure software execution time, or to have a timer you can use from software you're better off programming a DMA Timer to free-run at 1MHz and just read the DMA Counter register to get "the time". Then subtract subsequent samples to get the duration.

Tom

0 Kudos
Reply
844 Views
salman83
Contributor II

Hi Tom,

Thanks for the reply.

It is not a homework.

Basically, I want to use some timeouts in my code. It does not require any capture from external signal.

So you are suggesting me that I should go for DMA timers for measuring time (milliseconds) in the software.

Regards.

0 Kudos
Reply
844 Views
TomE
Specialist II

> It is not a homework.

I couldn't tell from your post that it wasn't.

> So you are suggesting me

Yes. They are very easy to program and use for this.

Tom

0 Kudos
Reply
844 Views
salman83
Contributor II

Hi Tom,

I looked into the DMA Timers. I found that there is no overflow flag to tell that the counter has reached its upper limit.

I want a counter which i can use to calculate the start and end time. If the difference is 500 milliseconds, then I execute a certain operation. I do not want any interrupts at all.

Thanks for your help anyway.

Regards.

0 Kudos
Reply
845 Views
TomE
Specialist II

> I want a counter which i can use to calculate the start and end time

Which the DMA timers are perfect for. There's no need for overflow interrupts or anything.

> I do not want any interrupts at all.

What makes you think you even need an interrupt? What broken timers have you used in the past that need this (maybey timers on 8-bitters).

I guess this might be a little "tricky", but I've known about this use of simple binary arithmetic for so long it just seems an utterly obvious way to do this.

You set up the DMA Timer to free-run instead of resetting at a reference value, so it counts from zero to 0xffffffff AND THEN ROLLS OVER TO ZERO. This is incredibly powerful and simple. To use this, all you have to do is set the timer running and then:

unsigned long nTime1 = DTCn;

... Do something that you want to time ...

unsigned long nTime2 = DTCn;

unsigned long nElapsedTime = nTimer2 - nTime1;

THAT'S IT. You're subtracting two 32-bit numbers. No need to worry about rollover or anything. As long as the thing you're timing is less than the roll time (4,294,967,296 microseconds, or 71 minutes, 34 seconds) you end up with an unsigned duration in microseconds.

But make sure you always SUBTRACT the two values before comparing them to anything. If you write code like "if (nTime2 > nTime1 + 1000)" it won't work when it wraps (once every 71 minutes) and those bugs are very hard to track down.

You can have longer times by making the timer run slower, or measure finer times by making it run faster, remembering the maximum duration is now less.


I find that using 1MHz makes coding very easy when everything is in microseconds and there's no need to convert to some weird "tick value".


Tom


0 Kudos
Reply
844 Views
salman83
Contributor II

Thanks Tom for the clarification.

One more question, I was reading through the documentation MCF52259RM. The formula mentioned in section 26.4.2 about calculating the timeout values is little strange.

For example in section 26.1.2, the maximum timeout mentioned is 109,951 seconds at 80 MHz (~31 hours). 

But if I use the formula in section 26.4.2, that is

Timeout period = (1 ⁄ 80000000) × 16 × (0xFF + 1) × (0xFFFFFFFF + 1) = 219,902.3255552 seconds.

System Frequency: 80 MHz

But if I divide the timeout period by 2, then i will get the mentioned maximum timeout period.

Once again thanks for the help.

0 Kudos
Reply
844 Views
TomE
Specialist II

Congratulations, you've found your very own bug in a manual!

I've just looked at the manual for the chip I work with, and the summary there says:

    Maximum timeout period of 219,902 seconds at 80 MHz (~61 hours)

Here's where you can start on your own Archaeology Expedition. Typing "Maximum timeout period of 109,951 seconds at 80 MHz" into Freescale's Search box finds the MCF52259 manual ONLY. Searching for "219,902" instead finds the MCF5329, 5282, 53017 and 5373 manuals. Cut/copy/paste/edit bug in copying chapters around. There's an Addendum to that manual, but it doesn't mention that one.

You can get very long timeouts, but to make your code easier I'd recommend you divide by 1, 1 and 80 to get a 1us count. If you want a finer time period, the next useful  one is 1, 1 and 8 to get a 100ns clock.

.

Tom

0 Kudos
Reply