Code:
'****************************************************************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 6/9/2011 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
'References
'Arduino - http://interface.khm.de/index.php/lab/experiments/connect-a-ltc2400-high-precision-24-bit-analog-to-digital-converter/
'Datasheet - http://cds.linear.com/docs/Datasheet/2410fs.pdf
;-------------------------------------------------------------------------------
Device = 18F26K22
Xtal = 10
Declare PLL_Req = On
All_Digital = TRUE
;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings
Declare Reminders Off
@ CONFIG_REQ = 0 ; Override Compiler's configuration settings
Asm-
Config FOSC = HSMP ;HS oscillator (medium power 4-16 MHz)
Config PLLCFG = On ;Oscillator multiplied by 4
Config PRICLKEN = On ;Primary clock enabled
Config FCMEN = OFF ;Fail-Safe Clock Monitor disabled
Config IESO = OFF ;Oscillator Switchover mode disabled
Config PWRTEN = OFF ;Power up timer disabled
Config BOREN = SBORDIS ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
Config BORV = 190 ;VBOR set to 1.90 V nominal
Config WDTEN = OFF ;Watch dog timer is always disabled. SWDTEN has no effect.
Config WDTPS = 256 ;1:256
Config CCP2MX = PORTC1 ;CCP2 input/output is multiplexed with RC1
Config PBADEN = OFF ;PORTB<5:0> pins are configured as digital I/O on Reset
Config CCP3MX = PORTB5 ;P3A/CCP3 input/output is multiplexed with RB5
Config HFOFST = On ;HFINTOSC output and ready status are not delayed by the oscillator stable status
Config T3CMX = PORTC0 ;T3CKI is on RC0
Config P2BMX = PORTB5 ;P2B is on RB5
Config MCLRE = EXTMCLR ;MCLR pin enabled, RE3 input pin disabled
Config STVREN = On ;Stack full/underflow will cause Reset
Config LVP = On ;Single-Supply ICSP enabled if MCLRE is also 1
Config XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
Config Debug = OFF ;Disabled
Config CP0 = OFF ;Block 0 (000800-003FFFh) not code-protected
Config CP1 = OFF ;Block 1 (004000-007FFFh) not code-protected
Config CP2 = OFF ;Block 2 (008000-00BFFFh) not code-protected
Config CP3 = OFF ;Block 3 (00C000-00FFFFh) not code-protected
Config CPB = OFF ;Boot block (000000-0007FFh) not code-protected
Config CPD = OFF ;Data EEPROM not code-protected
Config WRT0 = OFF ;Block 0 (000800-003FFFh) not write-protected
Config WRT1 = OFF ;Block 1 (004000-007FFFh) not write-protected
Config WRT2 = OFF ;Block 2 (008000-00BFFFh) not write-protected
Config WRT3 = OFF ;Block 3 (00C000-00FFFFh) not write-protected
Config WRTC = OFF ;Configuration registers (300000-3000FFh) not write-protected
Config WRTB = OFF ;Boot Block (000000-0007FFh) not write-protected
Config WRTD = OFF ;Data EEPROM not write-protected
Config EBTR0 = OFF ;Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
Config EBTR1 = OFF ;Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
Config EBTR2 = OFF ;Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
Config EBTR3 = OFF ;Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
Config EBTRB = OFF ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
Endasm-
Declare Reminders On
;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
' ** Declare Pins Used **
Symbol CS = PORTB.0 ' Chip Select Pin
Symbol SDO = PORTB.1 ' Shift Data Pin
Symbol SCK = PORTB.2 ' Shift Clock Pin
;-------------------------------------------------------------------------------
' ** Declare Variables **
Symbol VREF = 5.0 ' Vref in Volts
Dim RAW As Dword ' RAW value read from ADC
Dim SIGN As Dword ' Sign Value for Negative Numbers
Dim COUNTS As Dword ' 24-Bit Counts Variable
Dim VOLTS As Float ' Volts value
Dim READY As Bit ' Ready Bit for conversion completion
;-------------------------------------------------------------------------------
' ** MAIN **
;-------------------------------------------------------------------------------
MAIN:
GoSub GET_SAMPLE
If READY = 1 Then
GoSub CONVERT_SAMPLE
EndIf
SerOut PORTB.7,84,[13,10]
DelayMS 250
GoTo MAIN
;-------------------------------------------------------------------------------
' ** SUB ROUTINES **
;-------------------------------------------------------------------------------
GET_SAMPLE: 'INPUT - N/A
'RETURN - RAW as DWORD
Low CS 'Request Device Conversion
If SDO = 1 Then ' Test if Device is ready
READY = 1
SHIn SDO,SCK,MsbPre_L,[RAW.Byte3,RAW.Byte2,RAW.Byte1,RAW.Byte0]
Else
READY = 0
DelayMS 100 'Wait for Conversion to complete
SerOut PORTB.7,84,["Conversion not Complete",13,10]
EndIf
High CS 'Request Device to Sample
Return
;-------------------------------------------------------------------------------
CONVERT_SAMPLE: 'INPUT - RAW as DWORD
'RETURN - VOLTS as Float
'RETURN - COUNTS as Float
COUNTS = RAW
SerOut PORTB.7,84,["0-",Bin32 COUNTS,32,SDec COUNTS,13,10]
'Test for Overanging & Pos/Neg Values
If RAW.29 = 1 Then
If RAW.28 = 1 Then
SerOut PORTB.7,84,["Over Range VIN >= 0.5 * VREF",13,10]
Else
COUNTS = COUNTS
SIGN = 1
EndIf
Else If RAW.29 = 0 Then
If RAW.28 = 0 Then
SerOut PORTB.7,84,["Under Range VIN <= 0.5 * VREF",13,10]
Else
COUNTS = ~COUNTS 'Invert Sample
SIGN = -1 'Apply Negative sign in final calculation
EndIf
EndIf
SerOut PORTB.7,84,["1-",Bin32 COUNTS,32,SDec COUNTS,13,10] '
COUNTS.31 = 0 'EOC Bit
COUNTS.30 = 0 'Dummy Bit
COUNTS.29 = 0 'Sign Bit
SerOut PORTB.7,84,["2-",Bin32 COUNTS,32,SDec COUNTS,13,10]
COUNTS.4 = 0 'Sub LSBs
COUNTS.3 = 0 'Sub LSBs
COUNTS.2 = 0 'Sub LSBs
COUNTS.1 = 0 'Sub LSBs
COUNTS.0 = 0 'Sub LSBs
SerOut PORTB.7,84,["3-",Bin32 COUNTS,32,SDec COUNTS,13,10]
COUNTS = COUNTS >> 5
SerOut PORTB.7,84,["4-",Bin32 COUNTS,32,SDec COUNTS,13,10]
COUNTS = SIGN * COUNTS 'Apply Pos/Neg Sign Convention
SerOut PORTB.7,84,["5-",Bin32 COUNTS,32,SDec COUNTS,13,10]
VOLTS = COUNTS * VREF / 16777216.0 '24-BIT RESOLUTION
SerOut PORTB.7,84,["6 - ",Dec8 VOLTS," (V)",13,10]
Return