Seeking Programming Help
I'm using a LTC1298, 12bit A/D converter. Now my analogue signal going in is 1.60 v and i need to read the same value coming out. now i figured how to convert from binary to obtain my voltage reading, v = (5v * AD/4096) but the reading i'm getting is wrong (1.12v). i tried different options but no luck, when i print the r and v2 values on my screen i get the correct reading for r (2469) but the incorrect reading for v2 (12 when it's suppose to 60). So i'm trying to figure out why v2 is not reading 60.
this is the program that i'm using:
' {$STAMP BS2}
' Program: LTC1298.BS2 (LTC1298 analog-to-digital converter)
CS CON 0 ' Chip select; 0 = active
CLK CON 1 ' Clock to ADC; out on rising, in on falling edge.
DIO_n CON 2 ' Data I/O pin _number_.
config VAR Nib ' Configuration bits for ADC.
AD VAR Word ' Variable to hold 12-bit AD result.
v VAR Word
r VAR Word
v2 VAR Word
startB VAR config.BIT0 ' Start bit for comm with ADC.
sglDif VAR config.BIT1 ' Single-ended or differential mode.
oddSign VAR config.BIT2 ' Channel selection.
msbf VAR config.BIT3 ' Output 0s after data xfer complete.
' ==========================================================
' Demo Program
' ==========================================================
' This program demonstrates the LTC1298 by alternately sampling the two
' input channels and presenting the results on the PC screen using Debug.
HIGH CS ' Deactivate ADC to begin.
HIGH DIO_n ' Set data pin for first start bit.
again: ' Main loop.
FOR oddSign = 0 TO 1 ' Toggle between input channels.
GOSUB convert ' Get data from ADC.
GOSUB calcvolt
DEBUG "Channel ",DEC oddSign, ": ",DEC AD,CR ' Display data.
PAUSE 500
DEBUG "Voltage Reading: ", DEC v, ".", DEC v2, " Volts", CR
PAUSE 500
NEXT
GOTO again
' ==========================================================
' ADC Subroutine
' ==========================================================
convert:
config = config | %1011 ' Set all bits except oddSign.
LOW CS ' Activate the ADC.
SHIFTOUT DIO_n,CLK,LSBFIRST,[noparse][[/noparse]config\4] ' Send config bits.
SHIFTIN DIO_n,CLK,MSBPOST,[noparse][[/noparse]AD\12] ' Get data bits.
HIGH CS ' Deactivate the ADC.
RETURN ' Return to program.
calcvolt:
v = 5 * AD / 4096
r = 5 * AD // 4096
v2 = 100 * r / 4096
RETURN ' Return to program.
I'd really appreciate some expert from those who are expert in the field. Thank you
this is the program that i'm using:
' {$STAMP BS2}
' Program: LTC1298.BS2 (LTC1298 analog-to-digital converter)
CS CON 0 ' Chip select; 0 = active
CLK CON 1 ' Clock to ADC; out on rising, in on falling edge.
DIO_n CON 2 ' Data I/O pin _number_.
config VAR Nib ' Configuration bits for ADC.
AD VAR Word ' Variable to hold 12-bit AD result.
v VAR Word
r VAR Word
v2 VAR Word
startB VAR config.BIT0 ' Start bit for comm with ADC.
sglDif VAR config.BIT1 ' Single-ended or differential mode.
oddSign VAR config.BIT2 ' Channel selection.
msbf VAR config.BIT3 ' Output 0s after data xfer complete.
' ==========================================================
' Demo Program
' ==========================================================
' This program demonstrates the LTC1298 by alternately sampling the two
' input channels and presenting the results on the PC screen using Debug.
HIGH CS ' Deactivate ADC to begin.
HIGH DIO_n ' Set data pin for first start bit.
again: ' Main loop.
FOR oddSign = 0 TO 1 ' Toggle between input channels.
GOSUB convert ' Get data from ADC.
GOSUB calcvolt
DEBUG "Channel ",DEC oddSign, ": ",DEC AD,CR ' Display data.
PAUSE 500
DEBUG "Voltage Reading: ", DEC v, ".", DEC v2, " Volts", CR
PAUSE 500
NEXT
GOTO again
' ==========================================================
' ADC Subroutine
' ==========================================================
convert:
config = config | %1011 ' Set all bits except oddSign.
LOW CS ' Activate the ADC.
SHIFTOUT DIO_n,CLK,LSBFIRST,[noparse][[/noparse]config\4] ' Send config bits.
SHIFTIN DIO_n,CLK,MSBPOST,[noparse][[/noparse]AD\12] ' Get data bits.
HIGH CS ' Deactivate the ADC.
RETURN ' Return to program.
calcvolt:
v = 5 * AD / 4096
r = 5 * AD // 4096
v2 = 100 * r / 4096
RETURN ' Return to program.
I'd really appreciate some expert from those who are expert in the field. Thank you
Comments
v2 = 100 * r / 4096
If r = 2469, then 100 * r = 246900 which is too large to fit into a 16-bit word.
Change that line to read
v2= r */ $0006
Now when you print v2 you will get 57. You're closer, but have lost some accuracy.
A better solution would be
calcvolt: ' This calculates v in millivolts, and you retain accuracy v = AD */ $0138 v2 = v//1000 'fractional part of v RETURN ' Return to program
In the main program, replace
DEBUG "Voltage Reading: ", DEC v, ".", DEC v2, " Volts", CR
with
DEBUG "Voltage Reading: ", DEC1 v/1000, ".", DEC3 v2, " Volts", CR
The */ operator comes in handy for this situation. Plus, you can delete variable r.
phil