C8051F5X系列带有一个12位的ADC,并且有一个八位复用器,可以通过ADC0CN、REF0CN寄存器进行设置,如下所示
void ADC0_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = ACTIVE_PAGE;
// Initialize the Gain to account for a 5V input and 2.25 VREF
// Solve the equation provided in Section 9.3.1 of the Datasheet
// The 5V input is scaled by a factor of 0.44 so that the maximum input
// voltage seen by the pin is 2.2V
// 0.44 = (GAIN/4096) + GAINADD * (1/64)
// Set GAIN to 0x6CA and GAINADD to 1
// GAIN = is the 12-bit word formed by ADC0GNH[7:0] ADC0GNL[7:4]
// GAINADD is bit ADC0GNA.0
ADC0CF |= 0x01; // Set GAINEN = 1
ADC0H = 0x04; // Load the ADC0GNH address
ADC0L = 0x6C; // Load the upper byte of 0x6CA to
// ADC0GNH
ADC0H = 0x07; // Load the ADC0GNL address
ADC0L = 0xA0; // Load the lower nibble of 0x6CA to
// ADC0GNL
ADC0H = 0x08; // Load the ADC0GNA address
ADC0L = 0x01; // Set the GAINADD bit
ADC0CF &= ~0x01; // Set GAINEN = 0
ADC0CN = 0x03; // ADC0 disabled, normal tracking,
// conversion triggered on TMR2 overflow
// Output is right-justified
REF0CN = 0x33; // Enable on-chip VREF and buffer
// Set voltage reference to 2.25V
ADC0MX = 0x0A; // Set ADC input to P1.2
ADC0CF = ((SYSCLK / 3000000) - 1) << 3; // Set SAR clock to 3MHz
EIE1 |= 0x04; // Enable ADC0 conversion complete int.
AD0EN = 1; // Enable ADC0
SFRPAGE = SFRPAGE_save;
}