/******************************************************************
 *    DESCRIPTION:   Use Output Compare interrupt to generate 
 *                   a squarewave on PM2 for HCS12C32
 *    SOURCE:        ledindicator.c
 *    Date:          10-14-2006
 *    AUTHOR:        Eric Lindberg, Andrew Toth
 ******************************************************************/
#include "project_main.h"
//set up interrupt service on for output compuare to toggle PM2
#define InterruptInterval 46875 //.5 second @ 24MHz osc


#define PRESCALER 0b101	                   // divide bus clock by 32

#define WaveChannel C0F                    //channel 0 mask byte
#define WaveCompareReg Tim0.tc[0]          //EQU TC0
#define TogglePin  PTM2                    //EQU mPTM_PTM2 ; toggle this pin
#define TogglePinPort Pim.ptm.byte         //EQU PTM ; toggle this port
#define TogglePinDirection  Pim.ddrm.byte  //EQU DDRM ;

void SetLedindicator(){
  TogglePinDirection = TogglePinDirection | DDRM2;		//make it output pin
 	Tim0.tscr1.bit.ten = 1;                             // turn on timer1
 	Tim0.tscr2.bit.pr = PRESCALER; 	                    //
 	Tim0.tios.byte = Tim0.tios.byte | WaveChannel;      //select timer channel 0 for output compare
  //Clear the timer FLAG in TFLG1 by writing a ``1'' to it.
  Tim0.tflg1.bit.c0f = 1;
  Tim0.tie.bit.c0i= WaveChannel;                      //Enable timer channel 0 interrupt
 
  WaveCompareReg = Tim0.tcnt.word+(tU16) InterruptInterval; //  LDD  offset
  return;
}

//Vector Address for Timer Channel 2 is $FFEA
interrupt void ServiceLedindicator() {
 TogglePinPort = TogglePinPort^TogglePin;		// ^ is bitwise exclusive OR operation 
 Tim0.tflg1.bit.c0f = 1;                    // Clear the timer ch0 flag
 // load offset to start a new output compare 
 WaveCompareReg = Tim0.tcnt.word+ (tU16) InterruptInterval;
}