;******** PROGRAM PENGATUR KECEPATAN MOTOR ***************
; -
	.DATA
	Org	30H
Kecepatan	Ds	1

	.CODE
;---------------------
;KONSTANTA
;---------------------
ROM			EQU	2000H
TabelKecepatan		EQU	60H


Init_Serial		EQU	0ECH
Serial_In   EQU 010EH             


Init_LCD		EQU	05ECH
PosisiAwal_LCD		EQU	05CBH
Kirim_Perintah		EQU	062BH
Kirim_Karakter		EQU	064AH
KirimPesan_LCD		EQU	05E0H
Baris2_LCD		EQU	0654H
Delay_LCD		EQU	0660h

StepKecepatan		EQU	8

;------------
; Deklarasi I/O
;------------
Motor		Bit	P1.1

	Org	ROM			;Reset Vector
	Ajmp	Start			;
	Org	ROM+3H			;External Interrupt 0 Vector
	Reti				;
	Org	ROM+0BH			;Timer 0 Interrupt Vector
	Ajmp	Timer0Interrupt
	Org	ROM+13H			;External Interrupt 1 Vector
	Reti
	Org	ROM+1BH			;Timer 1 Interrupt Vector
	Reti
	Org	ROM+23H			;Serial Interrupt Vector
	Ajmp	Serial_Interrupt

Start:
	Clr	RI	
	Clr	TI
	Lcall	Init_LCD
	Acall	InisialHardware
	Mov	Kecepatan,#5
	Acall	StartMotor
	Acall	Tampilan_LCD
	ajmp	$

StartMotor:
	Acall	SetTabelKecepatan
	Mov	TH0,#0EEH		;Interupsi timer diaktifkan setiap 5 mS
	Mov	TL0,#00H		;
	Setb	ET0			;
	Setb	TR0			;
	Mov	R0,#TabelKecepatan	;R0 menuju ke tabel data kecepatan motor
	Mov	R7,#StepKecepatan	;R7 diisi dengan jumlah step kecepatan
	Ret

*********** SUBROUTINE INISIAL HARDWARE 
;- Aktifkan Serial Interrupt
;- Isi Tabel Kecepatan dan Tabel Pointer Data Kecepatan

InisialHardware:
	Lcall	Init_Serial		;Inisial Serial Port 9600 bps
	Clr	RI
	Clr	TI
	Acall	HapusTabel		;Isi Tabel Kecepatan dan Pointer data kecepatan
	Mov	Kecepatan,#0
	Setb	EA			;Aktifkan Serial Interrupt
	Setb	ES			;
	Ret
;-------------
;Isi Tabel Kecepatan dengan FF atau tidak ada kecepatan
;Isi Tabel Alamat Step Kecepatan dengan alamat awal dari Tabel Kecepatan
;-------------

HapusTabel:
	Mov	R0,#TabelKecepatan		;R0 ke alamat awal tabel ke
						;cepatan motor
	Mov	R7,#StepKecepatan		;R7 diisi dengan jumlah step
						;kecepatan

LoopKecepatanAwal:
	Mov	@R0,#0FFH			;Isi FFH di seluruh data
	Inc	R0				;kecepatan motor
	Djnz	R7,LoopkecepatanAwal		;
	Ret


;--------
;INTERRUPT SERVICE ROUTINE
;--------
Serial_Interrupt:
	Clr	EA
	Clr	ES			;Disable Interrupt
	Mov	A,SBUF			;
	Push	A
	CLR	TI
	Acall	Numerik?
	Jc	BukanNumerik
	Clr	C
	Subb	A,#30H			;Simpan di variabel kecepatan
	Mov	Kecepatan,A		;

BukanNumerik:
	Pop	B
	Mov	DPTR,#TabelPerintah
	Acall	CariPerintah1B
	Jc	Kembali
	Jmp	@A+DPTR

Numerik?:
	Cjne	A,#'0',$+3		
	Jc	N1
	Cjne	A,#'9',$+3
	Jnc	N2
	Clr	C
	Ret

N2:
	Mov	B,A
	Setb	C
N1:
	Ret

Kembali:		
	Clr	RI			;Hapus Flag Receive
	Clr	TI			;Hapus Flag Transmit
	Setb	ES			;Enable Serial Interrupt
	Setb	EA
	Acall	Tampilan_LCD
	Reti				;Kembali dari interrupt

HapusBufferKecepatan:
	Mov	R0,#TabelKecepatan
	Mov	R7,#StepKecepatan

LoopHapus:
	Mov	@R0,#0FFH
	Inc	R0
	Djnz	R7,LoopHapus	
	Ret

Step:
	Acall	SetTabelKecepatan
	Ljmp	Kembali	

******** PENGATURAN KECEPATAN ******

SetTabelKecepatan:
	Lcall	Hapustabel
	Mov	A,Kecepatan
	Jz	TidakSet
	Mov	R0,#TabelKecepatan
LoopSetTabel:
	Mov	@R0,#00
	Inc	R0
	Djnz	A,LoopSetTabel
TidakSet:
	Ret

TambahKecepatan:
	Push	A
	Mov	A,Kecepatan
	Cjne	A,Step,$+3
	Jnc	TidakInc	
	Inc	Kecepatan
TidakInc:
	Pop	A
	Ajmp	Step


KurangiKecepatan:
	Push	A
	Mov	A,Kecepatan
	Jz	TidakDec
	Dec	Kecepatan
TidakDec:
	Pop	A
	Ajmp	Step

Forward:
	Clr	P1.0
	Acall	Tampilan_Arah_Forward
	Ljmp	Kembali

Reverse:
	Setb	P1.0
	Acall	Tampilan_Arah_Reverse
	Ljmp	Kembali

Timer0Interrupt:
	Clr	ET0
	Mov	TH0,#0EEH
	Mov	TL0,#00

	Mov	A,@R0			;Ambil data di tabel kecepatan motor
	Mov	C,A.0
;	clr	es
;	lcall	0cbh
;	clr	ti
;	setb	es
	Mov	P1.1,C
	Inc	R0			;Tunjuk tabel kecepatan berikutnya
	Djnz	R7,TidakResetTabel
	Mov	R0,#TabelKecepatan
	Mov	R7,#StepKecepatan	
TidakResetTabel:
	Setb	ET0
	Reti

******* AKHIR PENGATURAN KECEPATAN **********
;--------------------
;	Tampilan LCD
;--------------------

Tampilan_LCD:
	Lcall	PosisiAwal_LCD
	Lcall	Delay_LCD
	Lcall	Delay_LCD
	Lcall	Delay_LCD
	Mov 	DPTR,#Tampilan
	Lcall	KirimPesan_LCD
	Mov	A,Kecepatan
	Anl	A,#0FH
	Add	A,#30H
	Lcall	Kirim_Karakter
	Ret

Tampilan_Arah_Forward:
	Lcall	PosisiAwal_LCD
	Lcall	Baris2_LCD
	Mov 	DPTR,#Tampilan_Forward
	Lcall	KirimPesan_LCD
	Ret

Tampilan_Arah_Reverse:
	Lcall	PosisiAwal_LCD
	Lcall	Baris2_LCD
	Mov 	DPTR,#Tampilan_Reverse
	Lcall	KirimPesan_LCD
	Ret


Tampilan:
	DB	'Kecepatan Sekarang : ',0FH

Tampilan_Forward:
	DB	'Forward',0FH

Tampilan_Reverse:
	DB	'Reverse',0FH


TabelPerintah:
	DB	'A'
	DW 	TambahKecepatan
	DB	'B'
	DW	KurangiKecepatan
	DB	'0'
	DW	Step
	DB	'1'
	DW	Step
	DB	'2'
	DW	Step
	DB	'3'
	DW	Step
	DB	'4'
	DW	Step
	DB	'5'
	DW	Step
	DB	'6'
	DW	Step
	DB	'7'
	DW	Step
	DB	'8'
	DW	Step
	DB	'>'
	DW	Forward
	DB	'<'
	DW	Reverse

	DB	0

CariPerintah1B:
LoopCariPerintah:
	Mov	A,#00H
	Movc	A,@A+DPTR
	Jz	PerintahTidakKetemu
	Clr	C
	Subb	A,B
	Jz	PerintahKetemu
	Inc	DPTR
	Inc	DPTR
	Inc	DPTR
	Ljmp	LoopCariPerintah

PerintahTidakKetemu:
	Setb	C
	Ret

PerintahKetemu:
	Inc	DPTR
;	Movx	A,@DPTR
	Mov	A,#00H
	Movc	A,@A+DPTR
	Inc	DPTR
	Push	A
	Mov	A,#00H
	Movc	A,@A+DPTR
	Mov	DPH,A
	Pop	DPL
	Mov	A,#00H
	Clr	C
	Ret

