'*************************************************************************
'
'	Title:	ECH-2 Chronometer Test Program
'
'	File:	ECH2-TST.BS2
'
'	Written by:	F. Black / DC
'	Written for:	E-O DEVICES, LLC.
'
'	Date:	August 4, 1999
'
'	CPU:	Parallax Basic Stamp II
'
'	Description:
'
'	This program TESTS ECH-2A with the EHO-1A (no LCD output)
'	Features:
'		1) Measures positive pulse width on channel A
'		2) Provide ASCII decimal output on serial port in format:
'			"hb,lb,fb" CR
'			where: hb = high byte of result in decimal
'			       lb = low byte of result in decimal
'			       fb = fraction byte of in decimal
'			Note: Quotation marks are included. CR at end
'				for easy parse by Excel.
'		   
'
'*************************************************************************


'***********************************************************************
'***********************************************************************
'PORT DEFINITIONS:
'
K0	con     0       'port  0: Key pad button 0 (i)
K1	con     1       'port  1: Key pad button 1 (i)
K2	con     2       'port  2: Key pad button 2 (i)
K3	con     3       'port  3: Key pad button 3 (i)
K_TRG	con     4       'port  4: Button - trigger (i)
DDAT	con     5       'port  5: LCD display serial data bus (o)
DCLK	con     6       'port  6: LCD display serial data clock (o)
DE	con     7       'port  7: LCD data enable (o)
HDI	con     8       'port  8: Host serial data input (i)
HDO	con     9       'port  9: Host serial data output - combined w/DRS (o)
DRS	con     9       'port  9: LCD register select - combined w/HDO (o)
HCLK	con     10      'port 10: Host serial data clock (o)
DAV	con     11      'port 11: Data available handshake (i)
AUX	con     12      'port 12: Auxiliary addressing select (o)
CS	con     13      'port 13: Chip select addressing to device, lo active (o)
DPWR	con     14      'port 14: LCD display power enable, lo active (o)
MPWR	con     15      'port 15: Main power enable, hi active (o)
'***********************************************************************


'MISCELLANEOUS DEFINITIONS:
'
lf	con	$0A	'line feed
baud	con	84	'speed for baud rate A (9600)
samples	con	10	'sample count for max/min/sum

'VARIABLE DEFINITIONS:
'

minw	var	word
maxw	var	word
sumw	var	word
minf	var	word
maxf	var	word
sumf	var	word
value	var	word
result	var	word
resultf	var	byte
command var	byte
status	var	byte
valid	var	status.highbit	'bit 7 of status indicates valid range
cnt	var	byte


'MAIN PROGRAM BEGINS:
'

main:
	gosub	init
	debug	"Wake Up",cr,lf
	low	DPWR
	debug	"Display Power ON",cr,lf
	pause	100
	high	MPWR
	debug	"Main Power ON",cr,lf
	serout  16,Baud,20,["waiting...",cr,lf]

	pause	1000

	serout  16,Baud,20,["initializing...",cr,lf]
	command = $01		'reset the ECH-2
	low	CS		'assert CS
	shiftout  HDO,HCLK,msbfirst,[command] 'send command to module
	high	CS		'deassert CS

	pause 2000

tjmp:
	command = $20		'calibrate the ECH-2
	low	CS		'assert CS
	shiftout  HDO,HCLK,msbfirst,[command] 'send command to module
	high	CS		'deassert CS

	pause	1000

	serout  16,Baud,20,["running...",cr,lf]
	command = $55' use $55 for +width on CH A
	low	CS		'assert CS
	shiftout  HDO,HCLK,msbfirst,[command] 'send command to module
	high	CS		'deassert CS
	cnt = 0

rept:
	

wait_sdav:
	if IN11 = 1 then wait_sdav
	low	CS		'assert CS
	shiftin	HDI,HCLK,msbpre,[result.highbyte] 'get whole num.
	high	CS		'deassert CS
	low	CS		'assert CS
	shiftin	HDI,HCLK,msbpre,[result.lowbyte] 'get whole num.
	high	CS		'deassert CS
	low	CS		'assert CS
	shiftin	HDI,HCLK,msbpre,[resultf] 'get fraction
	high	CS		'deassert CS
'	serout  16,Baud,10,[34,hex(result.highbyte),",",hex(result.lowbyte),",",hex(resultf),34,cr,lf] 'HEX OUTPUT
	serout  16,Baud,10,[34,dec(result.highbyte),",",dec(result.lowbyte),",",dec(resultf),34,cr,lf] 'DECIMAL OUTPUT
	goto rept


'SUBROUTINE:	INIT
'
'	Description:  INITIALIZES VARIABLES and system
'
'	Entry:	none
'	Exit:	none
'
init:
	minw= 20000
	maxw = 1
	sumw = 0
	minf = 256
	maxf = 0
	sumf = 0
	return
'init ends



'SUBROUTINE:	DISPLAY
'
'	Description:  Displays answers on computer monitor
'
'
Display:
	value = resultf * 39  'should be * 1000 / 256
	value = value / 10
	if value < 100 then form2
		debug dec result,".",dec value," ns",cr,lf
		return

form2:
	if value < 10 then form3
		debug dec result,".0",dec value," ns",cr,lf
		return

form3:
	if value = 0 then form4
		debug dec result,".00",dec value," ns",cr,lf
		return

form4:
	debug dec result,".000 ns",cr,lf
	return

