/*
Matt Hobson
Amy Thompson
Adeola Arije
ELEN 598
Spring, 2006
Filename: ForceATD.c
Program purpose: Contains the routines to setup and use the S12 ATD converter
                 to measure the Analog voltage output from the on-track force
				 sensors.  The four sensors yield a voltage of 0-5V for a 5V
				 input voltage.  The S12 will monitor the voltages from the 
				 sensors and when a robot rolls over the sensor, the S12 will
				 will trigger the corresponding servo to actuate; raising the
				 gate for the robot to pass through.
*/

//********************************** INCLUDES **********************************

#include <mc9s12dp256.h>
#include <stdio.h>

//********************************* PROTOTYPES *********************************

extern void delay (int); //Delay function prototype

//********************************* VARIABLES **********************************

unsigned char force0 = 0;
unsigned char force1 = 0;
unsigned char force2 = 0;
unsigned char force3 = 0;

//****************************** ATD_INIT FUNCTION *****************************

void ATD_Init (void)
{
  ATD0CTL2 = 0xC2; //Power up ATD,Fast Flag clear,Sequence complete Interrupt on
  ATD0CTL3 = 0x20; //4 conversions per sequence, Non-FIFO mode
  ATD0CTL4 = 0x87; //8-bit,Phase2sample=2 ATDclocks, ATDclock = 1.5MHz(24MHz bus)
  delay(500);      //Delay for at least 20uS
  ATD0CTL5 = 0xB0; //Right-justified,Unsigned,Continuous scan,Sample Multiple 
                   //channels: from AD0 to AD4
} //ATD ON

//****************************** ATD_COMPLETE ISR ******************************

#pragma interrupt_handler ATD_complete()	//Interrupt pragma for ATD

void ATD_complete(void)  //Occurs when ATD sequence has been completed
{
  force0 = ATD0DR0L;
  force1 = ATD0DR1L;
  force2 = ATD0DR2L;
  force3 = ATD0DR3L;
}

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