The BX24 provides eight 10-bit A/D converters. For those applications where more resolution is required, the LTC1298 Dual 12-bit A/D is an attractive alternative.
Program LTC1298_1.Bas illustrates how to perform a measurement on either Channel 0 or 1. Program LTC1298_2.Bas extends this to performing a measurment a number of times and computing an average.
Program LTC1298_3.Bas illustrates an arrangement which measures atmospheric pressure using a Motorola MPX4115 on Channel 0 and temperature using a nominal 10K NTC thermistor in a voltage divider arrangement on Channel 1.
' LTC1298_1.Bas (BX24)
'
' Illustrates how to interface with a Linear LTC1298 12-bit Dual A/D.
'
' BX24 LTC1298
' ------ Dout (term 6)
' |
' 1K |
' DAT Pin15 -------------------------- Din (term 5)
' CLK Pin14 -------------------------- Clk (term 7)
' NOT_CS Pin 13 ---------------------- /CS (term 1)
'
' Routine performs an A/D conversion on Ch 0 (term 2 of LTC1298) and
' displays. Repeats for Ch 1 (term 3). Continually loops.
'
' copyright, Peter H. Anderson, Baltimore, MD, Nov, '99
Const NOT_CS as Byte = 13
Const CLK as Byte = 14
Const DAT as Byte = 15
Sub Main()
Dim Band as Integer
Call OpenSerialPort(1, 19200)
Do
Band = LTC1298GetADC(0) ' perform a meas on Ch 0
Call PutB(0) ' and display Ch and result
Call PutByte(Asc(" "))
Call PutI(Band)
Call NewLine()
Band = LTC1298GetADC(1) ' same for Ch 1
Call PutB(1)
Call PutByte(Asc(" "))
Call PutI(Band)
Call NewLine()
Call Sleep(0.5)
Loop
End Sub
Function LTC1298GetADC(ByVal Ch as Byte) as Integer
Dim Band as Integer
Dim ADCommand as Byte
Dim N as Byte
Band = 0
If (Ch = 0) then
ADCommand = &H0d
Else
ADCommand = &H0f
End If
Call PutPin(NOT_CS, 1) ' initial conditions
Call PutPin(CLK, 1)
Call PutPin(NOT_CS, 0) ' 1->0 transition resets LTC1298
For N = 1 to 4 ' send 4-bit command, ms bit first
If ((ADCommand AND bx00001000) <> 0) Then ' Its a one
Call PutPin(DAT, 1)
Else
Call PutPin(DAT, 0) ' otherwise a zero
End If
Call PutPin(CLK, 0) ' clock pulse
Call PutPin(CLK, 1)
ADCommand = ADCommand * 2 ' orient next bit into position
Next
Call PutPin(CLK, 0) ' dummy clock pulse
Call PutPin(CLK, 1)
For N = 1 to 12 ' fetch the 12 bit result, ms bit first
Call PutPin(Dat, 2) ' make DAT an input
Call PutPin(CLK, 0) ' clock
Call PutPin(CLK, 1)
If (GetPin(DAT) <> 0) Then ' read the data bit
Band = (Band * 2) + 1
Else
Band = Band * 2
End If
Next
Call PutPin(NOT_CS, 1) ' turn off the device
LTC1298GetADC = Band
End Function
' LTC1298_2.Bas (BX24)
'
' Extends on LTC1298_1.Bas. Adds functions to perform N measurments
' on a specified channel and return the average as a float.
'
' Performs 100 measurments on Ch 0 and displays result. Same on Ch 1.
'
' copyright, Peter H. Anderson, Baltimore, MD, Nov, '99
Const NOT_CS as Byte = 13
Const CLK as Byte = 14
Const DAT as Byte = 15
Sub Main()
Dim BandAvg as Single
Call OpenSerialPort(1, 19200)
Do
BandAvg = LTC1298GetADCAvg(0, 100) ' perform 100 meas on Ch 0
Call PutB(0) ' and display Ch and average
Call PutByte(Asc(" "))
Call PutS(BandAvg)
Call NewLine()
BandAvg = LTC1298GetADCAvg(1, 100) ' same for Ch 1
Call PutB(1)
Call PutByte(Asc(" "))
Call PutS(BandAvg)
Call NewLine()
Call Sleep(0.5)
Loop
End Sub
Function LTC1298GetADCAvg(ByVal Ch as Byte, _
ByVal NumMeas as Integer) as Single
Dim N as Integer, BandInt as Integer
Dim Sum as Single, Avg as Single
Sum = 0.0
For N = 1 to NumMeas
BandInt = LTC1298GetADC(Ch)
Sum = Sum + CSng(BandInt)
Next
Avg = Sum / CSng(NumMeas)
LTC1298GetADCAvg = Avg
End Function
Function LTC1298GetADC(ByVal Ch as Byte) as Integer
Dim Band as Integer
Dim ADCommand as Byte
Dim N as Byte
Band = 0
If (Ch = 0) then
ADCommand = &H0d
Else
ADCommand = &H0f
End If
Call PutPin(NOT_CS, 1) ' initial conditions
Call PutPin(CLK, 1)
Call PutPin(NOT_CS, 0) ' 1->0 transition resets LTC1298
For N = 1 to 4 ' send 4-bit command, ms bit first
If ((ADCommand AND bx00001000) <> 0) Then ' Its a one
Call PutPin(DAT, 1)
Else
Call PutPin(DAT, 0) ' otherwise a zero
End If
Call PutPin(CLK, 0) ' clock pulse
Call PutPin(CLK, 1)
ADCommand = ADCommand * 2 ' orient next bit into position
Next
Call PutPin(CLK, 0) ' dummy clock pulse
Call PutPin(CLK, 1)
For N = 1 to 12 ' fetch the 12 bit result, ms bit first
Call PutPin(Dat, 2) ' make DAT an input
Call PutPin(CLK, 0) ' clock
Call PutPin(CLK, 1)
If (GetPin(DAT) <> 0) Then ' read the data bit
Band = (Band * 2) + 1
Else
Band = Band * 2
End If
Next
Call PutPin(NOT_CS, 1) ' turn off the device
LTC1298GetADC = Band
End Function
' LTC1298_3.Bas (BX24)
'
' Extends on LTC1298_2.Bas. Measures and displays atmospheric pressure
' using a Motorola MPX4115 sensor on Ch 0 and temperature is degrees F
' using a series 10K and 10K NTC Thermistor arrangement on Ch 1.
'
' ****************************
' For atmospheric pressure the voltage is calculated as
'
' V = BandAvg/4096 * Vref
'
' The MPX4115 pressure sensor outputs a voltage;
'
' V = Vref * (Pmillibars * 0.0009 - 0.095)
'
' Equating these two expressions and solving for P_millibars;
'
' Pmillibars = 0.27 * BandAvg + 105.0
'
' In the US, we use inches of mercury
'
' PInchesHg = 0.00797 * BandAvg + 2.95
' *******************************
'
' For temperature, a series voltage divider consisting of a fixed
' resistor R1 and an NTC thermistor (Rtherm) is used.
'
' V = BandAvg/4096 * Vref
'
' Using voltage division;
'
' V = Vref * Rtherm / (R1 + Rtherm)
'
' Equating these two expressions and solving for Rtherm;
'
' Rtherm = R1 / (4096/band - 1)
'
' Using a two point model for the thermistor;
'
' TKelvin = 1.0 / (a + b * ln (Rtherm))
'
' TCelcius = TKelvin - 273.15
' TF = TCelcius * 1.8 + 32.0
' ******************************
'
' copyright, Peter H. Anderson, Baltimore, MD, Nov, '99
Const NOT_CS as Byte = 13
Const CLK as Byte = 14
Const DAT as Byte = 15
Const a as Single = 0.000413200 ' thermistor constants
Const b as Single = 0.000320135
Sub Main()
Dim BandAvg as Single, PMillibars as Single, TF as Single
Dim Str as String
Call OpenSerialPort(1, 19200)
Do
BandAvg = LTC1298GetADCAvg(0, 100) ' perform 100 meas on Ch 0
PMillibars = CalcPressure(BandAvg) ' calc pressure in millibars
Str = "Pressure in Millibars = "
Call PutStr(Str)
Call PutS(PMillibars)
Call NewLine()
BandAvg = LTC1298GetADCAvg(1, 100)
TF = CalcTF(BandAvg) ' calc temperature
Str = "TF = "
Call PutStr(Str)
Call PutS(TF)
Call NewLine()
Call Sleep(0.5)
Loop
End Sub
Function CalcPressure(ByVal BandAvg as Single) as Single
CalcPressure = 0.27 * BandAvg + 105.0
End Function
Function CalcTF(ByVal BandAvg as Single) as Single
Dim RTherm as Single, TKelvin as Single, TCelcius as Single
Dim TF as Single
RTherm = 10.0e3 / ((4096.0/BandAvg) - 1.0)
TKelvin = 1.0 / (a + b * log(RTherm))
TCelcius = TKelvin - 273.15
TF = TCelcius * 1.8 + 32.0
CalcTF = TF
End Function
Function LTC1298GetADCAvg(ByVal Ch as Byte, _
ByVal NumMeas as Integer) as Single
Dim N as Integer, BandInt as Integer
Dim Sum as Single, Avg as Single
Sum = 0.0
For N = 1 to NumMeas
BandInt = LTC1298GetADC(Ch)
Sum = Sum + CSng(BandInt)
Next
Avg = Sum / CSng(NumMeas)
LTC1298GetADCAvg = Avg
End Function
Function LTC1298GetADC(ByVal Ch as Byte) as Integer
Dim Band as Integer
Dim ADCommand as Byte
Dim N as Byte
Band = 0
If (Ch = 0) then
ADCommand = &H0d
Else
ADCommand = &H0f
End If
Call PutPin(NOT_CS, 1) ' initial conditions
Call PutPin(CLK, 1)
Call PutPin(NOT_CS, 0) ' 1->0 transition resets LTC1298
For N = 1 to 4 ' send 4-bit command, ms bit first
If ((ADCommand AND bx00001000) <> 0) Then ' Its a one
Call PutPin(DAT, 1)
Else
Call PutPin(DAT, 0) ' otherwise a zero
End If
Call PutPin(CLK, 0) ' clock pulse
Call PutPin(CLK, 1)
ADCommand = ADCommand * 2 ' orient next bit into position
Next
Call PutPin(CLK, 0) ' dummy clock pulse
Call PutPin(CLK, 1)
For N = 1 to 12 ' fetch the 12 bit result, ms bit first
Call PutPin(Dat, 2) ' make DAT an input
Call PutPin(CLK, 0) ' clock
Call PutPin(CLK, 1)
If (GetPin(DAT) <> 0) Then ' read the data bit
Band = (Band * 2) + 1
Else
Band = Band * 2
End If
Next
Call PutPin(NOT_CS, 1) ' turn off the device
LTC1298GetADC = Band
End Function