PDA

View Full Version : 12F683 ADCIN - both channels the same



JetPack Spartan
- 7th January 2011, 03:27
I'm having a problem with reading two analog inputs. The first channel (GPIO.0) works well but the issue is that the next channel reads identically even though the voltage going into the pin is clearly different.

I must be missing something simple, I think that what I am trying to do is quite basic. I've looked at the PicBasic Pro manual, the datasheet and searched the forum. I did see that someone had a similar problem but the solution was not effective for me.

The wiring has been checked over many times, I am confident that the error is in the code.

The intention is to read the two axis and the button of a mini joystick and send the data serially to another uC.


@ DEVICE PIC12F683, MCLR_OFF
@ DEVICE PIC12F683, INTRC_OSC_NOCLKOUT
@ DEVICE PIC12F683, WDT_OFF
@ DEVICE PIC12F683, BOD_ON
@ DEVICE PIC12F683, PWRT_ON
'-------------------------------------------------------------------------------
DEFINE OSC 8
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
define CHAR_PACING 1000

OSCCON = %01110001 ' internal oscillator at 4 MHZ

CMCON0 = 0 ' Comparators off
ANSEL = %00000011 ' Set 0, 1, analog
WPU = 0 ' Internal pull-ups = off
GPIO = %00000000 ' All outputs = 0 on boot
TRISIO = %00011111 ' GPIO.0,1,2,3,4 input, GPIO.5 output

joyY var GPIO.0
joyX var GPIO.1
joyBtn var GPIO.2
Sout var GPIO.5

AD var byte
X var byte
Y var byte
Btn var byte
B var bit

Main:

adcin joyX, AD

X = AD
AD = 0

pause 1

adcin joyY, AD

Y = AD
AD = 0

B = joyBtn
if B then
Btn = Btn + 1
else
Btn = 0
endif

if Btn > 100 then Btn = 100

serout Sout, 2, ["cmd", X, Y, Btn, 128, 128]
goto Main

end

Rich H

mister_e
- 7th January 2011, 03:41
Yet another one where the PBP manual explain it all, but no one seems to bother reading :D Just kidding

ADCIN ADCHannel, Var

ADCHannel must be a decimal value, 0,1,2,3,4,5 etc... you use the ADC channel number, NOT the port PIN name.

ADCIN 0, Var
ADCIN 1, Var2
plah plah plah

JetPack Spartan
- 7th January 2011, 03:47
Ahh, I just knew it was going to be something simple.

Thanks!

Rich H

Acetronics2
- 7th January 2011, 08:04
Hi, Rich

The way to give a decent name to ADC inputs ... :



.
.
.
GPIO = %00000000 ' All outputs = 0 on boot
TRISIO = %00011111 ' GPIO.0,1,2,3,4 input, GPIO.5 output

joyY CON 0 ' ADC pin number must be set as a CONstant ...
joyX CON 1

joyBtn var GPIO.2
Sout var GPIO.5

AD var byte
X var byte

.
.
.

Main:

adcin joyX, AD

X = AD
AD = 0

pause 1

adcin joyY, AD

Y = AD

.
.
.




Alain