Tracking Elapsed Time

Our team is getting better at programming using MPLAB and I had a question:

  1. What is the best way to track elapsed time?

For example, the the main loop running on the RC is fast enough to loop (3) times each time the IR board receives an input then sends out a pulse. We would like to create a timer and ignore any other pulses from the IR board for a 500ms window to prevent processing the same signal (3) times.

I wasn’t sure if the RC had an internal clock which could be read and stored into a variable.

Good Luck!
TEAM #1711
Traverse City, MI

The PIC has several onboard hardware timers you could use for this, but I think just counting passes through the main loop would be much simpler for this application.

If you really want to use a hardware timer for this, look at this IFI whitepaper. A polled timer should be good enough for your purposes, so you’d setup the timer in your initialization, then you’d start it and pre-load the correct values when you saw a pulse. as long as the interrupt flag is 0, it hasn’t timed out and you should ignore pulses.

We’ve found using loop counters to be very inaccurate in the past. Writing a hardware timer isn’t difficult. We’ve done it several ways, ranging from using someone else’s pre-package code, to writing our own routines last year.

See for a previous discussion on the topic.

Use the same tpe of system the NHRA uses for ETs. they can handle a 26-foot car at 330 MPH right down to thousanths of a second

We’ve found that loop counters are accurate enough for almost all applications. One way to make them better is to account for lost radio packets.

unsigned int loop_counter;

void timer(void) {

	static unsigned char prev_packet;
	unsigned char packets;

	if (rxdata.packet_num > prev_packet)
		packets = rxdata.packet_num - prev_packet;
		packets = 256 - prev_packet + rxdata.packet_num;
  loop_counter += packets;

note: code not guaranteed to compile, done off the top of my head.