Below is part of the ADC code i found in microchip's TCP/IP stack.. I am trying to understand the reason why the code is written that way..
Code:
// AN0 should already be set up as an analog input
ADCON0bits.GO = 1;
// Wait until A/D conversion is done
while(ADCON0bits.GO);
// AD converter errata work around (ex: PIC18F87J10 A2)
#if !defined(__18F452)
PRODL = ADCON2;
ADCON2bits.ADCS0 = 1;
ADCON2bits.ADCS1 = 1;
ADCON2 = PRODL;
#endif
// Convert 10-bit value into ASCII string
itoa(*((WORD*)(&ADRESL)), AN0String);
First question i have is regarding the "#if !defined(__18F452)" macro. Why does the code assign ADCON2 to PRODL, and after that make changes to ADCON followed by assigning PRODL back to ADCON2..?
Won't that overwrite the changes made in the middle..?
Second question is, these lines are part of a function that was called everytime the ADC need to sample the signal. Why does the ADC needs to be reinitialized everytime before the sample is taken..? I mean, why not just do it like below..?
Code:
// Wait until A/D conversion is done
while(ADCON0bits.GO);
// Convert 10-bit value into ASCII string
itoa(*((WORD*)(&ADRESL)), AN0String);
Is there any benefit in reinitializing the ADC before taking the samples..? Even in 18F4620's datasheet on page 227, it ask to reconfigure A/D module everytime before taking the sample (Step 7 which ask to go back to Step 1 or Step 2).