/*******************************************************************
* FileName:        Problem 2.c
* Processor:       PIC18F4520
* Compiler:        MPLAB C18 v.3.06 
*
* Lab Exam Problem 2 - Winter 2008-2009
*                                                                     
*
* Creation and Revisions:
*      Author               Date			Comments
*   David Fisher			1/26/09
********************************************************************/

/** Header Files ***************************************************/     
#include <p18f4520.h> 
#include <adc.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

/** Define Constants Here ******************************************/


/** Local Function Prototypes **************************************/


/** Global Variables ***********************************************/
int RA0result;

/*******************************************************************
* Function:        void main(void)
********************************************************************/
#pragma code
void main (void)
{
	OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_12_TAD, 
		ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS, 
		0x0E);
	
	TRISB = 0x00;

	while (1)
    {
		SetChanADC( ADC_CH0 );		// Select the pin
		ConvertADC(); 				// Start conversion
		while( BusyADC() ); 		// Wait for completion
		RA0result = ReadADC(); 		// Read result
		
		switch(RA0result/256)
		{
			case 0:
				PORTB = 0b1000;
				break;
			case 1:
				PORTB = 0b0100;
				break;
			case 2:
				PORTB = 0b0010;
				break;
			case 3:
				PORTB = 0b0001;
				break;
			
		}
			
		
    }
}