A/D code on a PIC

I can't get the A/D inputs to work. What blindingly-obvious thing am I missing?

#include config LVP = OFF config XINST = OFF config CPUDIV = OSC1_PLL2 config PLLDIV = 1 config WDT = OFF config MCLRE = OFF config DEBUG = OFF config PBADEN = ON config LPT1OSC = OFF config FOSC = INTOSCIO_EC

org 0 ;============================= ; Set up I/O ports ;============================= MOVLW 0x2F ; make A0-A3,A5 analog in; A4, A6 digital out, MOVWF TRISA MOVLW 0x1E ; make B1-B4 analog in, B0,B5-B7 digital out, MOVWF TRISB CLRF TRISC ; Make PortC digital out

;====================================== ; register, A/D initialization ;====================================== CLRF PORTA CLRF PORTB CLRF PORTC

BSF ADCON2,7 ; right justify BSF ADCON2,4 ; set Tad = 32 BSF ADCON2,2 ; Fosc/4

MOVLW 0x03 MOVWF ADCON1 ; set inputs AN0-AN10, vrefs Vdd/Vss

goto start

ADCREAD MOVWF ADCON0 ; set A/D to channel defined in WREG BSF ADCON0,0 ; start conversion CALL ONE_US CALL ONE_US CALL ONE_US ; wait 5us for A/D to settle CALL ONE_US CALL ONE_US BSF ADCON0,1 ; set the GO bit BTFSC ADCON0,1 ; GO bit will change to zero when the conversion is complete goto $-2

MOVFF ADRESH, TEMPHI MOVFF ADRESL, TEMPLO

BCF ADCON0,0 ; turn off A/D RETURN

START MOVLW 0x00 ; set ADC to read AN0 CALL ADCREAD END

Reply to
Randy Day
Loading thread data ...

complete

Am I reading this right? You set the go/done bit (adcon0,1) then test to see if it is low. How could it ever be low if you just set it in the previous line? Don't you have to set it after the conversion is read? Bob

Reply to
Bob Eld

complete

Maybe "goto $-2" should be "goto $-1"

Reply to
Donkey D...

complete

Am I reading this right? You set the go/done bit (adcon0,1) then test to see if it is low. How could it ever be low if you just set it in the previous line? Don't you have to preset it up where you start conversion right after the ADC is started, third line in ADCREAD before the delays? Bob

Reply to
Bob Eld

Actually, no. The '2455 is word-addressable. $-2 takes you back to the prev instruction. That threw me too when I went from PIC16's to 18's. :)

Reply to
Randy Day

My reading of the datasheet is that the micro sets the go/done bit low when it is finished, although the text is a bit ambiguous. From the datasheet:

"When the A/D conversion is complete, the result is loaded into the ADRESH:ADRESL register pair, the GO/DONE bit (ADCON0 register) is cleared..."

One thing I should mention is that my program does run, rather than locking up in the loop we're discussing. Obviously it's getting set low at some point, but I'm not seeing any useful readings.

I *so* wish they'd provided an example of A/D code in the datasheet. :(

Reply to
Randy Day

No appnotes?

Reply to
Homer J Simpson

see

previous

Yes, but you have to set the go/done bit high to start the process that's what "go"means. You can test the bit as you are doing to see if the process is complete but you should not reset it again until you want the next conversion. You certainly shouldn't set it before you test it even though there is a loop there. BTW if you have implemented a loop to test the bit, you don't need the delays because the test loop will create the necessary delay.

Reply to
Bob Eld

There are plenty of app ntes on the microchip site. You appear to be loading ADCON0 with 0x00 in your read routine which will turn the ad converter off.

Reply to
cbarn24050

You should take a look in the family reference manual for the part in question. It has sample code for devices such as the A/D.

BAJ

Reply to
Byron A Jeff

They have source for the 18C452, which does not appear to have an ADCON2 register, and uses register bits which are unused on the '2455. But they do use interrupts in a way I'll have to investigate further. :)

Is that not appropriate? I set the ADON bit on the next line. Should ADON be set once and left that way? These are the types of things I'm still not clear on.

Reply to
Randy Day

I see that now, you labeled it start conversion, typical aquisition is around 13uS you seem to only have 5uS. I cant see what your doing with the result.

Reply to
cbarn24050

I'll remember that. I haven't yet graduated from the 8-bit 16's

Reply to
Donkey D...

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.