View previous topic :: View next topic |
Author |
Message |
BaidareW
Joined: 01 May 2005 Posts: 2 Location: Lithuania
|
ADC question |
Posted: Tue May 03, 2005 11:01 am |
|
|
I have PIC18f42 and I want to convert analog value to digital. Now I tested everything with my development board and it works. I measured 4,91V and I got the right result. But I am interested if I will have lets say -6V going to my R0 pin how I need to modify my programm to get right value ? First time posting to this forum, so sorry for possible errors
Here is my program:
Code: |
#include <18F452.h>
#device ADC=8
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
int8 laikinas[100];
signed int8 i;
void main()
{
//test
printf("labas visiems ");
i=0;
setup_port_a( AN0 ); //A0 pin
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );
while(i<98)
{
laikinas[i]=read_adc();
delay_us(10);
++i;
}
i=0;
while(i<98)
{
printf(" %u\n\r",laikinas[i]); //div 52 and we have result (255/4.91)
++i;
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 03, 2005 12:02 pm |
|
|
Quote: | If I will have lets say -6V going to my R0 pin |
Read section 22.0 of the 18F452 data sheet:
Quote: | Absolute Maximum Ratings:
Voltage on any pin with respect to VSS (except VDD, MCLR, and RA4) ..... -0.3V to (VDD + 0.3V) |
VSS is ground. This means the lowest voltage you can have on RA0
is -0.3 volts. So you can't directly measure negative voltages.
You could use an opamp circuit to invert the negative voltage so it's in
a positive range.
Or, you could use two resistors to make a voltage divider. About
half-way down the following page, there is a schematic diagram which
shows how to do this. Though, the resistor values they use are too
large for the PIC's A/D input requirements. You would need to divide
those resistor values by 10. Also, depending on the maximum negative
voltage you expect to apply, the ratio of the resistors might be different.
http://www.analog.com/library/analogDialogue/archives/33-01/chip/ |
|
|
BaidareW
Joined: 01 May 2005 Posts: 2 Location: Lithuania
|
|
Posted: Tue May 03, 2005 1:20 pm |
|
|
Thank you for your reply. It is sad that I need to have more things to do ADC besides my PIC So the minimum voltage I can have is -0.3 and maximum 13.25V.. if I want something more I need to play with all this for a while.. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 03, 2005 1:34 pm |
|
|
Quote: | So the minimum voltage I can have is -0.3 and maximum 13.25V. |
That's wrong. When you read a data sheet, you need to read it very
closely. The data sheet says:
Voltage on MCLR with respect to VSS (Note 2) ..... 0V to +13.25V
That 13.25v maximum only applies to the MCLR pin. Most other pins
have a much lower max allowable voltage. MCLR is special. It's used
to reset the PIC and to put it into programming mode.
To find the maximum voltage allowed on A/D pins, look in this section:
TABLE 22-21: A/D CONVERTER CHARACTERISTICS
It says the maximum analog input voltage is: AVDD + 0.3V
If you're using Vdd (likely +5v) as the analog reference, then this
means you can have a maximum of 5.3v on your A/D pins. |
|
|
|