' ***********************************************************************
' *                       < MACH96 - Hardware V1.0 >                    *
' *                               <TACHO>                               *
' *                  Hybrid computer section front-end                  *
' *             Source code for Basic Stamp microcontrollers            *
' *                used as a tacho-meter with analog out                *
' *                      by Dr.Godfried-Willem Raes                     *
' *                       filename: MACH96.BS1                          *
' ***********************************************************************
' 14.07.1996: Stamp1 application for Laser Tacho to be used in
'             FVV-Brabant project MACH96
'             This code implements an rpm to voltage convertor
'             for the freewheel of the Leuven steamengine.
'             This wheel has 6 spokes and rotates at ca. 100rpm.
'             Thus the input frequency is 100*6=600 pulses per minute
'             or, 10 pulses per second (10Hz).
'             This code is based on the <Diana> code for BOM96.
' 15.07.1996: Debug session, hardware & software. Works.
'             T1000 used as downloader.

' Hardware description:
' *********************
' The BASIC Stamp serves as frequency to pwm converter.
' The circuit is build from discrete components, not a hybrid BS1 stamp.
' The output is a DC-voltage to be fed to an ADC-converter 
' channel (Contec board on <Hera>). 
' It accepts pulses (from a TTL Schmitt-Trigger) input through pin 1, and output 
' pwm-signals its pin 7. Resolution is <= 8 bits.(?) 
' Pin 7 is connected to an RC-circuit, for integration:
'              4.7kOhm - 0.47mF gives 2.2 ms RC = 1 PWM burst      OK 
'              10kOhm - 1 mF would give 10ms RC-time
' This dc-signal is buffered. The DC range is 0-5V.        
' Conversion speed is a function of the input signal!

Start:
	   input 0         ' not used
	   input 1         ' input pin for puls-time measurement 
	   input 2         ' not used
	   input 3         ' not used
	   input 4         ' not used
	   input 5         ' not used
	   input 6         ' not used
	   input 7         ' PWM-output pin!. Defined as IN to reduce leaks.
	   w4 = 0          ' reset w4 on init.

Frq:    
	pulsin 1,1,w1      ' Read positive-going pulse on pin 1.
	pulsin 1,0,w2      ' Read negative-going pulse on pin 1.
				    ' These readings return 16bit values:0-&HFFFF
				    ' One bit-count equals 10microseconds.
				    ' A full period is w1+w2.
				    ' We divide, to avoid overflow. (max.=&HFFFF)
	w1 = w1/2         
	IF w1 < 2 THEN NulOut
	w2 = w2/2     
	IF w2 < 2 THEN NulOut
	w3 = w1+w2         ' Spoke-pulse period in 20microsecond increments
				    ' w3=&HFFFF => 1.3107s => f-spokes= 0.762951094 Hz
				    ' This should become the lowest measurable frequency and rpm.
				    ' It corresponds to 0.1271585 Hz for the 6-spoke flywheel
				    ' To obtain the rpm value we multiply by 60 and this
				    ' gives us 7.6295 rpm.(= 8rpm as integer) 
				    ' The freewheel period is then: 7.8642 seconds!
				    ' If w1=w2= 1 we have the fastest possible speed:
				    ' w3= 2 => 40microsec => f-spokes= 25000 Hz
				    ' f-wheel= 4166Hz, or, 250000 rpm.
				    ' If either w1 or w2 was 0, w3 becomes invalid.
				    ' Clearly we can limit ourselves to considering 
				    ' only the MSByte of w3.
				    ' If MSB of w3 = 1 we have:
				    ' 256 * 20=  5,120 milisec. = 195Hz for spokes or
				    ' 32 Hz for the wheel = 1953 rpm
				    ' Therefore, we limit w3 to w3 > 2049 and thus
				    ' rpm to 245
	IF w3 < 1024 THEN Frq   
				    ' this condition means unreliable reading
				    ' so we take another reading.
	IF w3 < 1910 THEN PrevOut
				    ' If the reading is overrange, we stick to the
				    ' previous reading.
				    ' Of course we could also fix the output
				    ' at 5V
	    
	w3=w3/10           ' rpm= 50000 / (w3 /10)
	w3=50000/w3        ' w3 now holds the exact rpm value.
	w3=w3 - 7 MAX 255  ' remove the offset for &HFFFF=8rpm
	pwm 7,w3,1         ' output this as a number (1) of pwm bursts
				    ' each burst takes about 5 ms.
	w4 = w3            ' save oldvalue
	'debug #w3         ' used for measurement and test purposes. 
	goto Frq
NulOut:
	pwm 7,0,1
	w4 = 0
	goto Frq
PrevOut:
	pwm 7,w4,1
	goto Frq
VijfOut:
	pwm 7,255,1
	w4 = 255
	goto Frq

	bsave              ' use once to generate an object file
