CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Time Measurement Between Events

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

Time Measurement Between Events
PostPosted: Sun Jul 03, 2005 10:16 pm     Reply with quote

Hello All,

I have searched the forum for posts that were similar to my problem without any luck.

I need to measure the time between one flag being set and a second flag being set in my code. My initial attempt at resolution should be around 1ms. Part is a 18F2525 and TIMER_0 (RTCC) is currently being used as a counter for a firmware RTC. A 4Mhz crystal is being used.

My initial approach would be to configure TIMER_1 and start it upon the first flag being set and read the number of ticks when the second flag is set..... However, my inexperience with timers doesn't make this as easy as it sounds.

Here are some specific questions that may help me understand:

1. I assume you probably can't enable(start and stop) timers within code. I would imagine that I will have to generate an ISR for the timer and then use the ISR to generate a start time, a finish time, and then subtract the two in the main-line code reach a period?

2. How do I extract the value of the timer to allow me to subtract one reading from the next? (Now that I think of it, I guess I could just use an ISR to add ticks to a counter. Zero the counter at the first flag and read the counter at the second flag?)

3. Is there a better way to do this?

4. Is there a post that anyone knows of that already solves this problem?

Thanks in advance for the help,

John
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 03, 2005 10:58 pm     Reply with quote

Do a test program.

The program below displays the following output on the terminal window:
Quote:
1010
2020

Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//========================

void main()
{
int16 value;

set_timer1(0);

// Turn on the timer and let it run for 1 ms.
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
delay_ms(1);

// Turn it off, read it and display it.
setup_timer_1(T1_DISABLED);
value = get_timer1();
printf("%lu\n\r", value);

// Turn it on again for 1 ms.
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
delay_ms(1);

// Turn it off, read it and display it.
setup_timer_1(T1_DISABLED);
value = get_timer1();
printf("%lu\n\r", value);

while(1);
}
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Time Measurement Between Events
PostPosted: Mon Jul 04, 2005 12:48 am     Reply with quote

Quote:

1. I assume you probably can't enable(start and stop) timers within code. I would imagine that I will have to generate an ISR for the timer and then use the ISR to generate a start time, a finish time, and then subtract the two in the main-line code reach a period?

Nope you can. Read the datasheet on the pic.

Quote:

2. How do I extract the value of the timer to allow me to subtract one reading from the next? (Now that I think of it, I guess I could just use an ISR to add ticks to a counter. Zero the counter at the first flag and read the counter at the second flag?)

Well if you start at 0 and you stop it then just read the timer value

Quote:

3. Is there a better way to do this?

yeah, toggle a pin and measure the signal with a scope.

Quote:

4. Is there a post that anyone knows of that already solves this problem?

lots of them if you search with the correct terms.
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Mon Jul 04, 2005 11:35 am     Reply with quote

Thanks,

It looks like PCM's suggestion is just what I need.

To explain in a litle more detail what I'm attempting might clarify things. I have two SPI sensors that return readings. When the readings excede a preset value a flag is set. So I'm attempting to determine velocity by measuring the time between the two flags.

John
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group