View previous topic :: View next topic |
Author |
Message |
mishell .g Guest
|
analog to digital general question |
Posted: Fri Oct 17, 2008 3:46 am |
|
|
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
|
|
Posted: Fri Oct 17, 2008 7:54 am |
|
|
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
|
|
Posted: Fri Oct 17, 2008 9:53 am |
|
|
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
|
|
Posted: Fri Oct 17, 2008 10:54 am |
|
|
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 |
Posted: Fri Oct 17, 2008 3:36 pm |
|
|
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
|
|
|
Guest
|
|
Posted: Sat Oct 18, 2008 7:09 am |
|
|
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: 1934 Location: Norman, OK
|
|
Posted: Sat Oct 18, 2008 7:49 am |
|
|
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
|
|
Posted: Sat Oct 18, 2008 7:52 am |
|
|
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: 1934 Location: Norman, OK
|
|
Posted: Sat Oct 18, 2008 7:58 am |
|
|
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
|
|
Posted: Sat Oct 18, 2008 8:11 am |
|
|
yes i was seen this after i posted this...
thank you. |
|
|
|