PDA

View Full Version : ADC PIC18F2431 or PIC18F4431



mark_s
- 24th August 2010, 16:18
I have been trying to figure out how to use the
ADCIN command with the PIC18F2431 or PIC18F4431.

My problem is using more than one channel. I can get AN0
to work or AN1. But not togeather. It may be that the results are stored
in the fifo, which ADCIN does not address?

If anybody knows how to use the ADC in these chips. With or without adcin and
can share that would be great.

I just want to read some pots on AN0 and AN1, nothing fast.

Thanks
Mark

code
'************************************************* ***************
'* Name : ADC TEST PIC18F2331 - 4431 *
'* HIGH SPEED ADC AND PBP *
'* Notice : Copyright (c) 2010 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 8/24/2010 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************

DEFINE OSC 20

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 LCD_DREG PORTD '
DEFINE LCD_DBIT 0 ' STARTING BIT LCD PORTD.0 TO PD.3
DEFINE LCD_RSREG PORTD ' PORTD RS REGISTER
DEFINE LCD_RSBIT 4 ' LCD RESET PORTD.4
DEFINE LCD_EREG PORTD ' PORTD E REGISTER
DEFINE LCD_EBIT 5 ' LCD ENABLE PORTD.5
DEFINE LCD_BITS 4 ' 4 BIT INPUT VS 8 BIT
DEFINE LCD_LINES 4 ' LCD DISPLAY LINES


TRISA =%00011111
LATA =%00000000
TRISB =%00001111
LATB =%00000000
TRISC =%00000001
LATC =%00000000
TRISD =%00000000
LATD =%00000000
TRISE =%00000010

'AD REGISTERS
ADCON0 = %00000001 'AD ENABLED
ADCON1 = %00010000 'VREF = RAILS,FIFO
ADCON2 = %00111001 'LEFT JUSTIFIED, 16 TAD, FOSC/8
ADCON3 = %11000000 'AD INTERUPTS & TRIGGERS DISABLED
ADCHS = %00000000 ' ENABLED CH AN0,AN1,AN2,AN3
ANSEL0 = %00000011 'Select Analog channels AN0:AN2
ANSEL1 = %00000000 'Not used with PIC18F2331, 2431
'ADPNT ????????????? NEED TO POINT PROPER FIFO REG FOR RESULTS???

ADC_AN0 var word
ADC_AN1 var word

MAIN:
PORTE.0 = 1
ADCIN 0,ADC_AN0
ADCIN 1,ADC_AN1
PAUSE 200
PORTE.0 = 0
'4X20 LCD DISPLAY AD RESULTS AN0, AN1
LCDOUT $FE, $80, "PIC18F2431 ADC TEST "
LCDOUT $FE, $C0, "AN0 ",DEC4 ADC_AN0," "
LCDOUT $FE, $94, "AN1 ",DEC4 ADC_AN1," "
LCDOUT $FE, $d4, " "
PAUSE 200
GOTO MAIN

END
/code

Acetronics2
- 24th August 2010, 16:58
Hi, Mark

I do not think PBP can directly do what you are expecting ...

you should have a closer read of the ADC chapter of your datasheet and , after , drive the relevant registers ...
that are somewhat more numerous than those driven by a classical Adcin PBP command.

You'll also note there are fixed input groups, and Channel 0 + Channel 1 is not a valid group ...

PBP doesn't do everything for you, in short terms ... ;)

Alain

mark_s
- 24th August 2010, 17:33
Hi, Mark

I do not think PBP can directly do what you are expecting ...

you should have a closer read of the ADC chapter of your datasheet and , after , drive the relevant registers ...
that are somewhat more numerous than those driven by a classical Adcin PBP command.

You'll also note there are fixed input groups, and Channel 0 + Channel 1 is not a valid group ...

PBP doesn't do everything for you, in short terms ... ;)

Alain

Hi Alain,

Thanks for your reply. Yes I concluded last night that adcin would not work, but was
hoping otherwise!
Most likely will need a routine to set the group, channel, GO/DONE and store results on each read.

Regards
Mark

Bruce
- 24th August 2010, 21:46
Here's one example of A/D with this series. http://www.picbasic.co.uk/forum/showthread.php?t=6768

mark_s
- 24th August 2010, 22:53
Here's one example of A/D with this series. http://www.picbasic.co.uk/forum/showthread.php?t=6768

Thanks Bruce!

Perfect

I searched the forum for pic18f4431, not pic18f4331:o

Regards

BigWumpus
- 6th December 2010, 20:24
Today I trapped into the same hole...

ADCIN doesn't work on a PIC18Fxx31.

I have just written some code to read AN0..AN7 on devices who have this inputs.

Init-Part:


;ADC
ADON=true:ADCON2=%11101101 ;FOSC/16 AN0-AN4=analog right-justified, 40xTAD Sample
ANSEL0=%00011111


Inside Main-Code:


;ADC-Zyklus
;messen, umrechnen und gewichtet mitteln
;CtADC contains the ADC-channel
ADCHS=0:If CtADC.2 Then ADCHS=%01010101 ;Channel-Select
ADCON0.3=CtADC.1:ADCON0.2=CtADC.0
GO=true:Repeat:Until !GO ;ADC starten
DummyW.Highbyte=ADRESH:DummyW.Lowbyte=ADRESL