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 support@ccsinfo.com

analog to digital general question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
mishell .g
Guest







analog to digital general question
PostPosted: Fri Oct 17, 2008 3:46 am     Reply with quote

Hi
I have a good experience with pics but I need to do one simple thing that I don't know how: using the A\D.

I need to take external analog input to pic16f88/7 and check what is the binary number for that voltage. For example I need to input 4.2v and put the binary number of it in an integer (get the number to my software).
After that, to check if this integer is bigger then some other integer that I define (with the A\D of the pic16f88).
How do I get the binary number from analog input ? How is it represented ? Floating point ? What code required by the A\D ?

I will be more than happy to get example code for getting a number.

Thank you a lot.
Guest








PostPosted: Fri Oct 17, 2008 7:54 am     Reply with quote

There are several examples programs for using ADC supplied with the compiler. May not be specific to the 16F88 but should work the same way. Take a look at those examples.
mishell
Guest







PostPosted: Fri Oct 17, 2008 9:53 am     Reply with quote

Yes there is help file for everything but that wasnt my question.

i need to know how a fraction is presented by the chip ?
i need to check if the analog input is bigger then 4.25 so how do i write the number 4.25 in hex ?? floating point?

can someone give me example of how to check if a volatge is bigger then 4.25 ?

value=read_adc()
if (Value> ??????????? )
.
.
Guest








PostPosted: Fri Oct 17, 2008 10:54 am     Reply with quote

According to the data sheet, the 16f88 has a 10 bit ADC, and is a 5V part. This means that the PIC can measure voltages from 0V to 5V, with 10 bits of resolution. The digital value will be a 10 bit binary number. 5V corresponds to 0x3FF, or 1023. 0V corresponds to 0x000. To calculate a given voltages digital value, use this equation:

Digital Value = (Given Voltage *1024)/5V

So the value for 4.25V will be 870 (the actual value is 870.4, but the adc can't resolve 870.4, only 870 or 871.)
mishell
Guest







THANKS
PostPosted: Fri Oct 17, 2008 3:36 pm     Reply with quote

Thank you , it hepled a lot.

i just didnt understand-how do i present a number in software??
i cant just write 870 (=4.25v) ... ??
so how this 870 is writen? what are all the ways to write that?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 17, 2008 3:38 pm     Reply with quote

See this example program:
http://www.ccsinfo.com/forum/viewtopic.php?t=32168&start=1
Guest








PostPosted: Sat Oct 18, 2008 7:09 am     Reply with quote

thanks but it wasnt my question,

how do i present a number in ccs in hex?
lets say 870- how do i write it?

to compare to numbers:
value=read_adc()
if (value> ??????? ) // ???? = 870
dyeatman



Joined: 06 Sep 2003
Posts: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Sat Oct 18, 2008 7:49 am     Reply with quote

The link PCM gave you was EXACTLY your question:

Quote:
i just didnt understand-how do i present a number in software??
i cant just write 870 (=4.25v) ... ??
so how this 870 is writen?

You need to read it and understand it. It tells you how to convert and display the number 870 as a voltage using printf. You can also use printf to display it in hex if you wish. Here is a link to an example:

http://www.ccsinfo.com/forum/viewtopic.php?t=34753

As far as comparisons, yes, you can do it by comparing the raw ADC value (i.e. if value > 870, if value < 500.... etc.).


Last edited by dyeatman on Sat Oct 18, 2008 7:54 am; edited 1 time in total
mishell
Guest







PostPosted: Sat Oct 18, 2008 7:52 am     Reply with quote

I get error on ' setup_adc_ports(AN0); '
why is that?? is this code good generally?
thanx!

Code:
#include <16F88.h>
#device adc=10
#fuses INTRC_IO, NOWDT, NOBROWNOUT, PUT, NOLVP,NOMCLR
#use delay(clock=8000000)

void main() {

int16 adc_value;

setup_adc_ports(AN0);
set_adc_channel(0);
delay_us(20);


WHILE(1)
{

DELAY_MS(2000);
OUTPUT_HIGH(PIN_B1);
DELAY_MS(2000);

adc_value=READ_ADC();
if(adc_value>870)                         //870 IS 4.25V TO COMPARE TO
{OUTPUT_low(PIN_B1);}

}


}
dyeatman



Joined: 06 Sep 2003
Posts: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Sat Oct 18, 2008 7:58 am     Reply with quote

If you look in the 16F88.h file it shows the line should be:

Code:
setup_adc_ports(sAN0);


To be safe I would add the line

setup_comparator(NC_NC_NC_NC)

.
Guest








PostPosted: Sat Oct 18, 2008 8:11 am     Reply with quote

yes i was seen this after i posted this...
thank you.
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