PDA

View Full Version : 16F684 AtoD problem



hell_pk
- 11th February 2009, 04:23
Hi!
I am trying ADC of 16F684 with this simple code.The problem is that the analog input channels are pulled high.I have tried with different registers but all in vain.


PORTC=0
CMCON0=$07
'CMCON0=$00
ANSEL=$00
TRISC=$00
WPUA=$00 'doesnt effect the output
'OSCCON=%1110111

'configure ADC
ADCON1=%01110000
TRISA.0=1
PORTA=$00
ANSEL.0=1

ADCON0=%00000001

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

adval var word ' Create adval to store result
Res Var word


Define LCD_DREG PORTC
Define LCD_DBIT 0
Define LCD_RSREG PORTC
Define LCD_RSBIT 4
Define LCD_EREG PORTC
Define LCD_EBIT 5
DEFINE LCD_BITS 4


Lcdout $fe,1
loop:
ADCIN 0, adval

Res = 2*adval

Lcdout $fe, $80
Lcdout "T:", DEC2 Res,"C" 'text for the line 1

Pause 300
goto loop

mackrackit
- 11th February 2009, 20:43
I think this is all you need to set for this PIC. I have not used one for awhile...


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
TRISA.0=1
ANSEL.0=1

hell_pk
- 13th February 2009, 05:20
I have Set these bits in my program but still the port pins are pulled up.

mackrackit
- 13th February 2009, 05:44
What is your analog source?
Can you post a schematic and your current code?

hell_pk
- 16th February 2009, 04:25
I have already posted the code.Analog source is simply a pot for 1-5V range.

Archangel
- 16th February 2009, 07:19
Hello hell_pk,
Try adding OPTION_REG.7 = 0 to the upper area of you code.

hell_pk
- 17th February 2009, 09:49
I have added the line but still no result.very strange.
i have configured all registers ANSEL,WUPA etc in program.
What am I missing!!!

Adrian
- 17th February 2009, 14:43
You have all the inputs as digital inputs ANSEL = $00 Shouldn't A0 be an analogue input?

Adrian

mister_e
- 17th February 2009, 16:07
Out of curiosity, which compiler version are you using?

mackrackit
- 17th February 2009, 22:41
I have already posted the code.Analog source is simply a pot for 1-5V range.
Post #1 shows a lot of things but it is difficult to tell where the code is or the things you hace tried. Post your current code in tags like Mr.E suggest.

You have ANSEL set two different ways in post #1. Is that the way you have it in your code?

Did you read and try post #2? Some of the registers PBP handles for you when using ADCIN.

hell_pk
- 18th February 2009, 09:17
Compiler ver 2.50



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

OPTION_REG.7 = 0
PORTC=0

CMCON0=$07
'CMCON0=$00
ANSEL=$01
TRISA=$01
TRISC=$00
WPUA=$00
'OSCCON=%1110111

'configure ADC
ADCON1=%01110000

PORTA=$00

ADCON0=%00000001

adval var word ' Create adval to store result
Res Var word

' Define LCD registers and bits

Define LCD_DREG PORTC
Define LCD_DBIT 0
Define LCD_RSREG PORTC
Define LCD_RSBIT 4
Define LCD_EREG PORTC
Define LCD_EBIT 5
DEFINE LCD_BITS 4



Lcdout $fe,1
loop:
ADCIN 0, adval
Res = 2*adval

Pause 300

Lcdout $fe, $80
Lcdout "T:", DEC2 Res,"C" 'text for the line 1

Lcdout $fe, $80+8 ,#Res
Lcdout $fe, $C0 'set cursor at the beginning of line 2
Lcdout "V: ", #adval 'formatted text for line 2


Pause 300
goto loop

mackrackit
- 18th February 2009, 15:08
Try commenting out these lines


ADCON1=%01110000

PORTA=$00

ADCON0=%00000001

I think they may be causing a conflict.

hell_pk
- 20th February 2009, 04:34
Still no luck.The Analog channel pulls up before ADCIN command therefore something must be wrong with register configs.
very frustrating!

mackrackit
- 20th February 2009, 06:59
Sorry for all the confusion, here is some tested code.
I have it running right now.


<html>
<head></head>
<body><!--StartFragment--><pre><code><font color="#00FF00"><b><i>'16F684 ADCIN TEST

</i></b></font><font color="#008000">@ __config _INTRC_OSC_NOCLKOUT &amp; _WDT_ON &amp; _MCLRE_OFF &amp; _CP_OFF

</font><font color="#0000FF"><b>DEFINE </b></font><font color="#FF0000">ADC_BITS </font>8 <font color="#00FF00"><b><i>' Set number of bits in result
</i></b></font><font color="#0000FF"><b>DEFINE </b></font><font color="#FF0000">ADC_CLOCK </font>3 <font color="#00FF00"><b><i>' Set clock source (3=rc)
</i></b></font><font color="#0000FF"><b>DEFINE </b></font><font color="#FF0000">ADC_SAMPLEUS </font>50 <font color="#00FF00"><b><i>' Set sampling time in uS
</i></b></font><font color="#FF0000">TRISA</font>=%00000001
<font color="#FF0000">ANSEL</font>=%00000001
<font color="#FF0000">ADX</font> <font color="#0000FF"><b>VAR</b></font> <font color="#0000FF"><b>BYTE
</b></font><font color="#FF0000">START</font>:
<font color="#0000FF"><b>HIGH </b></font><font color="#FF0000">PORTC</font>.4
<font color="#0000FF"><b>PAUSE </b></font>250
<font color="#0000FF"><b>LOW </b></font><font color="#FF0000">PORTC</font>.4
<font color="#0000FF"><b>ADCIN </b></font>0, <font color="#FF0000">ADX
</font><font color="#0000FF"><b>PAUSE </b></font>250
<font color="#0000FF"><b>SEROUT2 </b></font><font color="#FF0000">PORTC</font>.5, 17197, [<font color="#0000FF"><b>DEC </b></font><font color="#FF0000">ADX</font>, $d, $a]
<font color="#0000FF"><b>GOTO </b></font><font color="#FF0000">START
</font></code></pre><!--EndFragment--></body>
</html>