***************************************************************************
***************************************************************************
**   EDIS STIMULATOR - GENERATES 36-1 TOOTH VR SIGNAL
**
**   (C) 2002 - B. A. Bowling And A. C. Grippo
**
**   This header must appear on all derivatives of this code.
**
***************************************************************************
***************************************************************************

.header 'EdisStim'
.pagewidth 130
.pagelength 90

.nolist
	include "MC68HC908QT4.equ"
.list
	org	RamStart
;	include "edisstim.h"


adreadlow:   ds     1		  ;Indexed (from ADC) timer rollover value - high byte
adreadhigh:  ds     1             ;Indexed (from ADC) timer rollover value - low byte
toothcnt:    ds     1             ;Tooth counter (from 1 to 35, 36 means missing tooth)


initCONFIG1: equ    %00001111     ;Init. for Low Power Consumption
initStack:   equ    RamLast       ;init. stack pointer to last RAM

SelectCH2AD: equ    %00000010	  ;initial ADSCR select and Read CH2
SelectCH3AD: equ    %00000011     ;initial ADSCR Select and Read CH3
Div2ADICLK   equ    %01000000     ;initial ADICLK = Bus/4
Timerstop    equ    %01110010     ;Stop timer, reset of timer counter, prescale for 1 usec
Timergo      equ    %01000010     ;Enable timer, prescale of 4 for 1 usec increment, interrupts
SetPWM       equ    %00011010     ;PWM, clear on compare, toggle state on overflow
SetPWMNoTog  equ    %00011000     ;PWM, clear on compare, no toggle on overflow

***************************************************************************
**
** Main Routine Here - Initialization and main loop
**
***************************************************************************
	     org    FlashStart
Start:
             clra                 ;Init. Acc. For simulation
             mov    #initCONFIG1,CONFIG1 ;initialize Config1
             ldhx   #initStack+1  ;initialize the stack pointer
             txs
             mov    #$01,osctrim
;
;   Clear all RAM - for the hell of it.
;
             ldhx   #RamStart     ;point to start of RAM
ClearRAM:
             clr    ,x            ;clear RAM location
             aix    #1            ;advance pointer
             cphx   #RamLast+1    ;done ?
             bne    ClearRAM      ;loop back if not


; Set up the timer - for PWM mode
             MOV    #Timerstop,tsc ; Stop Timer so it can be set up
	     mov    #$02,TMODH     ; full period
             mov    #$00,TMODL

             mov    #SetPWM,TSC0

             mov    #$01,TCH0H	      ; compare (duty)
             mov    #$00,TCH0L

             MOV    #Timergo,TSC      ;  set timer prescaler to divide by 4

             mov    #Div2ADICLK,adiclk
             clr    toothcnt

             cli		      ;Enable interrupts (timer rollover)

***************************************************************************
********************    M A I N  E V E N T  L O O P     *******************
***************************************************************************

LOOPER:
             clrh
             mov    #SelectCH2AD,ADSCR ;Select channel2 PTA4
ADREAD:
             brclr  7,ADSCR,ADREAD ;Wait till read is complete

             lda    ADR           ;Load A with Analog Data Register - result of ADC conversion
             tax
             lda    RPMHIGHBYTES,x ; Use the ADC value as an index into RPMHIGHBYTES, get high-order
             sta    adreadhigh

             lda    ADR
             tax
             lda    RPMLOWBYTES,x  ; Do the same, but for lower-order 16 bytes
             sta    adreadlow

             jmp    LOOPER

***************************************************************************
; Interrupt routine - for timer rollover
;
HIGH_WATER:
	     pshh

             lda    tsc
             MOV    #Timerstop,tsc        ; Stop Timer so it can be set up
             mov    adreadhigh,TMODH      ; full period,
             mov    adreadlow,TMODL       ;  both high and low bytes

             inc    toothcnt              ; Add one to toothcount
             lda    toothcnt
             cmp    #36T                  ; This is the missing tooth if equal to 36
             bne    NORMAL_TOOTH

MISSING_TOOTH:
             clr    toothcnt
             mov    #SetPWMNoTog,TSC0     ; Do not toggle pin state on PWM overflow this time around
             lda    adreadhigh
             inca                    ; Add one to timer compare value to make sure it never triggers
             sta    TCH0H
             lda    adreadlow
             inca                    ; Add one more to make sure compare never occurs, hence always high
             sta    TCH0L
             bra    RESTART_TIM

NORMAL_TOOTH:
             mov    #SetPWM,TSC0
             lda    adreadhigh
             clc
             rora
             sta    TCH0H
             lda    adreadlow
             rora
             sta    TCH0L

RESTART_TIM:
             MOV    #Timergo,TSC          ;  set timer prescaler to divide by 4

	     pulh
	     rti


***************************************************************************
**
** Dummy ISR - just performs RTS
**
***************************************************************************
Dummy:			; Dummy vector - there just to keep the assembler happy
	     rti


;
;*************************  Vectors  **************************************
;

             org    Vadc
             fdb    Dummy         ;ADC vector
             fdb    Dummy         ;Keyboard vector
             org    Vtimov
             fdb    HIGH_WATER    ;Timer overflow vector
             org    Vtimch1
             fdb    Dummy         ;Timer channel 1 vector
             fdb    Dummy         ;Timer channel 0 vector
             org    Virq
             fdb    Dummy         ;IRQ vector
             fdb    Dummy         ;SWI vector
             fdb    Start         ;Reset vector

;=====================================================================
             org     $F500

; This is the lookup table for low and high byte pulse period (width is always 1/2 period).
; The value of the ADC (i.e. pot voltage) is then indexed into these tables to find the low and high
; byte values needed to put n the TIMER register (timer running at 1 us clock)
;
; To calibrate the pot for RPM reading, measure the wiper voltage, and mark the corresponding angle points:
;
;  500 RPM = 0.31 volts
; 1000 RPM = 0.70 volts
; 1500 RPM = 1.09 volts
; 2000 RPM = 1.48 volts
; 2500 RPM = 1.84 volts
; 3000 RPM = 2.27 volts
; 3500 RPM = 2.65 volts
; 4000 RPM = 3.05 volts
; 4500 RPM = 3.44 volts
; 5000 RPM = 3.83 volts
; 5500 RPM = 4.22 volts
; 6000 RPM = 4.61 volts


RPMHIGHBYTES:
	db	65T
	db	52T
	db	43T
	db	37T
	db	32T
	db	28T
	db	26T
	db	23T
	db	21T
	db	20T
	db	18T
	db	17T
	db	16T
	db	15T
	db	14T
	db	13T
	db	13T
	db	12T
	db	11T
	db	11T
	db	10T
	db	10T
	db	10T
	db	9T
	db	9T
	db	8T
	db	8T
	db	8T
	db	8T
	db	7T
	db	7T
	db	7T
	db	7T
	db	7T
	db	6T
	db	6T
	db	6T
	db	6T
	db	6T
	db	6T
	db	5T
	db	5T
	db	5T
	db	5T
	db	5T
	db	5T
	db	5T
	db	5T
	db	5T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T


RPMLOWBYTES:
	db	26T
	db	21T
	db	103T
	db	51T
	db	141T
	db	239T
	db	10T
	db	172T
	db	179T
	db	8T
	db	153T
	db	92T
	db	70T
	db	81T
	db	119T
	db	180T
	db	5T
	db	102T
	db	214T
	db	82T
	db	217T
	db	106T
	db	4T
	db	165T
	db	76T
	db	250T
	db	174T
	db	102T
	db	35T
	db	228T
	db	168T
	db	112T
	db	59T
	db	9T
	db	218T
	db	173T
	db	130T
	db	90T
	db	51T
	db	14T
	db	235T
	db	201T
	db	169T
	db	138T
	db	108T
	db	80T
	db	53T
	db	27T
	db	2T
	db	233T
	db	210T
	db	188T
	db	166T
	db	145T
	db	125T
	db	105T
	db	87T
	db	68T
	db	51T
	db	34T
	db	17T
	db	1T
	db	242T
	db	227T
	db	212T
	db	198T
	db	184T
	db	170T
	db	157T
	db	145T
	db	132T
	db	120T
	db	109T
	db	97T
	db	86T
	db	75T
	db	65T
	db	55T
	db	45T
	db	35T
	db	25T
	db	16T
	db	7T
	db	254T
	db	245T
	db	237T
	db	228T
	db	220T
	db	212T
	db	204T
	db	197T
	db	189T
	db	182T
	db	175T
	db	168T
	db	161T
	db	154T
	db	148T
	db	141T
	db	135T
	db	129T
	db	122T
	db	116T
	db	111T
	db	105T
	db	99T
	db	94T
	db	88T
	db	83T
	db	77T
	db	72T
	db	67T
	db	62T
	db	57T
	db	52T
	db	48T
	db	43T
	db	38T
	db	34T
	db	30T
	db	25T
	db	21T
	db	17T
	db	12T
	db	8T
	db	4T
	db	0T
	db	252T
	db	249T
	db	245T
	db	241T
	db	237T
	db	234T
	db	230T
	db	227T
	db	223T
	db	220T
	db	216T
	db	213T
	db	210T
	db	206T
	db	203T
	db	200T
	db	197T
	db	194T
	db	191T
	db	188T
	db	185T
	db	182T
	db	179T
	db	176T
	db	174T
	db	171T
	db	168T
	db	165T
	db	163T
	db	160T
	db	158T
	db	155T
	db	152T
	db	150T
	db	148T
	db	145T
	db	143T
	db	140T
	db	138T
	db	136T
	db	133T
	db	131T
	db	129T
	db	127T
	db	124T
	db	122T
	db	120T
	db	118T
	db	116T
	db	114T
	db	112T
	db	110T
	db	108T
	db	106T
	db	104T
	db	102T
	db	100T
	db	98T
	db	96T
	db	94T
	db	93T
	db	91T
	db	89T
	db	87T
	db	85T
	db	84T
	db	82T
	db	80T
	db	79T
	db	77T
	db	75T
	db	74T
	db	72T
	db	70T
	db	69T
	db	67T
	db	66T
	db	64T
	db	62T
	db	61T
	db	59T
	db	58T
	db	56T
	db	55T
	db	54T
	db	52T
	db	51T
	db	49T
	db	48T
	db	47T
	db	45T
	db	44T
	db	42T
	db	41T
	db	40T
	db	38T
	db	37T
	db	36T
	db	35T
	db	33T
	db	32T
	db	31T
	db	30T
	db	28T
	db	27T
	db	26T
	db	25T
	db	24T
	db	22T
	db	21T
	db	20T
	db	19T
	db	18T
	db	17T
	db	16T
	db	15T
	db	13T
	db	12T
	db	11T
	db	10T
	db	9T
	db	8T
	db	7T
	db	6T
	db	5T
	db	4T
	db	3T
	db	2T
	db	1T

	end

