/*****************************************************************************
 *
 * Analog to digital scanning.
 *
 *****************************************************************************
 * FileName:        ADCmain.c
 * Dependencies:    
 * Processor:       PIC18F with CAN
 * Compiler:       	C18 02.30.00 or higher
 * Linker:          MPLINK 03.20.01 or higher
 * Company:         Microchip Technology Incorporated
 *
 * Software License Agreement
 *
 * The software supplied herewith by Microchip Technology Incorporated
 * (the "Company") is intended and supplied to you, the Company's
 * customer, for use solely and exclusively with products manufactured
 * by the Company. 
 *
 * The software is owned by the Company and/or its supplier, and is 
 * protected under applicable copyright laws. All rights are reserved. 
 * Any use in violation of the foregoing restrictions may subject the 
 * user to criminal sanctions under applicable laws, as well as to 
 * civil liability for the breach of the terms and conditions of this 
 * license.
 *
 * THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, 
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, 
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 *
 * This is a simple timer function used within the DeviceNet Stack for
 * demonstration.
 * 
 *
 *
 * Author               Date        Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Ross Fosler			08/13/04	...	
 * 
 *****************************************************************************/


#include	<P18cxxx.H>
#include	<stdlib.h>
#include	"TYPES.H"
#include	"IO.H"
#include	"strings.h"

UCHAR	uAvgCnt;
UINT uAvgADC;
UINT uOldADC;
ULONG	WREG1;
ULONG	WREG2;
ULONG	WREG3;


#define	TOT_AVG_CYCLES	64

void ADC_Init(void)
{
	ADCON0 = 0b00000001;	// Enable the ADC
	ADCON1 = 0b00000000;	// All analog
	ADCON2 = 0b10000011;	// Right justified, RC timed

	uAvgCnt = TOT_AVG_CYCLES;
	
	uOldADC = ~uAvgADC;
}


void ADC_Update(void)
{
	UCHAR pValStr[6];

	// Scan interrupt flag
	if (PIR1bits.ADIF)
	{
		PIR1bits.ADIF = 0;

		if (uAvgCnt)
		{
			uAvgCnt--;
			uAvgADC = uAvgADC + ADRES;
		}
		else
		{
			// Take the average
			uAvgADC = uAvgADC >> 6;

			// If it changed the update
			if (uOldADC != uAvgADC)
 			{
				uOldADC = uAvgADC;

				// Write the value
				putrs(MsgPotValPos);
				pValStr[1] = pValStr[2] = pValStr[3] = pValStr[4] = pValStr[5] = ' ';			
				itoa(uAvgADC, (char*)(pValStr + 1));
				pValStr[0] = 5;
				puts(pValStr);

				// Write the voltage
				putrs(MsgPotVoltPos);
				pValStr[1] = pValStr[3] = pValStr[4] = pValStr[5] = ' ';
				WREG1 = (uAvgADC * 5) / 1023;
				itoa(WREG1, (char*)(pValStr + 1));
				pValStr[2] = '.';
				WREG1 = (((5 * uAvgADC) % 1023) * 10);
				WREG2 = WREG1 / 1023;
				itoa(WREG2, (char*)(pValStr + 3));
				WREG2 = (WREG1 % 1023) * 10 / 1023;
				itoa(WREG2, (char*)(pValStr + 4));
				pValStr[0] = 5;
				puts(pValStr);

			}

			uAvgCnt = TOT_AVG_CYCLES;
			uAvgADC = 0;

		}
	}
	else
	{
		// Start a conversion
		ADCON0bits.GO = 1;
	}	
	
	

}
