'*************************************************************************
'
'	Title:	ECH-1 Chronometer Test Program
'
'	File:	EHO-ECH1.BS2
'
'	Written by:	Dan Coner and Matthew Slaby
'	Written for:	E-O DEVICES, LLC.
'
'	Date:	December 31,2002
'
'	CPU:	Parallax Basic Stamp II
'
'	Description:
'
'	This program TESTS ECH-1 with the EHO-1A
'	Features:  
'
'*************************************************************************


'***********************************************************************
'***********************************************************************
'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:
'

N	var	byte
whole	var	word
fract	var	byte
minw	var	word
maxw	var	word
sumw	var	word
minf	var	word
maxf	var	word
sumf	var	word
value	var	word
result	var	word
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
'	gosub	dsp_init
'	msg = t_EOD
'	gosub	send_text
	high	MPWR
	debug	"Main Power ON",cr,lf

	serout  16,Baud,20,["waiting...",cr,lf]
	pause	1000

	serout  16,Baud,20,["Prepare ECH-1 for test then press 'g'.",cr,lf]
	serin	16,Baud,[WAIT("g")]
	goto start

reset:

	serout  16,Baud,20,[cr,lf,"reseting...",cr,lf]
	command=$00		'command to reset
	low	CS		'assert CS
	shiftout  HDO,HCLK,msbfirst,[command] 'send command to module
	high	CS		'deassert CS
	pause	1000
	
start:

	serout  16,Baud,20,[cr,lf,"initializing...",cr,lf]
	
	command = $70	'$70 command = sw_exec, sw_N, and N=0

	low	HDO		'hold HDO low to send serial communication command
	low	CS		'assert CS
	pause	1000
	high	CS		'deassert CS
	serout  16,Baud,20,["running...",cr,lf]

rept:
	
	IF command > $7F THEN reset

	low	CS		'assert CS
	shiftout  HDO,HCLK,msbfirst,[command] 'send command to module
	high	CS		'deassert CS

	N = command & $0F
	serout  16,Baud,20,[cr,lf,"command=",hex(command)," and N=",hex(N),"...",cr,lf]
	command = command + 1
	cnt = 0

wait_sdav:
	if IN11 = 0 then wait_sdav

'	serout  16,Baud,20,["check sum then press 'g'.",cr,lf]
'	serin	16,Baud,[WAIT("g")]

	pause	10
	low	CS		'assert CS
	shiftin	HDI,HCLK,msbpre,[result.highbyte] 'get whole num.
	high	CS		'deassert CS
	pause	10
	low	CS		'assert CS
	shiftin	HDI,HCLK,msbpre,[result.lowbyte] 'get whole num.
	high	CS		'deassert CS

'for debug only:
'	low	CS		'assert CS
'	shiftin	HDI,HCLK,msbpre,[status] 'get coarse count.
'	high	CS		'deassert CS

'	debug	hex4(result)," ",hex2(resultf)," ",hex2(status),cr,lf
'	debug	hex4(result)," ",hex2(resultf),cr,lf
'** this one is for quick test:
'	debug   dec result,".",dec3 (resultf*250/64)," ns",cr,lf
'**
'	serout  16,Baud,10,[34,dec(result.highbyte),34,cr] 'PLOT ONLY
'	serout  16,Baud,10,[34,dec(result.lowbyte),34,cr] 'PLOT ONLY
	serout  16,Baud,10,[34,hex(result.highbyte),",",hex(result.lowbyte),34,cr] 'display received data
	whole = result >> 6
	fract = result.lowbyte & $3F
	serout	16,Baud,10,[34,dec(whole)," & ",dec(fract),"/64",34,cr]
'	serout  16,Baud,10,[34,dec(result.highbyte),",",dec(result.lowbyte),34,cr] 'FILE ONLY


	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

