View previous topic :: View next topic |
Author |
Message |
arunkish
Joined: 23 Dec 2008 Posts: 83
|
Strange error in ADC |
Posted: Fri Aug 12, 2011 9:38 pm |
|
|
I am using 18F4620, 20Mhz Crystal and I am trying to read the ADC and convert it into Volts. (10bit adc). When I measure the voltage in Multimeter it gives me 0.19, but when i see it on the LCD it gives me, .00205 using the below code. When the voltage reaches more than 1.0, the LCD shows me correct reading like 1.10 , 2.32 etc... Where am I wrong ???
But when .0020 is displayed I read the variable analog_val and it gives me 42. So (42*5)/1023 gives me 0.20. But when I get a value of .00205 ?????
Code: |
int16 analog_val;
float volts;
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(7);
delay_us(50);
analog_val = read_adc();
volts=(float)(analog_val*5.0)/1023.0;
sprintf(string,"Arun : \%3.2f ",volts);
displaytext_adv(0x02,0xC1,YELLOW,BLACK,400,255, string);
|
Tris settings for PORTE
set_tris_e(0b00000110);
Please advice
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 12, 2011 9:52 pm |
|
|
You are missing the line for setup_adc_ports().
Also, the internal ADC clock is not the best for accuracy. Look in the
ADC section of the PIC data sheet to see a table of the correct clock
divisor for setup_adc() for 20 MHz. Then look in the .h file for your PIC
in the section on setup_adc() to see the correct constant to use to enable
that divisor. |
|
|
arunkish
Joined: 23 Dec 2008 Posts: 83
|
|
Posted: Fri Aug 12, 2011 11:01 pm |
|
|
Dear PCM
Thank you for your reply. I would like to use only PORTE2 / AN7 for ADC, I dont have an option in the header file to use AN7 / RE2 alone. But as I said I guess that I am getting the right ADC value as 42, but when converted into volts instead of 0.20 it shows .00205. I will also check for the table you said. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Sat Aug 13, 2011 7:05 am |
|
|
you're using...
sprintf(string,"Arun : \%3.2f ",volts);
.. and a quick read of that function says no error checking is done.
that may be your problem
also
3.2f means convert to float and truncate giving a value of say 1.23 but you say you saw .00205 !
so something is wrong !
perhaps format to 5.4 and see what results you get ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sat Aug 13, 2011 8:01 am |
|
|
One possibility is that the output is not what you think.
If (for instance), this is being written to an LCD, and you are not clearing the screen. Is it possible that you had a sequence of values one after the other, with some digits left of the display, that just happened to result in the display finally showing 0.00205.
Almost every value is going to overflow your field width, which then makes the output difficult to determine (the first digit, is the _total_ field width, and includes the decimal point).
Try "%04.2f " which then leaves space for the digit in front of the decimal, and puts a space afterwards, which will make it obvious if older values are still being displayed.
The reason the header doesn't have an option to use AN7 alone, is that the chip doesn't support this _read the data sheet_. Ideally it'd be better to move to something like AN0, which can be selected on it's own. Register 19-2 in the data sheet, shows the allowed configurations.
Best Wishes |
|
|
arunkish
Joined: 23 Dec 2008 Posts: 83
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 16, 2011 1:51 pm |
|
|
Quote: | I would like to use only PORTE2 / AN7 for ADC,
I dont have an option in the header file to use AN7 / RE2 alone.
|
What does this mean ? Does it mean that you still don't have a
setup_adc_ports() statement ? Are you trying to use AN7 as
an Analog input while the pin is still configured as a Digital pin ?
Quote: | I have SIMULATED the variables and you can see what happens. I am confused !!
volts=((42.0*5.0)/1023.0);
sprintf(string,"Arun : %3.2f ",volts);
I get .00205
and for %5.4f
.0000205xxxxx |
Post your compiler version. |
|
|
arunkish
Joined: 23 Dec 2008 Posts: 83
|
|
Posted: Tue Aug 16, 2011 8:19 pm |
|
|
Dear PCM
As you can see I have not used any Analog Inputs. Just doing a math, but I get wrong results. Compiler version is 3.222 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Wed Aug 17, 2011 2:26 am |
|
|
I don't know what you are doing, so took your 'simulation', and stuck it into a minimum program:
Code: |
#include <18F4620.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES PUT //Power Up Timer
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOWRT //Program memory not write protected
#fuses INTRC
#use delay(clock=8000000)
#use RS232(XMIT=PIN_C6,RCV=PIN_C7,ERRORS,Baud=9600)
void main(void) {
char buffer[32];
float volts;
volts=((42.0*5.0)/1023.0);
sprintf(buffer,"Arun : %3.2f ",volts);
printf("%s",buffer);
do {
} while(TRUE);
}
|
Compiled with 3.222.
Displays:
"Arun : .20 "
This is cut and pasted from hyperterminal.
Exactly what you'd expect.....
3.222, is old, but seems to work OK for this (the version I have dates to mid 2005). So I have to ask how you are actually seeing this?. Are you using a simulator?. If so, _doubt it_.
I suspect you are wasting a lot of time, possibly finding faults in the simulation, rather than the code....
Best Wishes |
|
|
|