PDA

View Full Version : ADC on 12F683



JakeMend
- 6th July 2017, 22:36
I am not able to get the ADC on a 12F683 to work. The values it reports fluctuate seemingly randomly.

I have two 4.7K resistors attached in series between +5V and GND. A wire from Pin 4 (bit 3) is attached between them so I should be seeing a constant 2.5V. But instead, the ADC gives me numbers fluctuating from 0 to 255.

I am using the program (ADCINX4) that I got off the meLABS site, but it does not seem to work. The only change I made to the program below is to make my LCD display work.

I had tried a number of things on my own, but none of them worked, so I am turning to this collection of experts for help.

I had been using the POT command in the past and it worked fine, I was told that ADC was a better way to go.

Thanks!

-Jake



' Name : ADCINX4.pbp
' Compiler : PICBASIC PRO Compiler 2.6
' Assembler : PM or MPASM
' Target PIC : PIC12F683
' Hardware : LAB-X4 Experimenter Board
' Oscillator : 4MHz internal
' Keywords : ADCIN, SEROUT
' Description : PICBASIC PRO program to display result of 8-bit A/D
' conversion on LCD.
'

Include "modedefs.bas" ' Mode definitions for Serout

' Define ADCIN parameters
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

LCD Var GPIO.1 ' LCD TX pin

' I added these lines to make the chip work with my LCD display
I var byte '*
I = 254 '*
clr var byte '*
clr = 1 '*
hm var byte '*
hm = 2 '*
L1 var byte '*
L1 = 128 '*
'*

adval Var byte ' Create adval to store result

ANSEL = %00111000 ' Set AN3 analog, rest digital
CMCON0 = 7 ' Analog comparators off
Pause 1000 ' Wait 1 second for LCD to init

serout lcd, 4, [I, clr] ' getting the LCD ready to display
serout lcd, 4, [I, hm]
serout lcd, 4, ["ready to begin"]
pause 2000
serout lcd, 4, [I, clr]

mainloop:
ADCIN 3, adval ' Read channel 3 to adval

Serout LCD, 4, [I, clr] ' Clear screen
Serout LCD, 4, ["Value: ", #adval] ' Display the decimal value

Pause 200 ' Wait 0.2 second
Goto mainloop ' Do it forever

End

JakeMend
- 7th July 2017, 00:44
I found my mistake:

Pin 4 is GP3

Pin 3 is GP4, but AN3

I had the wire attached to the wrong pin.

Duh!