CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

about ADC

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rezaf



Joined: 12 Oct 2008
Posts: 7

View user's profile Send private message

about ADC
PostPosted: Wed Aug 26, 2009 2:35 pm     Reply with quote

Hi
I have problem with ADC commands. I written the code below and in schematic connected a 5v with potentiometer to AN0 pin but when the voltage is 0v the printf command send "1" to RS232 simulator and when voltage is 0.5v the printf command send "102" to RS232 simulator. what is the problem ?? how can I show the real voltage number in printf command ??
(I have tested %d-%Lu and etc.)
Code:
do {
     set_adc_channel(0);
     delay_us(5);
     value = read_adc();
    printf("\r\n\nCalibrated A/D = %d\n\r",value);
 }while(true);
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 26, 2009 2:41 pm     Reply with quote

Quote:
how can I show the real voltage number in printf command ??

See this post for sample code:
http://www.ccsinfo.com/forum/viewtopic.php?t=32168&start=1
Ttelmah
Guest







PostPosted: Wed Aug 26, 2009 2:50 pm     Reply with quote

The ADC returns just an integer number.
It's actual value, will depend on what you are using as a reference. So if (for instance), you are using the 5v supply (not very accurate....), you will get a number from 0 to 255 (if your ADC is set to 8bit - look at the #device statements), or 0 to 1023 (if the ADC is set to 10bit - again look at the #device statement....).
Converting it to a displayable 'voltage', will depend on how much time you want to take, and how accurate you want the result to be.
Assuming a standard ADC, that you _have_ selected 10bit with the device statement, and you are running from the 5v supply, the 'best' conversion will be:
Code:

//remember value _must_ be declared as a int16, or long
do {
set_adc_channel(0);
delay_us(5);
value = read_adc();
value = (((int32)value*5000)/1024)+5;
value=value/10;
printf("\r\n\nCalibrated A/D = %4.2Lw\n\r",value);
}while(true);


This gives the closest possible result with two displayed digits after the decimal point, for the PIC ADC (research the PIC data sheets to see why the divisor is /1024, not /1023, and why acount of 5 is added....).

Best Wishes
rezaf



Joined: 12 Oct 2008
Posts: 7

View user's profile Send private message

Re
PostPosted: Thu Aug 27, 2009 5:33 am     Reply with quote

thanks PCM programmer for the sample code link. thanks Ttelmah for your guide, but say to me that the formula is experimental or is constant ? how can I calculate the formula for other conversion ?

I want to read amplified output voltage of IR LED (RX) that is between 0 to 2.5 volt and connected to pin AN0 and show the number of voltage on LED Bar that connected to port B(according to voltage, LEDs go high)with OUTPUT_B(0xxx) command. do I must give Vref to PIC ?

Best Regards.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Thu Aug 27, 2009 8:28 am     Reply with quote

Too lazy to even read the datasheet after Ttelmah pointed it out to you right?

When you set up the adc peripheral on the PIC you have to give it a reference voltage. If you don't do it the compiler assumes you are using Vcc - Vss. Since Vss is GND this means the reference voltage by default is just Vcc. Since Vcc is 5000mV that is where the 5000 comes in. Since you are presumably using 10 bit adc, 2^10 is 1024 and that is where that comes in.

You can set up the adc so that it uses an external Vref (2.5V, 1.2V or whatever), you stick that on your VrefH pin and have to tell the compiler to use that Vref instead of Vcc. For series 18 devices the setup is something like:

Code:

SETUP_ADC_PORTS(AN0 | VSS_VREF)


so that if your Vref is 2.5V the adc will return 1023 when your input is 2.5V.
rezaf



Joined: 12 Oct 2008
Posts: 7

View user's profile Send private message

Re
PostPosted: Fri Aug 28, 2009 3:42 am     Reply with quote

sorry I am not lazy don't judge quickly. the microchip's datasheet is not for amateurs also my english is not very good. I read that several times from when I work on PIC but always go confused.
thanks for guides.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group