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.
16F883 ADC reading problems
Moderator: Benj
- 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
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: 16F883 ADC reading problems
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 );
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 );
- 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
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.
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);Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel