/**************************************************************************

myTimer.c

ME 218B Project Hats Off

Team Firefox

March 7, 2010

 

***************************************************************************/

#include "myTimer.h"

/*-------------------------- Module Functions -----------------------------*/

 

/*-------------------------- Module Variables -----------------------------*/

static unsigned int curTime = 0;

static char hasTimerStarted = FALSE;

 

void MyTimer_Init(void) {

      if (hasTimerStarted == FALSE) {

            hasTimerStarted = TRUE;

            TIM0_TSCR1 = _S12_TEN;                           //Turn timer system on

            TIM0_TSCR2 = TIM0_TSCR2 | _S12_PR1 | _S12_PR0;   //Set pre-scale to /8 = 3.0MHz

 

            TIM0_TIOS = _S12_IOS4;                           //Set cap/comp 4 to output compare

            TIM0_TC4 = (unsigned int) (TIM0_TCNT + 3000);    //Schedule first event for 4

 

            TIM0_TFLG1 = _S12_C4F;                           //Clear OC4 flag

            TIM0_TIE  |= _S12_C4I;                           //Enable OC4 interrupts

            EnableInterrupts;

      }

}

 

unsigned int MyTimer_GetTime(void) {

      return curTime;

}

 

//Implements blocking code for a given number of ms

//Used primarily for debugging and development

void wait(unsigned int ms) {

  unsigned int myTime = 0;

  myTime = MyTimer_GetTime();

  //wait until the current time minus the start time is greater than the wait time

  while ((MyTimer_GetTime() - myTime) < ms) {};

}

 

//Increment curTime variable every time 1ms occurs during OC interrupt

void interrupt _Vec_tim0ch4 myTimerIntRoutin(void){

  curTime++;

  TIM0_TC4 = (unsigned int) (TIM0_TCNT + 3000);  //Schedule first event for 4

  TIM0_TFLG1 = _S12_C4F;                        //clear OC4 flag

}