#include "OB6710.h"
#include "ADC.h"

#define PGA_GAIN_10X  0x02
#define PGA_GAIN_20X  0x00
#define PGA_ENABLE 0x01 
#define PGA_OUT           0x08
#define PGA_OUT_FILTER    0x0C

#define PGA_CURRENT_0UA   0x00
#define PGA_CURRENT_16UA  0x20
#define PGA_CURRENT_30UA0 0x30
#define PGA_BY_PASS 0x40

unsigned int ResultData[8], ADC_Channel, PGA_10X, PGA_20X;

void main(void)
{
	unsigned char Loop, Length;
	DACVAL0 = 5;
	DACVAL1 = 5;	
	DACCON = 0x03;
	ADC_Init();
	ADC_SetInputSource(d_ADC_INPUT_1ST, d_ADC_CH0_IN, d_ADC_MODE_continue);
//	ADC_SetInputSource(d_ADC_INPUT_2ND, d_ADC_CH0_IN, d_ADC_MODE_continue);// ADC channel_0 = P2.7	
//	ADC_SetInputSource(d_ADC_INPUT_3RD, d_ADC_OPA_OUT_IN, d_ADC_MODE_continue);// ADC channel_0 = P2.7	

  P2BUFFEN = 0x38;
	P2M0 = 0x00;
	P2M1 = 0xB8;	
	OPACON = PGA_GAIN_20X | PGA_ENABLE | PGA_OUT  ;
//	OPACON = PGA_BY_PASS | PGA_ENABLE | PGA_OUT;
	
	for(Loop = 0x00;Loop<200;Loop++);
	Length = 0x01;
//	ADCINTEN = 2; //for FPGA ´ú¸Õ 

	//---------------------------ADC Channel-------------------------------------------
	ADC_Channel = 0;
	for(Loop =0;Loop<8;Loop++)
	{
		ADC_start(Length);
		ADC_GetSampleResult(&ResultData[0], Length);		
		ADC_Channel = ADC_Channel + ResultData[0];
	}
	ADC_Channel = ADC_Channel/8;
	
	//----------------------------PGA Channel (10x)-----
	OPACON = PGA_GAIN_10X |  PGA_OUT | PGA_ENABLE ;
	ADC_SetInputSource(d_ADC_INPUT_1ST, d_ADC_OPA_OUT_IN, d_ADC_MODE_continue);
	PGA_10X = 0;
	for(Loop =0;Loop<8;Loop++)
	{
		ADC_start(Length);
		ADC_GetSampleResult(&ResultData[0], Length);		
		PGA_10X = PGA_10X + ResultData[0];
	}
	PGA_10X = PGA_10X/8;

	//-----------------------------PGA Channel (20X)----
	OPACON = PGA_GAIN_20X |  PGA_OUT | PGA_ENABLE ;
	ADC_SetInputSource(d_ADC_INPUT_1ST, d_ADC_OPA_OUT_IN, d_ADC_MODE_continue);
	PGA_20X = 0;
	for(Loop =0;Loop<8;Loop++)
	{
		ADC_start(Length);
		ADC_GetSampleResult(&ResultData[0], Length);		
		PGA_20X = PGA_20X + ResultData[0];
	}
	PGA_20X = PGA_20X/8;

  while(1);

}
