View previous topic :: View next topic |
Author |
Message |
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
Setup Vref for analog |
Posted: Sun Oct 07, 2007 2:47 pm |
|
|
How do you setup the vref on the analog pin if you want to use say 5v on a pot but the pic uses 3v. I tried the folowing setup but it does not seem to work.
Code: | define VSS_VREF 0x1000
setup_adc_ports( ALL_ANALOG| VSS_VREF );
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel(1);
setup_vref(pin_b2);
#define P1D PIN_B4
setup_timer_2(T2_DIV_BY_16, 255, 1); // 1000 Hz
output_low(P1D);
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
while(1){
value = read_adc();
//if (value > 144){
////value = (value-128)*2;
set_pwm1_duty(value);
setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_d);
//} |
|
|
|
Ttelmah Guest
|
|
Posted: Sun Oct 07, 2007 2:55 pm |
|
|
You can't.
Read the data sheet. Look at the limits for Vref. Just as for any other pin, it is (effectively) diode clamped to the supply rails. If you fed 5v into Vref, the effect would be to try to pull the supply rail up to about 4.5v...
Best Wishes |
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Sun Oct 07, 2007 2:57 pm |
|
|
If that is not possible how would it be possible to read the correct 8bit value from a pot if vdd is not available externally? |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Sun Oct 07, 2007 7:14 pm |
|
|
If you want to measure 0V-5V on a 3V system just use a voltage divider to scale 5V down to about 2.9V.
Is that what you are asking?
John |
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Mon Oct 08, 2007 4:17 am |
|
|
Yes that is basically what I wanted to know. I tried using say 2*1.5V batteries in series but the value of the analog is completely wrong. So I soldered a piece of wire from Vdd pin of pic regulator to pot and that seems to work but i don't think that that would be a recommended practice.
Can someone please explain why there is a #define Vdd_Vref in the header file if it is not possible to use another reference value on the pic than the Vdd of the pic itself or equivalent value?? |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Mon Oct 08, 2007 5:26 am |
|
|
Gerhard wrote: | Can someone please explain why there is a #define Vdd_Vref in the header file if it is not posible to use another reference value on the pic than the Vdd of the pic itself or equavalent value?? |
Gerhard wrote: | How do you setup the vref on the analog pin if you want to use say 5v on a pot but the pic uses 3v. I tried the folowing setup but it does not seem to work.
|
Quote: | 8.7 Analog Input Connection
Considerations
A simplified circuit for an analog input is shown in
Figure 8-6. Since the analog input pins share their connection
with a digital input, they have reverse biased
ESD protection diodes to VDD and VSS. The analog
input, therefore, must be between VSS and VDD. If the
input voltage deviates from this range by more than
0.6V in either direction, one of the diodes is forward
biased and a latch-up may occur. |
Quote: | 9.1.3 ADC VOLTAGE REFERENCE
The VCFG bits of the ADCON0 register provide
independent control of the positive and negative
voltage references. The positive voltage reference can
be either VDD or an external voltage source. Likewise,
the negative voltage reference can be either VSS or an
external voltage source. |
The option is given to you for the reasons stated in the data sheet. You may either use Vdd or an external voltage for the positive voltage reference.
IF you plan on using an external Vref, then it must be between Vss and Vdd.
I don't understand whether you are trying to use a pot to have an adjustable Vref or you are trying to measure a voltage across a pot? |
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Mon Oct 08, 2007 5:47 am |
|
|
The way i was planning to use it is as follows.
Pot has 3 legs.
1 = Vcc
2 = Vadj
3 = Gnd
By conecting Vcc to 5volt and 3 to ground leg 2 is conected to RA1. By adjusting the pot I efectively made a voltage divider that the analog input can read between 0v and 5volt. But the pic works on 3 volt so the calibration is of. I will use a 3volt regulator seeing that the pic's vdd pin is not acesible. Is this the normal way to do it or how?
I see that i cannot use 5volt. Thanks |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Mon Oct 08, 2007 5:56 am |
|
|
Are you trying to measure the position of the pot (i.e. a rotary dial)?
Can you provide a schematic? Trying to understand what you are doing and why, is extremely confusing.
If you MUST put 5V into your pot, then simply divide the output voltage (the wiper), down to a maximum of Vdd.
I don't understand why you can't use Vdd at the pot? |
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Mon Oct 08, 2007 6:11 am |
|
|
I am a student using a ready made board that has a 16f886(soic) pic mounted on it as well as a cc1000 rf sender and all the other electronics needed to program and comunicate.
The pin that comes out of the board is a pin that suplies the pic vdd through a 3volt regulator. I tried posting the schematic but it doesnt work.
The pin that comes out of the board can be conected to a supply voltage between 5 and 18volt. and the regulator then suplies the pic with the 3v. The pot i am using are one of these joysticks that you find on the remote control car's. When the joystic is pushed fully forward there is a Resistance of 9K and in the middle the resistance is 4.5k and pushed fully back it reads 50ohm.
I need the value of the analog input to be 255 when fully forward, 128 in the middle and close to zero when pushed fully back, so that i can use that value as the dity cycle of the pwm output.
Is this better or still unclear?? |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Mon Oct 08, 2007 8:31 am |
|
|
MUCH BETTER!
Tap off of Vdd some where on the board and use it for one leg of your pot. Opposite leg to ground and wiper to the AN pin (just as you've been trying with 5V.)
Everything else should be easy from there.
After you get that done, what's your next question or area of confusion? |
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Mon Oct 08, 2007 11:47 am |
|
|
That is the problem. I cannot tap of Vdd from the board. I will use a 3volt regulator instead. |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Mon Oct 08, 2007 12:38 pm |
|
|
Gerhard wrote: | That is the problem. I cannot tap of Vdd from the board. |
Why not? (not allowed to?, can't figure out how to?)
What board is it? (manufacturer, part number ,etc.) |
|
|
|