PDA

View Full Version : Adcin or Pot



lerameur
- 7th January 2007, 19:19
Hi, I wrote a program and I was wandering if it is better to use the Pot command or the Adcin command ?
Also , I wrote this small program. I have two potentiometer, I connected them to the input pin and to the positive supply . Is this good ?
I get somewhat good result with one, but when i put two, the second pot stays at 255..


'TRANSMIT PIC
INCLUDE "modedefs.bas"
DEFINE OSC 20 'use external 20mhz crystal
CMCON = 7 : ANSEL = 0 : ADCON1 = 7
DEFINE LCD_DREG PORTB ' Set LCD Data port
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 1 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 0 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2500
DEFINE LCD_DATAUS 250
DEFINE CHAR_PACING 2000

' Define ADCIN parameters
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

ADCON1 = %11000001 ' Set PORTA analog and RIGHT justify result
ADCON0 = %00000001 ' Configure and turn on A/D Module


TRISB = 0
TRISA = 1
'27
temp var word : right var byte : left var Byte : oldright var byte : oldleft var byte
encoded1 var word
'
'Leftwheel =CW : Rightwheel= CW '---- $66 = 01 10 01 10
'Leftwheel =CW : Rightwheel= CCW '---- $56 = 01 01 01 10
'Leftwheel =CCW : Rightwheel= CW '---- $5a = 01 01 10 10
'Leftwheel =CCW : Rightwheel= CCW '---- $65 = 01 10 01 01
'
loop:

ADCON0.2 = 1 'Start Conversion

oldright = right 'Pot reading 1
oldleft = left 'Pot reading 2
ADCIN 0, right 'Read channel PORTA.0
pause 50
ADCIN 1, left 'Read channel PORTA.1
pause 50

right = (right +oldright)/2 'Reducing the fluctuating output by taking an average
left = (left +oldleft)/2

Lcdout $fe, 1 'Clear screen
Lcdout "Right: ", Dec right 'Display the decimal value
Lcdout $fe, $C0, "Left: ", DEC left 'Display the decimal value
pause 100

goto loop
end

mister_e
- 7th January 2007, 21:39
Depending of your PIC, your ADCON, ANSEL setting have to be set to make AN0, AN1 as analog first. Your sampling time and clock source look fine... even if they are way too slow. Too slow is still better than to fast, so it's not your problem source here.

Your pot have to be connected between Vdd and Vss. The wiper goes to the PIC input. It's value must be equal OR less than the maximum recommended by your datasheet. It's a good idea to filter the POT output with a capacitor (10-100 nF)

malc-c
- 7th January 2007, 22:04
I tend to use a 10k Lin pot with one leg connected to +ve (5v) and a 0.1uf cap between the other leg and GND - the wiper then goes to the PIN.

Using the scale value in the POT statement you should be able to get the range right so the value is 0 at one end of the travel, to 255 at the other.

Edit - just noticed that you have an LCD attached - then using


Pot PORTA.0,scale,D
LCDOUT $FE,$C0, "Cal value " ,#D ; where D is the varible used in the POT statement

would show the value for the variable D in deceimal on the second line, so you can calibrate the range exactly

lerameur
- 7th January 2007, 23:51
I barely turn the Pot and it reaches goes from 0 to 255 in no time, I mean barely, I tried all the values for the sale ranging from 0 to 255 passing through 127, it react the same way.
My left leg of the pot is on GRD the middle is on the pin, and right leg on the Vcc. with 0.1uF cap gnd to pin.
is that it ?

i kept is very simple:

INCLUDE "modedefs.bas"
DEFINE OSC 20 'use external 20mhz crystal
CMCON = 7 : ANSEL = 0 : ADCON1 = 7
DEFINE LCD_DREG PORTB ' Set LCD Data port
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 1 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 0 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2500
DEFINE LCD_DATAUS 250
DEFINE CHAR_PACING 2000


TRISB = 0
TRISA = 1
'27
temp var word : right var byte : left var Byte : oldright var byte : oldleft var byte

loop:


oldright = right 'Pot reading 1
oldleft = left 'Pot reading 2

Pot PORTA.0,127,right
Pot PORTA.1,127,left
right = (right +oldright)/2 'Reducing the fluctuating output by taking an average
left = (left +oldleft)/2

Lcdout $fe, 1 'Clear screen
Lcdout "Right: ", Dec right 'Display the decimal value
Lcdout $fe, $C0, "Left: ", DEC left 'Display the decimal value
pause 100

goto loop
end

mister_e
- 8th January 2007, 05:23
if you're using POT you have it wrong. look in the PBP manual. Depending your POT impedance, the capacitor might be too low... i hate POT. your PIC have ADCs.... just use it with ADCIN and set the according i/o properly.

malc-c
- 8th January 2007, 10:12
My left leg of the pot is on GRD the middle is on the pin, and right leg on the Vcc. with 0.1uF cap gnd to pin.
is that it ?

If I read this correctly you have placed the capacitor in parallel with the left leg of the pot. If so this is wrong, you need to place it in series with the pot.

Using a 10K lin pot and a 0.1uf capacitor and a scale of 254 I get a range of 0 - 255 in the last 30% of travel, regardless of the value of scale. However, if a lower value of scale is used, the travel range is reduced, but with a higher value I get the full range (0 - 255) within this 30% of travel. I think that the choice of potentiometer and or the value of the capacitor used is critical if you want to get a full range across 100% of the travel of the pot.

peterdeco1
- 8th January 2007, 10:36
Check your statement TRISA=1. I believe you are making all PORTA as outputs except PORTA.0.

mister_e
- 8th January 2007, 17:52
Yup but POT 'Should' do the job... As output first to charge/discharge the capacitor, then set as input to evaluate the RC plah plah.

If the POT/Capacitors value match is not good AND the 'way to use them' is not good... of course results will be bad.

sayzer
- 8th January 2007, 18:52
In the schematic above, why the POT is connected to +5V ?

Any particular reason?

------------------

Archangel
- 9th January 2007, 00:40
Are you using a linear taper Pot? Audio taper pots have a logarithmic scale to their resistance and would likely present a similar responce.
JS

malc-c
- 9th January 2007, 10:39
Well, I ordered them as Lin pots.. but knowing the numbskulls in the local shop they could well indeed be log

lerameur
- 9th January 2007, 17:34
I am using a 47K linear POT.
I tries usin a 0.1 UF capacitor, I was barely moving the pot and it reaches 255, bu adding 2 0.1 uF in series I was getting a little bit further. Then any changes in pot values did not change a thing. Decreasing the scale also help going a bi further. I scaled it down to decimal 2 (out of 255) and 2 x 0.1 uF caps. i am only getting halfway with the pot. i would like to go all the way.... what should I try..

skimask
- 9th January 2007, 18:03
I am using a 47K linear POT.
I tries usin a 0.1 UF capacitor, I was barely moving the pot and it reaches 255, bu adding 2 0.1 uF in series I was getting a little bit further. Then any changes in pot values did not change a thing. Decreasing the scale also help going a bi further. I scaled it down to decimal 2 (out of 255) and 2 x 0.1 uF caps. i am only getting halfway with the pot. i would like to go all the way.... what should I try..

What are your Vref+ and Vref- settings on?

lerameur
- 9th January 2007, 18:07
i did not set them , so they should be using the default 5v
Is it needed fot eh POt command, i thout it was for the adcin (a/d)
I tried it anyway , put Vref+ to 2.5v,no changes

malc-c
- 9th January 2007, 19:50
OK I've just had a further look at the manual, and there was no connection of the second leg to the +ve, however disconnecting the link between the +5v and the pot resulted in the value of D (in the above example) to 1 regardless of where the pot was set. Reconnecting the leg of the POT to +ve and the value changed back to 255 !

Looking at the manual it states

The resistance is measured by timing the discharge of a capacitor through the resistor (typically 5K to 50K). Scale is used to adjust for varying RC constants. For larger RC constants, Scale should be set low (a minimum value of one). For smaller RC constants, Scale should be set to its maximum value (255). If Scale is set correctly, Var should be zero near minimum resistance and 255 near maximum resistance.

Unfortunately, Scale must be determined experimentally. To do so, set the device under measure to maximum resistance and read it with Scale set to 127. Adjust Scale until the Pot command returns 254. If 255, decrease the scale. If 253 or lower, increase the scale.

Whats happening here is that your not measuring the voltage and then converting that analogue voltage into a digital value as per A to D convertion. The resistor charges the capacitor, which then discharges, which results in a negative going pulse on the pin. The frequency of this pulse determins the value of being stored in the variable (D in my example above). Here are some traces showing the pulse rate when D was 0, around 20 and at 255.

lerameur
- 9th January 2007, 20:38
I am not using A/D yu are right, but it still do not work , maybe i should use the A/D and change the reference voltage

Ok i juste tried A/D works good
Pot sucks.. ;)

mister_e
- 9th January 2007, 23:26
Je te l'avais dit ;)

I told you!

lerameur
- 11th January 2007, 16:39
oui je sais caline..

Acetronics2
- 11th January 2007, 16:46
oui je sais caline..

I didn't know the specie-that-grows-but-never-reproduces had haunted this forum ... LOL

Being serious ... FORGET the "POT" Command ... and use RCTime instead !!!
ADC not really compulsory for numerous applications.

Alaain