PDA

View Full Version : A/D conversion problem on 18F4431



ttease
- 10th April 2007, 18:15
Hi,

Ver 2.46, MPMASM 5.1

I have the processor above with an analog accelerometer. The values I'm getting from the conversion are incorrect with respect to the reference voltage supplied. I have tried numerous configuration changes but no luck.

Setup:
Ref+ voltage is 2.6v on Pin RA3,
Ref- is Gnd.

measured voltages
RA0 = x-dir 1.1v
RA1 = y-dir .9v
RA2 = z-dir 1.8v

Acc. value 1g=800mV/g.

I was expecting to see the voltages for x and y to be ~1.3 volts or a value displayed of ~ 512 when the acc. is in the horizontal position. What I get now are reading of 900-1000. I tested this on an 16F877A and it preformed as expected. Also, I tried all 1.5, 2, 4, 6 g settings. No change.

Code is below:



DEFINE OSC 8
OSCCON = %01110010

'
' Define LCD pins
'
DEFINE LCD_DREG PORTD 'LCD data port
DEFINE LCD_DBIT 4 'LCD data starting bit 0 or 4
DEFINE LCD_BITS 4 'LCD bus size 4 or 8

DEFINE LCD_RSREG PORTC 'LCD register select port
DEFINE LCD_RSBIT 1 'LCD register select bit

DEFINE LCD_RWREG PORTC 'LCD read/write port
DEFINE LCD_RWBIT 2 'LCD read/write bit

DEFINE LCD_EREG PORTC 'LCD enable port
DEFINE LCD_EBIT 3 'LCD enable bit

wAdval var word
bLoop var byte
wMaxvalArray var word[3]
wMinvalArray var word[3]
m var byte

'I/O port defs 0=out, 1=in
' atod
TRISA = %00001111 ' AI 0=x-dir, AI 1=y-dir, 2=nc, AI 3=z-dir
' DO 4=g1, DO 5=g2, DO 6=nc, DO 7=nc
PORTA = 0

TRISB = %11111111 ' DI 0=sel, DI 1=up, DI 2=dn, DI 3=end
' DI 4=nc, DI 5=LVPGM, DI 6=PGC, DI 7=PGD
PORTB = 0 ' PORTB lines low to read buttons

TRISC = %00000000 ' All DO's 3 LCD, 4=cont, 5=LCD_RW, 6=LCD_RS, 7=LCD_E
PORTC = 0 ' PORTC lines low to read buttons

TRISD = %00000000 ' not used
PORTD = 0

TRISE = %00001000 ' DI 0=sleep, DO 1=nc, DO 2=nc, DI 3=MCLR
PORTE = 0

' port assignments
aACC_g1 var PortA.5
aACC_g2 var PortE.0
aACC_SLEEP var PortA.4

ANSEL0 = %00000111 ' set 0,1,2 to AI, rest to Digital
ANSEL1 = %00000000 ' set 0 to Digital, rest are n.c.

INTCON = %00000000
INTCON2 = %00000000
INTCON3 = %00000000

ADCON0 = %00000000 ' sin. shot meas, multichan, sim M1, DONE, a/d mod OFF
ADCON1 = %01011000 ' Vdd-ACC volt, Vss refs, FIFO en, buffer stuff
ADCON2 = %10000011 ' rt just., No conv delay, FRC/4
ADCON3 = %11000000 ' int. unimpl, all trigs. disabled
ADCHS = %01000000 ' group sel., Group D=AN3, Group B=AN1, Group A=AN0

' wake acc.
aACC_SLEEP = 1

' 1.5 g
aACC_g1 = 0'01
aACC_g2 = 0'01

ADCON0.0 = 1 'a/d mod ON

m = 0

wMinvalArray[m+1] = 1024
wMaxvalArray[m+1] = 0

wMinvalArray[m+2] = 1024
wMaxvalArray[m+2] = 0

wMinvalArray[m+3] = 1024
wMaxvalArray[m+3] = 0

'////////////////////////////////
'
' mainloop - the mother of all
'
'////////////////////////////////
mainloop:

for m = 1 to 3

if m = 1 then ADCON0 = %00000001
if m = 2 then ADCON0 = %00000101
if m = 3 then ADCON0 = %00001001

ADCON0.1 = 1 ' Start Conversion
Pause 5

notdone:
If ADCON0.1 = 1 Then goto notdone

Pause 5

' good conversion, grab the data
wAdval.LowByte = ADRESL
wAdval.HighByte = ADRESH

if wAdval < wMinvalArray[m] then wMinvalArray[m] = wAdval
if wAdval > wMaxvalArray[m] then wMaxvalArray[m] = wAdval

next m

lcdout $fe, $1, "x", Dec4 wMinvalArray[1], "-", DEC4 wMaxvalArray[1], _
"y", Dec4 wMinvalArray[2]
lcdout $fe, $c0, "-", DEC4 wMaxvalArray[2], _
" z", Dec4 wMinvalArray[3], "-", DEC4 wMaxvalArray[3]

Goto mainloop ' Do it forever

skimask
- 10th April 2007, 18:30
Do the returned values on the LCD change as you would expect them to as you move the accelerometer?
With respect to each of the values returned, if you applied a constant offset, would that fix your numbers and make them read what you would expect them to read?

ttease
- 10th April 2007, 22:24
skimask,

not sure yet, I want to take the voltage measurments and see how they look with respect to orientation.

Basic question, if I give the micro a ref voltage, should it not scale the number from 0-1024?

Another item I need to investigate are the Tad and conversion clock bits, I guessed at what values to put here.

thanks
t

skimask
- 10th April 2007, 23:03
skimask,

not sure yet, I want to take the voltage measurments and see how they look with respect to orientation.

Basic question, if I give the micro a ref voltage, should it not scale the number from 0-1024?

Another item I need to investigate are the Tad and conversion clock bits, I guessed at what values to put here.

thanks
t

If Vref+ is what the accelerometer puts out at max, and Vref- is what the accelerometer puts out at minimum, then, yes, it'll scale from 0 to 1023.
If not, either you'll want to do some sort of scaling to the end result. Problem is, if you're input range is less than your output target range, you'll end up losing precision (I just did a post for another guy talking about that elsewhere).