16F883 ADC reading problems

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
bwynne
Posts: 6
Joined: Sat May 08, 2010 10:27 pm

16F883 ADC reading problems

Post 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.
Attachments
inj_drive1x.fcf
(12 KiB) Downloaded 273 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: 16F883 ADC reading problems

Post 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.

bwynne
Posts: 6
Joined: Sat May 08, 2010 10:27 pm

Re: 16F883 ADC reading problems

Post 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 );

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: 16F883 ADC reading problems

Post 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);

Post Reply