A simple circuit is constructed to test the ADC. The potentiometer is use to vary the voltage level on the AN0 pin. The next step would be to configure the register on the PIC for the ADC to operate. ADCON0 sets the refrence voltage to Vdd and Vss and select AN0 as the input for the ADC. ADCON1 configure the PIN as analog input. Only PIN AN0 is selected as analog input in this example. ADCON2 sets the required time for acquisition. The value for TAD is choose based on the calculation in the datasheet (pic18f2455, page 298-299). ADC module is turn on for it to function. ADCON0=0; ADCON1=0x0E; ADCON2=0xD9; ADCON0bits.ADON = 1;
unsigned int ReadADC(void){ unsigned int temp; ADCON0bits.GO = 1; while(ADCON0bits.DONE); temp = ADRESH; temp = ((temp << 8)| ADRESL ); return temp; }
fADCValue = ADCValue * 5; fADCValue /= (1024); To display the floating point number conversion to string must be done. The function sprintf is use to format the output so that it can be showed on the LCD. The sprintf function provided by C18 can't be used directly on a floating point number, so the number must 1st be breakup in to the digits left and right of the decimal point. The sprintf is used to join up the 2 parts and the floating point number is display out on the 2x16 lcd. void Displayfloat(float data){ char LCDOut[16]; long multiplier = 10000; long lWhole=0; // Stores digits left of decimal unsigned long ulPart=0; // Stores digits right of decimal lWhole=(long)((float)data); ulPart=(long)((float)data*multiplier)-lWhole*multiplier; sprintf(LCDOut,"Dec: %ld.%04liV", lWhole, ulPart); XLCDPutRamString(LCDOut); } For this example an average of 200 data is taken before the ADC value is shown out on the LCD display. Attached is the project file for this guide. |
Guides >