/*
Matt Hobson
ELEN 599
Spring, 2006
Filename: ATD.c
Program purpose: Contains the routines to setup and use the S12 ATD converter
*/

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

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

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

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

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

unsigned char pot = 0;
//float average = 0;          //Stores the mean of the four results
//float voltage = 0;          //Stores the resulting voltage value

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

void ATD_Init (void)
{
  ATD0CTL2 = 0xC2; //Power up ATD,Fast Flag clear,Sequence complete Interrupt on
  ATD0CTL3 = 0x20; //4 conversions, Non-FIFO mode
  ATD0CTL4 = 0xE7; //8-bit,Phase 2 sample=16 ATD clocks, ATD clock = 500kHz
  delay(500);     //Delay for at least 20uS
  ATD0CTL5 = 0xA2; //Right-justified,Unsigned,Continuous scan,Sample 1 channel,
                   //Channel AN2 = Arm Potentiometer
} //ATD ON

//*************************** ATD0_INTERRUPT_ON FUNCTION ***********************

void atd0_interrupt_on (void)
{
   ATD0CTL2 = 	0XC2;  //Turns on the ATD 0 sample complete interrupt
}

//*************************** ATD0_INTERRUPT-OFF FUNCTION **********************

void atd0_interrupt_off (void)
{
   ATD0CTL2 = 	0X00;  //Turns off the ATD 0 sample complete interrupt
}

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

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

void ATD_complete(void)  //Occurs when ATD sequence has been completed
{
  pot = (ATD0DR0L + ATD0DR1L + ATD0DR2L + ATD0DR3L) / 4;
//  voltage = average * 0.01953125;
}

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