PDA

View Full Version : ADC reading and configuration problem



pxidr84
- 25th March 2011, 18:15
Hi everyone,

I can't get my ADCs to working properly. I've tried to play with the registers, with no results.

I have to sample 4 analog inputs.

Only one reading is correct, it's the "tsense" refered to the ADCIN5. The others values displayed are completely crazy.

I'm using the PIC18F4431 ( datasheet : http://ww1.microchip.com/downloads/en/DeviceDoc/39616d.pdf )

The simple code used for the test :

DEFINE OSC 32
DEFINE LCD_DREG PORTC
DEFINE LCD_EREG PORTD
DEFINE LCD_RSREG PORTD
DEFINE LCD_EBIT 0
DEFINE LCD_RSBIT 1
DEFINE ADC_BITS 10

TRISA=%11111111
TRISB=%11000000
TRISC=%110000
TRISD=%11100
TRISE=%11111111

ANSEL0=%11111111
ADCON0=%00011111
ADCHS=%01010101

potsense var word
tsense var word
usense var word
isense var word

pause 2000
LCDOUT $fe,1

lp:

ADCIN 4,potsense
ADCIN 5,tsense
ADCIN 6,usense
ADCIN 7,isense

lcdout $fe,$2,DEC5 potsense," ",DEC5 tsense
lcdout $fe,$c0,DEC5 usense," ",DEC5 isense

pause 10

goto lp

mark_s
- 26th March 2011, 18:12
Hello,

I had the same problem a while back. The adcin command does not work with the
pic18fxx31s. You have to read them manually. See this thread it may help.

http://www.picbasic.co.uk/forum/showthread.php?t=13644&highlight=PIC18F2331

pxidr84
- 27th March 2011, 12:43
Hello,

I had the same problem a while back. The adcin command does not work with the
pic18fxx31s. You have to read them manually. See this thread it may help.

http://www.picbasic.co.uk/forum/showthread.php?t=13644&highlight=PIC18F2331

Hi,

I've tried this (from Bruce's code) :


DEFINE OSC 32
DEFINE LCD_DREG PORTC
DEFINE LCD_EREG PORTD
DEFINE LCD_RSREG PORTD
DEFINE LCD_EBIT 0
DEFINE LCD_RSBIT 1
DEFINE ADC_BITS 10

CH1 VAR WORD
CH2 VAR WORD
CH3 VAR WORD
CH4 VAR WORD

' Analog setup
' Single shot, multi-channel, simultaneous Mode2, Groups A+B, then C+D
ADCON0 = %00011101

' Set +/- Vref to AVdd/AVss, FIFO buffer enabled
ADCON1 = %00010000

ADCON2 = %11001111 ' Right justified, 24 Tad, Frc
ADCON3 = 0 ' Disable all triggers
ADCHS = 0 ' Channel select for AN0,1,2,3
ANSEL0 = %00001111 ' AN0,1,2,3 analog input, rest digital

Main:
ADCON0.1 = 1 ' Start the conversion
WHILE ADCON0.1=1 ' Wait for it to complete (all 4 channels)
wend

' FIFO buffer holds all 4 channels. Reading ADRESH & ADRESL automatically
' increments the buffer pointer (on read of ADRESL) to the next channels
' storage location.
CH1.HighByte = ADRESH ' get result from AN0
CH1.LowByte = ADRESL ' Increment buffer pointer

CH2.HighByte = ADRESH ' get result from AN1
CH2.LowByte = ADRESL ' Increment buffer pointer

CH3.HighByte = ADRESH ' get result fron AN2
CH3.LowByte = ADRESL ' Increment buffer pointer

CH4.HighByte = ADRESH ' get result from AN3
CH4.LowByte = ADRESL ' clears buffer pointer


' show 4 A/D channel results
lcdout $fe,$2,DEC5 CH1," ",dec5 CH2
lcdout $fe,$c0,DEC5 CH3," ",dec5 CH4

pause 100

GOTO Main

The readings works, but the lower bits of the reading are unstable, and I don't know why.

mackrackit
- 29th March 2011, 19:19
What is the source of the analog? Do you have smoothing capacitors from the ADC pins to VSS? Sometimes that helps.

pxidr84
- 30th March 2011, 17:57
What is the source of the analog? Do you have smoothing capacitors from the ADC pins to VSS? Sometimes that helps.

In fact I've resolved my problem. The result of the ADC was configured to be right-justified (ADCON2.7=1), I've set it to be left-justified (ADCON2.7=0), and I haven't problems anymore (the readings are perfectly stable now).