Tilt_sensor.txt
Warning: if you clip this code into your program, look out for possible html code slipping in.
REM tilt sensor interface REM ss 11/98 REM ***************************************************************** REM This program was written to be compiled with revision 3 of VSTB. REM Because of this, it defines AIN() as a subroutine, since it only REM became a built-in function in revision 4. If you want to use this REM program, you will have to make the appropriate modifications. REM ***************************************************************** REM Fluid filled tilt sensors are problematic from a circuit design standpoint REM because they cannot withstand an applied DC voltage. AC must be applied REM to the bridge to prevent electrolytic plating of the liquid. REM This program interfaces directly to a tilt sensor using no external components. REM The sensor consists of 4 pins surrounding a single center pin in conductive REM liquid. The 4 outer pins are connected to the keypad pins 4-8 and the REM center pin connected to AIN pin 1. CONSTANT VTI_ADRES AS INTEGER = 0x1E CONSTANT VTI_ADCON0 AS INTEGER = 0x1F CONSTANT VTI_TRISA AS INTEGER = 0x85 CONSTANT VTI_ADCON1 AS INTEGER = 0x9F CONSTANT VTI_Analog_5 AS INTEGER = 2 REM CONSTANT VTI_Analog_3 AS INTEGER = 4 REM CONSTANT VTI_Analog_0 AS INTEGER = 6 CONSTANT VTI_Fosc_by_32 AS INTEGER = 0x81 GLOBAL VTI_AD_GO AS BIT = VTI_ADCON0,2 REM simple A/D, SBC2000-074/-062 VITAL FUNCTION AIN(channel AS INTEGER) AS INTEGER POKE(VTI_ADCON1,VTI_Analog_5) :REM set up port a as 5 analog inputs POKE(VTI_TRISA, PEEK(VTI_TRISA) OR (2^channel)) POKE(VTI_ADCON0, VTI_Fosc_by_32 OR (channel * 8)) VTI_AD_GO = 1 : REM Start the conversion AIN = PEEK(VTI_ADRES) : REM Read the result END CONSTANT CRLF AS STRING = "\013\010" REM these two constants correct for any mechanical off center of the sensor CONSTANT pitchoffset AS INTEGER = 0 CONSTANT rolloffset AS INTEGER = 0 GLOBAL B4 AS BIT = 6,4 GLOBAL B5 AS BIT = 6,5 GLOBAL B6 AS BIT = 6,6 GLOBAL B7 AS BIT = 6,7 GLOBAL temp AS INTEGER GLOBAL PITCH1 AS INTEGER, PITCH2 AS INTEGER GLOBAL roll1 AS INTEGER, roll2 AS INTEGER GLOBAL PITCH AS INTEGER, ROLL AS INTEGER REM Here is the key to the "no DC" requirement. REM Two bridge elements are excited + to -, then - to +, REM then the other two bridge elements are similarly excited, REM then all electrodes are turned off. REM Four A/D reading are taken during the four excitation phases. REM Routine takes ~8mS on an SBC2000-074. VITAL SUBROUTINE tilt() B4 = 0 :REM energize the two pitch electrodes B5 = 1 pitch1 = AIN(0) :REM measure the result in the center B4 = 1 :REM energize the two pitch electrodes B5 = 0 :REM in the opposite phase as before pitch2 = AIN(0) :REM measure the result temp = B4 :REM - VERY IMPORTANT - temp = B5 :REM turn both pitch electodes off! B6 = 0 :REM repeat for roll axis B7 = 1 roll1 = AIN(0) B6 = 1 B7 = 0 roll2 = AIN(0) temp = B6 :REM turn both roll electodes off temp = B7 END REM ********** main POKE(0x81,128 + PEEK(0x81)) :REM turn off PORT B pullups DO tilt() PIPE PRINT COMM0 PRINT CRLF, pitch1,pitch2,roll1,roll2 PITCH = pitch1-pitch2+pitchoffset roll = roll1-roll2+rolloffset LCD_COMMAND(128) PIPE PRINT LCD PRINT PITCH, ROLL," " LOOP UNTIL 0
© 2002 Vesta Technology