.TITLE  motor control VARIAKLE FREQUANCY WITH OUT PA5 AND IN PA3
	;DATE 10/9/2001 original
	;AUTHOR Nutty Kong
	.area   go	 (ABS)
	.module go
PORTA	=	0X1000
BAUD	=	0X102B
SCCR1	=	0X102C
SCCR2	=	0X102D
SCSR	=	0X102E
SCDR	=	0X102F
CHECK	=	0X3000
PWREAD	=	0X3001		;THIS IS BECAUSE 8 VERSE 16 BIT REG
PW	=	0X3002


	.ORG	0X1040
START:	JSR	SINIT		;SETUP THE SERIAL PORT
	LDD	#0X0000		;THIS WILL BE FOR PW
	STD	PWREAD		;SO THE MOTOR DOSE NOT SPEN
CONT:	JSR	UPDATE		;CHECK FOR NEW SPEED SENT FROM SERIAL
	JSR	MODU		;RUN THE TIMING LOOP
	BRA	CONT		;GO IN AN INFANIT LOOP


SINIT:	LDAA	#0X30		;BAUD RATE OF 9600
	STAA	BAUD		;SET THE BAUD
	LDAA	#0X00		;SETTINGS
	STAA	SCCR1		;STORE
	LDAA	#0X0C		;ENABLE READ / WRITE
	STAA	SCCR2		;STORE IT
	RTS			;RETURN

UPDATE:	LDAA	SCSR		;THIS IS FOR GETTING THE SERIAL STATUS
	BITA	#0x20		;CHECK TO SEE IF THERE IS NEW SPEED SENT
	BEQ	UDRE		;THIS COULD CAUSE TROUBLE SO CHECK IN DEBUG
	LDAA	SCDR		;IF NEW SPEED SENT THEN READ THE ANSCII SENT
	ANDA	#0X0F		;STRIP BITS SO THAT THE NUMBER IS LEFT
	LDAB	#0X0A		;FOR MUL BY 10 DEC
	MUL			;MULTIPLY
	STD	0X2FFF		;IF CORRECT HI BYTE STORED FIRST SO LOWER IN COU
NT
	LDAA	CHECK		;COUNT IS 3000, SHOULD HAVE LSBYTE OF D
	CMPA	PW		;IS THIS A DIFFRENT NUMBER THEN CURRENT SETTING
	BEQ	UDRE		;IF NOT THEN DONT CONTINUE JUST RETURN
	STAA	CHECK		;STORE FOR SAFE KEEPING MAY NOT BE NEEDED
	LDAB	#0X68		;100 BASE 10
	SBA			;THIS IS TO CHECK FOR WEATHER THE NUM IS TO HI
	BEQ	ERROR		;IF TO HI THEN AN ERRORS
	LDAA	CHECK		;GET THE NUMBER BACK FOR THE NEXT CHECK
	BMI	ERROR		;BRANCH IF NEG NO NEG NUBERS ALLOWED
	STAA	PW		;SET THE MOTOR SPEED TO THE NUMBER PWREAD
	JSR	OKS		;SEND OK MESSAGE
	BRA	UDRE		;GOTO RETURN
ERROR:	JSR	ERRORS		;SEND ERROR MESSAGE
UDRE:	RTS			;RETURN

MODU:	LDAA	PW		;CHECK THE VALUE OF PW
	BEQ	PMOD		;IF ZERO THEN DONT SET PA5 HI
	LDAA	#0X20		;FOR SETTING PA5 HI
	STAA	PORTA		;SET MOTOR ON
PMOD:	LDX	#0X0068		;LOAD X WITH 100 DECIMAL COUNT DOWN
LOOP:	CPX	PWREAD		;COMPAIR COUNT DOWN TO PW
	BNE	CDOWN		;IF NOT TIME TO GO LOW CONTIUNE COUNT DOWN
	LDAA	#0X00		;SET PA5 TO LOW
	STAA	PORTA		;TURN OFF THE MOTOR
	NOP			;THIS IS FOR DIGNOSTICS
CDOWN:	DEX			;COUNT DOWN
	BNE	LOOP		;KEEP COUNT DOWN TO ZERO
	RTS			;RETURN

ERRORS:	LDAA	#0X00		;FIRST TURN MOTOR OFF
	STAA	PORTA		;TURN OFF
	LDAA	#0X0D		;THIS IS FOR RETURN
	JSR	WRITE		;SEND OUT THE SERIAL PORT
	LDAA	#0X0A		;THIS IS CHAR FOR LINE FEED
	JSR	WRITE		;SEND LINW FEED TO SCI
	LDX	#EMES		;SET OFFSET TO ZERO
EMCHAR:	LDAA	0,X		;GET A CHARACTOR
	CMPA	#0X51		;CHECK TO SEE IF CHAR IS A CAPITAL Q IS EXCAPE C
HAR
	BEQ	EMRE		;RETURN
	INX			;INC THE OFFSET
	JSR	WRITE		;WRITE THE CHAR OUT
	BRA	EMCHAR		;WRITE NEXT
EMRE:	RTS			;RETURN

OKS:	LDAA	#0X0D		;THIS IS FOR RETURN
	JSR	WRITE		;SEND OUT THE SERIAL PORT
	LDAA	#0X0A		;THIS IS CHAR FOR LINE FEED
	JSR	WRITE		;SEND LINW FEED TO SCI
	LDX	#OMES		;SET OFFSET TO ZERO
OMCHAR:	LDAA	0,X			;GET A CHARACTOR
	CMPA	#0X51		;CHECK TO SEE IF CHAR IS A CAPITAL Q IS EXCAPE C
HAR
	BEQ	OMRE		;RETURN
	INX			;INC THE OFFSET
	JSR	WRITE		;WRITE TH CHAR OUT
	BRA	EMCHAR		;WRITE NEXT
OMRE:	RTS			;RETURN



WRITE:	LDAB	SCSR		;GET THE STATUS OF THE SERIAL PORT
	BITB	#0X80		;IS BIT 7 HI
	BEQ	WRITE		;IF NOT HI THEN LOOP UTILL CLEAR
	STAA	SCDR		;SEND CGOHAOUT SERIAL PORT
	RTS			;RETURN

EMES:	.ASCII	"ERROR!Q"
OMES:	.ASCII	"OKQ"

1