P1ASF uses 8 bits to correspond to each pin, so P1ASF= 1, that is, P1.1 is used as analog input.
if you want to output the measured voltage through P2 port and connect it to LED, do you want to display it with 8 LEDs? This shows 8-bit binary numbers, while the result of A/D conversion is 1-bit binary numbers, that is, ~ 11111111, and the corresponding decimal number is -123. Therefore, it is better to display the decimal number directly with a 4-bit digital tube, which is more intuitive.
when performing A/D conversion, initialize the P1ASF register first.
To realize A/D conversion, you need to write a subroutine:
unsigned int ADC _ in (unsigned char n)//n is the value of analog input bit channel, that is, the value of chs2 chs1 chs
three-digit binary number, ~7
{ ADC_CONTR = xe8+n; //start the conversion and set the conversion speed
while ((ADC _ contr &; x1)==) ; //wait for the end of conversion
return(ADC_RES*4+ADC_RESL); //Read the conversion result
}
The subroutine returns an unsigned integer, which is the conversion result.
redefine the variables of the conversion result:
unsigned int ADCX;
call in the main program:
ADCX=adc_in(n); //This n is the value of chs2 chs1 chs
The conversion result obtained after calling the subroutine is in ADCX.