Page 1 of 1

16F883 ADC reading problems

Posted: Mon Aug 23, 2010 9:59 pm
by bwynne
Please can someone help me.
I have written some C code using the boostC compiler, to read the ADCs as unsigned shorts.
Flowcode 4, is very restrictive with its limited number of signed variable types.
The boost C compiler manual actually recommends the use of unsigned variables for fast code where possible.
when is looked at the compiler and assembler output, i am not sure that the adcon1 commands have been generated correctly.
I have not been able the read the adc'c correctly.
It is hard to debug, as i have and ICD2 debugger and MPlab, but have not found out how to integrate to debug .

TIA
Barry.
Flowcode 4 project is attached.

Re: 16F883 ADC reading problems

Posted: Tue Aug 24, 2010 8:27 pm
by Benj
Hello,

The best way would be to look at the current sample ADC function and then either customise this using the custom code functionality or you can use the code to help build your sample function.

I had a look at your program but other then setting up the adcon registers I cannot see where you are performing a sample. Eg setting the go bit in adcon0 and then waiting for it to clear.

Re: 16F883 ADC reading problems

Posted: Tue Aug 24, 2010 9:01 pm
by bwynne
Hi, i am reading adc2 engine speed:

clear_bit(adcon0, CHS0);
set_bit(adcon0, CHS1 );
clear_bit(adcon0, CHS2);
clear_bit(adcon0, CHS3);
adc_on =1;
godone =1;
while(godone ==1);
MyADC2Val = (unsigned short)((ADRESH <<8) | ADRESL );

Re: 16F883 ADC reading problems

Posted: Wed Aug 25, 2010 9:34 am
by Benj
Hello,

The code below should work for you.

MX_ADC_CONV_SP is the number for the conversion speed.
MX_ADC_CHANNEL is the analogue channel to sample.

You should be able to refer to the device datasheet to get values for each of these.

MX_CHARGE_TIME is the number of program loops to wait for the cap to charge. 40 is normally a good number if you are switching channels.

Remember you will have to ensure that the specific pin you are sampling is already set to be an input.

Code: Select all

unsigned int iRetVal;	

//set the i/o pin as an input

//turn ADC on
adcon0 = (0x01 | (MX_ADC_CONV_SP << 6)) | (MX_ADC_CHANNEL << 2);

//wait the acquisition time
cnt = 0;
while (cnt < MX_CHARGE_TIME) cnt++;

//begin conversion and wait until it has finished
adcon0 = adcon0 | 0x02;
while (adcon0 & 0x02);

iRetVal = (adresh << 2);
iRetVal = iRetVal | (adresl >> 6);