Code:
'****************************************************************
'* Name : 7731_SigmA2D.BAS *
'* Author : Giandomenico Di Massa *
'* Notice : Copyright (c) 2006 *
'* : All Rights Reserved *
'* Date : 01/11/2006 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
define OSC 10
define SHIFT_PAUSEUS 20
'LCD setup
Define LCD_DREG PORTB 'Set the LCD port
Define LCD_DBIT 4 'set the fist bit for the LCD
Define LCD_RSREG PORTB 'Set the bit Register Select port
Define LCD_RSBIT 1 'set Register Select pin
Define LCD_EREG PORTB 'Set bit Enable port
Define LCD_EBIT 2 'Set the Enable pin
Define LCD_BITS 4
Define LCD_LINES 4
'Serial Peripheral Interface setup
symbol SCK=PORTC.6 'Clock pin
symbol SI=PORTC.5 'AD7731 Data in pin
symbol SO=PORTC.4 'AD7731 Data out pin
symbol CS=PORTC.7 'AD7731 Chip select pin
symbol RDY=PORTC.2 'AD7731 Ready pin
'Variables declaration
C1 Var byte
Send Var byte 'Byte sent to the 8 bits Communication register of the AD7731
Command var byte 'Byte sent as command to a register setted by Send in Communcation register
Command2 var word 'Word sent to 16 bit register as Mode or Filter register of the AD7731
Rec VAR byte 'Byte received from AD7731:only for communication register 8 bit
AD var word 'AD converter value 16 bits data word
mVolt var word
dmVolt var word
pause 500
LCDOUT $FE,1
input rdy
LCDOUT "Initiating..."
gosub waitbutton
lcdout $FE, $C0, "OK"
pause 1000
Test_communication_interface: 'Active and deactive the D0 digital line to test the AD7731 - Communication
Send=$2 'Send command $2 hex to the communication register:next operation is a write on Mode register
Command2=$0B74 'Send command $0B74 hex to the Mode Register: Active Digital line, put hi the D0 output
gosub lcdupdate
gosub ad_write2 'Send command
gosub waitbutton
Send=$2 'Send command $2 hex to the communication register:next operation is a write on Mode register
Command2=$0974 'Send command $0974 hex to the Mode Register: Active Digital line, put low the D0 output
gosub lcdupdate
gosub ad_write2 'Send command
gosub waitbutton
Setup_Configuration:
Send=$3 'Send command $3 hex to the communication register:next operation is a write on Filter register
Command2=$8002 'Send command $8002 hex to the Filter register: 122Hz output rate in nonCHOP mode
gosub lcdupdate
gosub ad_write2 'Send command
gosub waitbutton
'AD7731 Self Calibration
Send=$2 'Send command $2 hex to the communication register:next operation is a write on Mode register
Command2=$91F4 'Send command $0974 hex to the Mode Register: Internal zero scale calibration
gosub lcdupdate
gosub ad_write2 'Send command
gosub waitrdy
Send=$2 'Send command $2 hex to the communication register:next operation is a write on Mode register
Command2=$B1F4 'Send command $B1F4 hex to the Mode Register: Internal full scale calibration
gosub lcdupdate
gosub ad_write2 'Send command
gosub waitrdy
Send=$2 'Send command $2 hex to the communication register:next operation is a write on Mode register
Command2=$D1F4 'Send command $D1F4 hex to the Mode Register: System zero scale calibration
gosub lcdupdate
gosub ad_write2 'Send command
gosub waitrdy
Send=$2 'Send command $2 hex to the communication register:next operation is a write on Mode register
Command2=$F1F4 'Send command $F1F4 hex to the Mode Register: System full scale calibration
gosub lcdupdate
gosub ad_write2 'Send command
gosub waitrdy
lcdout $FE,$1
Loop:
Send=$2 'Send command $2 hex to the communication register:next operation is a write on Mode register
command2=$50F4 'Send command $50F4 hex to the Mode Register: Single conversion,Range 0-1.28V, Data length 16 bit, AIN1-AIN2 Full Differential
gosub ad_write2 'Send command
gosub waitrdy
gosub ad_read 'Read the Data Registaer
lcdout $FE, $80,"Rec:",hex ad," "
lcdout $FE,$80+10," AD:",dec ad," "
lcdout $FE,$C0,"Ad:",bin16 ad
mvolt = ad*195 'Scale the 16 bit value to milliVolt
mvolt=div32 10000
dmvolt=R2/100
lcdout $FE,$94,"Volt:",dec mvolt,",",dec2 dmvolt, " mV "
pause 300
goto loop
END
AD_Write2:
'AD7731 write routine into a 16 bit register as Mode register
'first byte is sent to the Communication register for the register selection
'second word od 16 bit is value to be sent
'this routine is valid only for writin in 16 bits registers
low cs 'Enable Device
shiftout so,sck,1,[send\8] 'Send the command to the communication register: write enable command
shiftout so,sck,1,[command2\16] 'Send the command to the 16 bit register
high cs 'disable the device
return
AD_Read:
low cs
shiftout so,sck,1,[$11\8]
shiftin si,sck,0,[AD\16]
high cs
return
WaitButton:
button PORTC.3,255,0,0,C1,1,GoOut
goto waitbutton
GoOut:
return
LCDupdate:
LCDOUT $FE,1
lcdout "Comm. register: ", hex send,$FE, $C0,BIN8 send
lcdout $FE, $94,"Command: ",hex command2 , $FE, $D4,bin16 command2
return
WaitRDY:
while rdy=1
wend
return
Bookmarks