最消费类的方案开发,一直用到九齐的片子,带AD的基本都是062D,但是后面推出了062E代替了D版,看资料,使用AD需要一个零点校准的初始化程序,刚开始也一脸懵逼,经过一番摸索有,在这里做一个记录,
//ADC初始化校准 ---------------------------------------------------------------------------------
//void F_ADJMD(void)
//void F_GND_Convert(char count)
//void ADC_Calibration_Pro(void)
void F_GND_Convert(char count)
{
char i;
ADMD = 0x90 | C_VSS; // Select (GND) pad as ADC input
CLRWDT(); //clear WDT, which is defined in NY8Common.h
delay(100); // Delay 0.56ms(Instruction clock=4MHz/2T) for waiting ADC stable
for(i=1;i<=count;i++)
{
ADMDbits.START = 1; // Start a ADC conversion session
while(ADMDbits.EOC == 0)
; // Waiting for ADC conversion complet
R_AIN_DATA_LB += ( 0x0F & ADR);
}
}
void F_ADJMD(void)
{
DISI();
ADMDbits.GCHS = 1; // Enable global ADC channel (SFR "ADMD")
F_GND_Convert(8); // executing GND ADC converting 8 times
R_AIN_DATA_LB >>= 3; // Only get Bit7~4
if(R_AIN_DATA_LB >= 2)
{
ADJMD = 0x00;
while(1)
{
CLRWDT();
R_AIN_DATA_LB = 0;
F_GND_Convert(8); // executing GND ADC converting 8 times
R_AIN_DATA_LB >>= 3; // Only get Bit7~4
if((ADJMD<0x1F) & (R_AIN_DATA_LB >= 2)){ADJMD++;}
else if(ADJMD == 0x1F){ADJMD = 0x00;break;}
else {R_AIN_DATA_LB = 1;break;}
}
}
if(R_AIN_DATA_LB == 0)
{
ADJMD = 0x20;
while(1)
{
CLRWDT();
R_AIN_DATA_LB = 0;
F_GND_Convert(8); // executing GND ADC converting 8 times
R_AIN_DATA_LB >>= 3; // Only get Bit7~4
if((ADJMD<0x3F)&(R_AIN_DATA_LB == 0)){ADJMD++;}
else if(ADJMD == 0x3F){ADJMD = 0x20;break;}
else {break;}
}
}
}
void ADC_Calibration_Pro(void)
{
//----- Initial ADC-----
ADMD = C_ADC_En | C_ADC_CH_Dis | C_VSS ; // Enable ADC power, Disable global ADC input channel. Select PA1 pad as ADC input (SFR "ADMD")
//----- ADC high reference voltage source select-----
// ADVREFH = C_Vrefh_VDD; // ADC reference high voltage is supplied by internal VDD (Note: ADC clock freq. must be equal or less than 2MHz @ VDD=5.0V)
// ADVREFH = C_Vrefh_4V; // ADC reference high voltage is supplied by internal 4V (Note: ADC clock freq. must be equal or less than 1MHz)
// ADVREFH = C_Vrefh_3V; // ADC reference high voltage is supplied by internal 3V (Note: ADC clock freq. must be equal or less than 500KHz)
ADVREFH = C_Vrefh_2V; // ADC reference high voltage is supplied by internal 2V (Note: ADC clock freq. must be equal or less than 250KHz)
delay(100); // 4M/2T , 0.56mS
//----- ADC clock frequency select----------------------------
// ADR = C_Ckl_Div1; // ADC clock=Fcpu/1, Clear ADIF, disable ADC interrupt
// ADR = C_Ckl_Div2; // ADC clock=Fcpu/2, Clear ADIF, disable ADC interrupt
ADR = C_Ckl_Div8; // ADC clock=Fcpu/8, Clear ADIF, disable ADC interrupt
// ADR = C_Ckl_Div16; // ADC clock=Fcpu/16, Clear ADIF, disable ADC interrupt
//----- ADC Sampling pulse width select-------------
ADCR = C_Sample_1clk | C_12BIT; // Sample pulse width=1 adc clock, ADC select 12-bit conversion ( Note: ADC clock freq. must be equal or less than 500KHz)
// ADCR = C_Sample_2clk | C_12BIT; // Sample pulse width=2 adc clock, ADC select 12-bit conversion ( Note: ADC clock freq. must be equal or less than 1MHz)
// ADCR = C_Sample_4clk | C_12BIT; // Sample pulse width=4 adc clock, ADC select 12-bit conversion ( Note: ADC clock freq. must be equal or less than 1.25MHz)
// ADCR = C_Sample_8clk | C_12BIT; // Sample pulse width=8 adc clock, ADC select 12-bit conversion ( Note: ADC clock freq. must be equal or less than 2MHz)
//--------------------------------------------------
ADJMD = 0x00;
F_ADJMD();
}