I'm pretty new at this programming stuff, and as we're trying to get a speed control on our robot's wheels together, I concluded that we needed some way to record time. I came up with the following, which will keep track of the time since powerup:
Code:
/* clock.h */
#ifndef CLOCK_H
#define CLOCK_H
typedef unsigned long int clock_int;
/* sets up the clock interrupts and such */
void clock_init();
/* runs every time the clock hits an interrupt */
void Clock_Interrupt_Handler();
/* starts the clock ticking */
void clock_start();
/* halts the clock */
void clock_stop();
/* returns the current clock time safely */
clock_int Get_Clock_Time();
#endif
/* clock.c */
#include "ifi_aliases.h"
#include "ifi_default.h"
#include "ifi_utilities.h"
#include "clock.h"
volatile clock_int clock_time; // time elapsed since last boot * .1msec
/* sets up the clock interrupts and such */
/* call this from User_Initialization */
void clock_init()
{
clock_time = 0;
};
/* runs every time the clock hits an interrupt */
void Clock_Interrupt_Handler()
{
//
};
/* starts the clock ticking */
void clock_start()
{
// enable the clocking interrrupt
};
/* halts the clock */
void clock_stop()
{
// disable the clocking interrupt
};
unsigned long int Get_Clock_Time()
{
unsigned long int time;
/* halt the clock for reading */
clock_stop();
/* read the time */
time = clock_time;
/* start the clock again ASAP */
clock_start();
return time;
};
Explanation of the above:
All times are of type clock_int, which is an unsigned long int (32 bits).
clock_init() is called in User_Initialization and will set up the interrupts for the timer.
Clock_Interrupt_Handler() is called from the interrupt handling routine; it checks to see if the timer triggered the interrupt, and if so, increments clock_time.
Get_Clock_Time() safely returns the value of clock_time.
The timer interrupt will be so arranged as to trigger every .1 msec. Thus, the clock ticks every .1 msec. Doing the math on that shows me that the RC could run for nearly 5 days continuously before the clock resets to 0. I think it's a pretty safe bet that no match will go on that long, so I now have a safe way of timing things by simple subtraction.
Comments? I'll be the first to admit I'm a n00b. Is this a totally insane way of doing this? Is there some RC feature that does this for me? Or am I a Clever Boy for putting it together?