What type of timers for autonomous?

Hey,
i read the white papers document on innovationfirst.com, n e ways i wanted to know if there are n e other ways of using real timers(not counters), And it will be really good if someone gives an short example :slight_smile:

Well, a timer really is just a counter, except you know there is a constant interval between increments, so you can get how much time has passed from the counter.

For autonomous mode, probably the best way to do it would be to set up a global timer with ms resolution. I used timer2 for this rather than timer1. Timer2 is easier to work with (8 bits rather than 16 bits) and easier to set up.

For ms resolution, set up timer2 as follows:
prescaler = 4
postscaler = 10
compareVal = 249

Use this equation to determine output freq:
Fosc Hz / 4 / prescaler / (compareValue + 1) / postscaler = out Hz

If you plug in the above values (Fosc = 40 MHz) you get 1 kHz, or 1000 cycles per second, or 1 cycle per ms.

Then just make sure you enable the interrupt and catch the interrupt flag and increment a global unsigned long int.

You can use that global variable anywhere in the program much the same way you would use TimeGetTime() in windows or System.getCurrentTimeMillies() in Java. It is the number of ms since your program started.

By the way, with a unsigned long your timer will overflow after 1193 hours. If you want to save a byte you can use an unsigned short long which will last 4.66 hours.