#ifndef _DRV_AFE_ADC_H_
#define _DRV_AFE_ADC_H_

unsigned int Drv_GetADC_AVGn(unsigned char channel, unsigned char Cnt);
unsigned int Drv_GetADC(unsigned char channel);
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// ====================================================================================@
//                              24Bit ADC 基本設置                                      @
// ====================================================================================@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#define SET_ADC_POWER(x)			{ _adcen = x;}    //!< ADC電源開關
#define SET_ADC_STARTCONVERT()		{ _start = 0; _start = 1; _start = 0;}   //!< 開始ADC轉換

#define SET_ADC_DATA_4_15()			{ _adrfs = 0; }
#define SET_ADC_DATA_0_11()			{ _adrfs = 1; }
//!< 參考電壓選擇
#define SET_ADC_REF_VREF()		{ _savrs1 = 0; _savrs1 = 1;}
#define SET_ADC_REF_AVDD()		{ _savrs1 = 0; _savrs1 = 0;}
#define SET_ADC_REF_VR()	    { _savrs1 = 1; _savrs1 = 0;}

#define ADC_CHANNEL_AN0		0
#define ADC_CHANNEL_AN1		1
#define ADC_CHANNEL_AN2		2
#define ADC_CHANNEL_AN3		3
#define ADC_CHANNEL_AN4		4
#define ADC_CHANNEL_AN5		5
#define ADC_CHANNEL_AN6		6
#define ADC_CHANNEL_AN7		7
#define ADC_CHANNEL_AN8		8
#define ADC_CHANNEL_AN9		9
#define ADC_CHANNEL_AN12	10
#define ADC_CHANNEL_AN13	11
#define ADC_CHANNEL_AN14	12
#define ADC_CHANNEL_AN15	13
#define ADC_CHANNEL_AVDD	14
#define ADC_CHANNEL_AVDD_2	15
#define ADC_CHANNEL_AVDD_4	16
#define ADC_CHANNEL_VR		17
#define ADC_CHANNEL_VR_2	18
#define ADC_CHANNEL_VR_4	19
#define ADC_CHANNEL_AVSS	20

#define SET_ADC_CHANNEL_AN0()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x00;}
#define SET_ADC_CHANNEL_AN1()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x01;}
#define SET_ADC_CHANNEL_AN2()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x02;}
#define SET_ADC_CHANNEL_AN3()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x03;}
#define SET_ADC_CHANNEL_AN4()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x04;}
#define SET_ADC_CHANNEL_AN5()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x05;}
#define SET_ADC_CHANNEL_AN6()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x06;}
#define SET_ADC_CHANNEL_AN7()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x07;}
#define SET_ADC_CHANNEL_AN8()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x08;}
#define SET_ADC_CHANNEL_AN9()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x09;}
#define SET_ADC_CHANNEL_AN12()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x0C;}
#define SET_ADC_CHANNEL_AN13()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x0D;}
#define SET_ADC_CHANNEL_AN14()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x0E;}
#define SET_ADC_CHANNEL_AN15()	{ _sadc1 &= 0x07; _sadc0 &= 0xF0; _sadc0 |= 0x0F;}
#define SET_ADC_CHANNEL_AVDD()  { _sadc1 &= 0x07; _sadc1 |= 0x10; _sadc0 &= 0xF0;}
#define SET_ADC_CHANNEL_AVDD_2(){ _sadc1 &= 0x07; _sadc1 |= 0x20; _sadc0 &= 0xF0;}
#define SET_ADC_CHANNEL_AVDD_4(){ _sadc1 &= 0x07; _sadc1 |= 0x30; _sadc0 &= 0xF0;}
#define SET_ADC_CHANNEL_VR()    { _sadc1 &= 0x07; _sadc1 |= 0x50; _sadc0 &= 0xF0;}
#define SET_ADC_CHANNEL_VR_2()  { _sadc1 &= 0x07; _sadc1 |= 0x60; _sadc0 &= 0xF0;}
#define SET_ADC_CHANNEL_VR_4()  { _sadc1 &= 0x07; _sadc1 |= 0x70; _sadc0 &= 0xF0;}
#define SET_ADC_CHANNEL_AVSS()  { _sadc1 &= 0x07; _sadc1 |= 0x80; _sadc0 &= 0xF0;}

#if SYS_CLOCK_FREQ == 4000000
#define SET_ADC_CLOCK_SYS_2()	{ _sacks2 = 0; _sacks1 = 0; _sacks0 = 1;}
#define SET_ADC_CLOCK_SYS_4()	{ _sacks2 = 0; _sacks1 = 1; _sacks0 = 0;}
#define SET_ADC_CLOCK_SYS_8()	{ _sacks2 = 0; _sacks1 = 1; _sacks0 = 1;}
#define SET_ADC_CLOCK_SYS_16()	{ _sacks2 = 1; _sacks1 = 0; _sacks0 = 0;}
#define SET_ADC_CLOCK_SYS_32()	{ _sacks2 = 1; _sacks1 = 0; _sacks0 = 1;}
#endif

#if SYS_CLOCK_FREQ == 8000000
#define SET_ADC_CLOCK_SYS_4()	{ _sacks2 = 0; _sacks1 = 1; _sacks0 = 0;}
#define SET_ADC_CLOCK_SYS_8()	{ _sacks2 = 0; _sacks1 = 1; _sacks0 = 1;}
#define SET_ADC_CLOCK_SYS_16()	{ _sacks2 = 1; _sacks1 = 0; _sacks0 = 0;}
#define SET_ADC_CLOCK_SYS_32()	{ _sacks2 = 1; _sacks1 = 0; _sacks0 = 1;}
#define SET_ADC_CLOCK_SYS_64()	{ _sacks2 = 1; _sacks1 = 1; _sacks0 = 0;}
#endif
#if SYS_CLOCK_FREQ == 12000000
#define SET_ADC_CLOCK_SYS_8()	{ _sacks2 = 0; _sacks1 = 1; _sacks0 = 1;}
#define SET_ADC_CLOCK_SYS_16()	{ _sacks2 = 1; _sacks1 = 0; _sacks0 = 0;}
#define SET_ADC_CLOCK_SYS_32()	{ _sacks2 = 1; _sacks1 = 0; _sacks0 = 1;}
#define SET_ADC_CLOCK_SYS_64()	{ _sacks2 = 1; _sacks1 = 1; _sacks0 = 0;}
#endif

#endif