/*******************************************************************
* FileName:        David Fisher Exam 2 Problem 1.c
* Processor:       PIC18F4520
* Compiler:        MPLAB C18 v.3.06 
*
* This file reads ADC RE0 and displays the mV to the LCD on the PICDEM
*                                                                     
*
* Creation and Revisions:
*      Author               Date			Comments
*   David Fisher
********************************************************************/

/** Header Files ***************************************************/     
#include <p18f4520.h> 
#include "LCD Module.h"
#include <adc.h>	
#include <stdio.h>	

/** Configuration Bits *********************************************/     
#pragma config OSC = EC  // EC = External 4MHz Crystal for PICDEM board only
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config BOREN = OFF


/** Global Variables ***********************************************/
char line1[17];
char line2[17];
int RE0result;

/*******************************************************************
* Function:        void main(void)
********************************************************************/
#pragma code
void main (void)
{
    XLCDInit();   
    XLCDClear();
    
	
	OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_12_TAD, 
			ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS, 
			0x09);


  	while (1)
    {
	    SetChanADC( ADC_CH5 );		// Select the pin
		ConvertADC(); 				// Start conversion
		while( BusyADC() ); 		// Wait for completion
		RE0result = ReadADC(); 		// Read result
		
		RE0result = (int) ((long)RE0result*5000/1023);
		
		sprintf(line1,"RE0 = %#4u mV",RE0result);
		XLCDL1home();
		XLCDPutRamString(line1);	
    }   
}