Here's some code I wrote that we never used. Basicly, you follow the table and it shows you how many ticks or loops is equal to X number of seconds. You set the timer by calling auton_timer_1_start(), pass the number of ticks (loops) it should wait. In your code you can check if the time has passed by using something like this:
Code:
if (auton_timer_1()) {
// Time has been reached
} else {
// Still cycling, time has not been reached
}
Let me know if you don't understand anything. Here's the code:
Code:
// Start prototypes
unsigned char auton_timer_1();
void auton_timer_1_start(unsigned int time);
// End Prototypes
// Variables needed
unsigned int auton_timer_1_current = 0; // Is updated every 26.2ms loop when using timer 1
unsigned int auton_timer_1_limit = 0; // Is the time limit, which gets set by auton_timer_1_start()
// Done with variables
/* ################################ Start Timer Table ########################
1s/26.2ms =~ 38 ticks/s
I.E: If you want a 1 second timer, you must set the timer for 38 ticks.
-= Seconds =- -= Ticks =-
0.25 10
0.50 19
0.75 29
1.00 38
1.25 48
1.50 57
1.75 67
2.00 76
2.25 86
2.50 95
2.75 105
3.00 115
3.25 124
3.50 134
3.75 143
4.00 153
4.25 162
4.50 172
4.75 181
5.00 191
############################### End Timer Table ################ */
/* ******************************************************************************
* FUNCTION NAME: auton_timer_1_start
* PURPOSE: Set and start timer 1
* CALLED FROM: anywhere
* ARGUMENTS: time limit (unsigned int)
* RETURNS: void
****************************************************************************** */
void auton_timer_1_start(unsigned int time) {
auton_timer_1_current = 0;
auton_timer_1_limit = time;
return;
}
/* ******************************************************************************
* FUNCTION NAME: auton_timer_1
* PURPOSE: Checks timer 1 to see if limit has been reached.
* CALLED FROM: anywhere
* ARGUMENTS: none
* RETURNS: TRUE if limit has been reached, FALSE if time has not expired
****************************************************************************** */
unsigned char auton_timer_1(void) {
++auton_timer_1_current;
if (auton_timer_1_current >= auton_timer_1_limit) {
return 1;
} else {
return 0;
}
}