*******************************************************
* A/D EEG Example -- EEL-4744 --  EMS & AAA *
* Note that this example is set up to run with SIM11,
* i.e., NOT with BUFFALO
*******************************************************

EEPROM	EQU	$B600
RAM	EQU	$0000
DATA	EQU	$0010
RAMEND	EQU	$01FF
OPTION	EQU	$39
ADCTL	EQU	$30
ADR1	EQU	$31
ADR2	EQU	$32
ADR3	EQU	$33
ADR4	EQU	$34
PACTL	EQU	$26
TMSK2	EQU	$24
TFLG2	EQU	$25
REGS	EQU	$1000

* Bit definitions
BIT6	EQU	%01000000
INV6	EQU	%10111111

* Constants
NSAMP	EQU	100
PE1	EQU	%00000001	;Value for ADCTL:
*				;   SCAN=MULT=CD=CC=CB=0, CA=1

****************************************************
* Pseudo-vector for RTI if using EVBU with BUFFALO
* JRTI	EQU	$00EB

* Interrupt Vectors
	ORG	$FFF0		;Interrupt vector for RTI
	FDB	RTI_ISR		;  when NOT using BUFFALO

****************************************************
* Data Area
	ORG	RAM
CNT	RMB	1
STOP	RMB	1
POINTER	RMB	2

	ORG	DATA
EEG	ZMB	NSAMP

****************************************************
* Main Program
	ORG	EEPROM
	LDS	#RAMEND
       	LDX     #REGS

* Turn on A/D System
	LDAA	#%10000000	; Bit 7 (ADPU) of OPTION
	STAA	OPTION,X	; ..turn on A/D system

* Wait 100 microsec = 200 E-cycles for Charge Pump to Stabilize
	LDAA	#40		; 2 E cycles
WAIT1	DECA			; 2 E cycles
	BNE	WAIT1		; 3 E cycles [(2+3)*40=200 E's]

* Set up RTI for 8.192 ms
	LDAA	#%00000001	; RTR1:RTR0  = 01 for 8.192 ms
	STAA	PACTL,X

* Initialize Constants
	LDAA	#NSAMP		; CNT has the number of samples
	STAA	CNT
	LDY	#EEG		; Y will point to the EEG output table
	STY	POINTER
	CLR	STOP		; When stop = 1, done

* Enable RTI
	BCLR	TFLG2,X INV6	; clear RTIF
	BSET	TMSK2,X BIT6	; enable RTI interrupt
	CLI

HERE	LDAA	STOP  		; When stop = 0, not done;
	BEQ	HERE		;    when stop = 1, done

	BCLR    TMSK2,X BIT6	; Disable RTI interrupt
DONE	BRA	DONE

***************************************************
** RTI INTERRUPT SERVICE ROUTINE
*	* Collect a sample
*	* Get sample counter & exit if count=0
*	* Store sample & decrement sample counter
*	* Clear RTI flag
***************************************************
	ORG	$B700
RTI_ISR	LDX	#REGS
	BRCLR	TFLG2,X BIT6 BYE	;IGNORE ILLEGAL
*					;   INTERRUPT

	BCLR	TFLG2,X INV6	; clear RTIF

	JSR	SAMPLE		; get the sample value (stored in A)

	LDY	POINTER
	STAA	0,Y		; save the value in the table

	DEC	CNT		; If Cnt = done; else not done
	BNE	NotDone		;

	INC	STOP		; done with sampling so set stop flag
	BRA	BYE

NotDone	LDY	POINTER		; Calculate address for next sample
	INY
	STY	POINTER

BYE	RTI

***************************************************
** Subroutine to sample the A/D PE1 pin
** Result is in register A
***************************************************
SAMPLE	LDX	#REGS
	LDAA	#PE1		; SCAN=MULT=0, CD=CC=CB=0, CA=1
	STAA	ADCTL,X		;    PE1=BIT0

	LDAA	#6		; 2 E cycles
WAIT2	DECA			; 2 E cycles
	BNE	WAIT2		; 3 E cycles (2+3)*6=30 E's
*				; Total delay is 30+2 = 32 E’s

	LDAA	ADR1,X		; Get new data
	RTS
