;  CD-player PIONEER PD-4500 remote controller

#INCLUDE "LIBREG.ASM"	;8051 SFR set

CLK_KHZ	= 8000		;OSC frequency, KHZ
RTC_MS	= 20		;system clock , MS

RTCV	= CLK_KHZ*RTC_MS
RTCV	= RTCV/12
RTCV	= - RTCV
RTCV	= RTCV & 0FFFFH
RTCVL	= RTCV & 0FFH
RTCVH	= RTCV >> 8
RTCVH	= RTCVH & 0FFH

;  ------ Constantes ------

STACK	.EQU 056H		;stack location
SYS	.EQU 006H		;RC5 system

; ------ Control Functions Codes ------

NONE	.EQU 000H	;key function - none
COD0	.EQU 001H	;key function - 0
COD1	.EQU 002H	;key function - 1
COD2	.EQU 003H	;key function - 2
COD3	.EQU 004H	;key function - 3
COD4	.EQU 005H	;key function - 4
COD5	.EQU 006H	;key function - 5
COD6	.EQU 007H	;key function - 6
COD7	.EQU 008H	;key function - 7
COD8	.EQU 009H	;key function - 8
COD9	.EQU 00AH	;key function - 9
COD10	.EQU 00BH	;key function - 10
CODRW	.EQU 00CH	;key function - REW
CODFF	.EQU 00DH	;key function - FFD
CODSR	.EQU 00EH	;key function - SEEK REW
CODSF	.EQU 00FH	;key function - SEEK FFD
CODPL	.EQU 010H	;key function - PLAY
CODST	.EQU 011H	;key function - STOP
CODPA	.EQU 015H	;key function - PAUSE
CODOP	.EQU 016H	;key function - OPEN
CODSY	.EQU 017H	;key function - SYNCHRO

; ------ Decoder Codes ------

K_RW	.EQU 000H	;function - REW
K_FF	.EQU 005H	;function - FFD
K_SR	.EQU 006H	;function - SEEK REW
K_SF	.EQU 002H	;function - SEEK FFD
K_PL	.EQU 004H	;function - PLAY
K_ST	.EQU 007H	;function - STOP
K_OP	.EQU 003H	;function - OPEN/CLOSE
K_PA	.EQU 001H	;function - PAUSE

;  ------ Ports ------

SER	.EQU INT0

;  ------ Variables ------

;  Bit addressing memory

P1_COPY	.EQU 021H
DEC_A	.EQU M_21H.2	;decoder address line A
DEC_B	.EQU M_21H.5	;decoder address line B
DEC_C	.EQU M_21H.4	;decoder address line C
DEC_D	.EQU M_21H.3	;decoder address line D

;  Internal data memory

DEL	.EQU 030H		;program timer 1
KEYTM	.EQU 031H		;program timer 2
CODE	.EQU 032H		;last control code
MISTR	.EQU 034H		;missing track number

;  ------ Vectors Area ------

	.ORG 0000H	;reset vector
	LJMP INIT
	.ORG 0003H	;INT0 vector
	LJMP RC5
	.ORG 000BH	;INT TIMER 0 vector
	LJMP RTC

;  ------ Main Program ------

INIT:	MOV SP,#STACK	;stack init

	MOV R37,#0H
	MOV CODE,R37

	MOV P1_COPY,#0FFH	;clear P1 copy byte

	MOV MISTR,#0H	;missing track init

	MOV TMOD,#11H    	;timer 0 and timer 1 init
	CLR TR0          	;timer 0 stop
	CLR TR1          	;timer 1 stop
	MOV TL0,#RTCVL   	;timer 0 load
	MOV TH0,#RTCVH

	CLR PX0          	;low    INT0 priority
	SETB IT0         	;fall   INT0 activating
	SETB EX0		;enable INT0
	CLR PT0          	;timer 0 int low priority
	SETB ET0         	;timer 0 int enable
	SETB TR0         	;timer 0 start
	SETB PT1         	;timer 1 int high priority
	SETB EA          	;interrupts enable

MAIN:     CLR EX0		;RC int disable
	MOV A,R37		;command code load
	ANL R37,#0C0H	;clear com. bits
	SETB EX0		;RC int enable
	MOV R0,A
	ANL A,#3FH	;control bits clear
	JNZ PRESS
	LJMP MAIN1	;no press or unused code

PRESS:	MOV A,R0
	CJNE A,CODE,DIF	;jump if new command
	ANL A,#3FH	;control bits clear
	MOV R7,A
	LJMP SAME

DIF:	MOV CODE,A	;new press, store code
	ANL A,#3FH	;control bits clear
	MOV R7,A

;Control Functions Analysis and Processing

	CJNE R7,#COD0,$+6H	;0
	LCALL KEY0
	CJNE R7,#COD1,$+6H	;1
	LCALL KEY1
	CJNE R7,#COD2,$+6H	;2
	LCALL KEY2
	CJNE R7,#COD3,$+6H	;3
	LCALL KEY3
	CJNE R7,#COD4,$+6H	;4
	LCALL KEY4
	CJNE R7,#COD5,$+6H	;5
	LCALL KEY5
	CJNE R7,#COD6,$+6H	;6
	LCALL KEY6
	CJNE R7,#COD7,$+6H	;7
	LCALL KEY7
	CJNE R7,#COD8,$+6H	;8
	LCALL KEY8
	CJNE R7,#COD9,$+6H	;9
	LCALL KEY9
	CJNE R7,#COD10,$+6H	;10
	LCALL KEY10
	CJNE R7,#CODFF,$+6H	;FFD
	LCALL KEYFF
	CJNE R7,#CODRW,$+6H	;REW
	LCALL KEYRW
	CJNE R7,#CODSF,$+6H	;SEEK FFD
	LCALL KEYSF
	CJNE R7,#CODSR,$+6H	;SEEK REW
	LCALL KEYSR
	CJNE R7,#CODPL,$+6H	;PLAY
	LCALL KEYPL
	CJNE R7,#CODST,$+6H	;STOP
	LCALL KEYST
	CJNE R7,#CODPA,$+6H	;PAUSE
	LCALL KEYPA
	CJNE R7,#CODOP,$+6H	;OPEN
	LCALL KEYOP

SAME:	MOV KEYTM,#7H	;load delay 140mS
	LJMP MAIN

MAIN1:	MOV A,KEYTM
	JNZ MAIN2

	LCALL RELEASE

MAIN2:	LJMP MAIN

;  ------ Subroutines Area ------

;Key "0" processing

KEY0:     MOV A,MISTR
	JZ KR
	LCALL STEP
KR:	RET

;Key "1" processing

KEY1:     MOV A,MISTR
	ADD A,#1H
	LCALL STEP
	RET

;Key "2" processing

KEY2:	MOV A,MISTR
	ADD A,#2H
	LCALL STEP
	RET

;Key "3" processing

KEY3:	MOV A,MISTR
	ADD A,#3H
	LCALL STEP
	RET

;Key "4" processing

KEY4:	MOV A,MISTR
	ADD A,#4H
	LCALL STEP
	RET

;Key "5" processing

KEY5:     MOV A,MISTR
	ADD A,#5H
	LCALL STEP
	RET

;Key "6" processing

KEY6:	MOV A,MISTR
	ADD A,#6H
	LCALL STEP
	RET

;Key "7" processing

KEY7:	MOV A,MISTR
	ADD A,#7H
	LCALL STEP
	RET

;Key "8" processing

KEY8:	MOV A,MISTR
	ADD A,#8H
	LCALL STEP
	RET

;Key "9" processing

KEY9:	MOV A,MISTR
	ADD A,#9H
	LCALL STEP
	RET

;Key "10" processing

KEY10:    MOV A,MISTR
	ADD A,#10
	JB ACC.7,K101
	MOV MISTR,A
K101:	RET

;Key "FFD" processing

KEYFF:    MOV A,#K_FF
	LCALL DLOAD
	MOV MISTR,#0H
	RET

;Key "REW" processing

KEYRW:    MOV A,#K_RW
	LCALL DLOAD
	MOV MISTR,#0H
	RET

;Key "SEEK FFD" processing

KEYSF:	MOV A,#K_SF
	LCALL DLOAD
	MOV MISTR,#0H
	RET

;Key "SEEK REW" processing

KEYSR:    MOV A,#K_SR
	LCALL DLOAD
	MOV MISTR,#0H
	RET

;Key "PLAY" processing

KEYPL:    MOV A,#K_PL
	LCALL DLOAD
	MOV MISTR,#0H
	RET

;Key "STOP" processing

KEYST:    MOV A,#K_ST
	LCALL DLOAD
	MOV MISTR,#0H
	RET

;Key "PAUSE" processing

KEYPA:	MOV A,#K_PA
	LCALL DLOAD
	MOV MISTR,#0H
	RET

;Key "OPEN" processing

KEYOP:    MOV A,#K_OP
	LCALL DLOAD
	MOV MISTR,#0H
	RET

RELEASE:	SETB DEC_A
	SETB DEC_B
	SETB DEC_C
	SETB DEC_D
	MOV P1,P1_COPY
	RET

STEP:     MOV R1,A

	MOV A,#K_ST
	LCALL DLOAD
	LCALL DELAY
	LCALL RELEASE
	LCALL DELAY

STEPS:	MOV A,R1
	JZ DONE
	MOV A,#K_FF	;step
	LCALL DLOAD
	LCALL DELAY
	LCALL RELEASE
	LCALL DELAY
	DEC R1
	SJMP STEPS

DONE:     MOV A,#K_PL
	LCALL DLOAD
	LCALL DELAY
	LCALL RELEASE
	LCALL DELAY

	MOV MISTR,#0H
	RET

DLOAD:	MOV C,ACC.0
	MOV DEC_A,C
	MOV C,ACC.1
	MOV DEC_B,C
	MOV C,ACC.2
	MOV DEC_C,C
	MOV C,ACC.3
	MOV DEC_D,C
	MOV P1,P1_COPY
	RET

DELAY:	MOV DEL,#3	;load delay 60mS
WAIT:	MOV A,DEL
	JNZ WAIT
	RET

;  ------ Interrupts Holders ------

;  TIMER 0 Interrupt
;  System clock 20mS

RTC:	PUSH PSW
	PUSH ACC
	CLR TR0          	;timer 0 stop
	MOV TH0,#RTCVH   	;timer 0 load for 20 mS
	MOV TL0,#RTCVL
	SETB TR0         	;timer start

;  20mS program counters

RTC1:	MOV A,DEL
	JZ RTC11
	DEC DEL

RTC11:	MOV A,KEYTM
	JZ RTC2
	DEC KEYTM

RTC2:	POP ACC
	POP PSW
	RETI

;  RC5 program decoder

#INCLUDE "LIBRC5.ASM"

;RC Recoding Table

RCTAB     .DB COD0	;key code 000H, key function - 0
	.DB COD1	;key code 001H, key function - 1
	.DB COD2	;key code 002H, key function - 2
	.DB COD3	;key code 003H, key function - 3
	.DB COD4	;key code 004H, key function - 4
	.DB COD5	;key code 005H, key function - 5
	.DB COD6	;key code 006H, key function - 6
	.DB COD7	;key code 007H, key function - 7
	.DB COD8	;key code 008H, key function - 8
	.DB COD9	;key code 009H, key function - 9
	.DB COD10	;key code 00AH, key function - 10
	.DB NONE	;key code 00BH, key function - none
	.DB NONE	;key code 00CH, key function - none
	.DB NONE	;key code 00DH, key function - none
	.DB NONE	;key code 00EH, key function - none
	.DB NONE	;key code 00FH, key function - none
	.DB CODFF	;key code 010H, key function - FFD
	.DB CODRW	;key code 011H, key function - REW
	.DB NONE	;key code 012H, key function - none
	.DB NONE	;key code 013H, key function - none
	.DB NONE	;key code 014H, key function - none
	.DB NONE	;key code 015H, key function - none
	.DB NONE	;key code 016H, key function - none
	.DB NONE	;key code 017H, key function - none
	.DB NONE	;key code 018H, key function - none
	.DB NONE	;key code 019H, key function - none
	.DB NONE	;key code 01AH, key function - none
	.DB NONE	;key code 01BH, key function - none
	.DB NONE	;key code 01CH, key function - none
	.DB NONE	;key code 01DH, key function - none
	.DB CODOP	;key code 01EH, key function - OPEN
	.DB NONE	;key code 01FH, key function - none
	.DB CODSF	;key code 020H, key function - SEEK FFD
	.DB CODSR	;key code 021H, key function - SEEK REW
	.DB CODSY	;key code 022H, key function - SYNCHRO
	.DB NONE	;key code 023H, key function - none
	.DB NONE	;key code 024H, key function - none
	.DB NONE	;key code 025H, key function - none
	.DB NONE	;key code 026H, key function - none
	.DB NONE	;key code 027H, key function - none
	.DB NONE	;key code 028H, key function - none
	.DB NONE	;key code 029H, key function - none
	.DB NONE	;key code 02AH, key function - none
	.DB NONE	;key code 02BH, key function - none
	.DB NONE	;key code 02CH, key function - none
	.DB NONE	;key code 02DH, key function - none
	.DB NONE	;key code 02EH, key function - none
	.DB NONE	;key code 02FH, key function - none
	.DB NONE	;key code 030H, key function - none
	.DB NONE	;key code 031H, key function - none
	.DB NONE	;key code 032H, key function - none
	.DB NONE	;key code 033H, key function - none
	.DB NONE	;key code 034H, key function - none
	.DB NONE	;key code 035H, key function - none
	.DB NONE	;key code 036H, key function - none
	.DB NONE	;key code 037H, key function - none
	.DB CODPL	;key code 038H, key function - PLAY
	.DB NONE	;key code 039H, key function - none
	.DB NONE	;key code 03AH, key function - none
	.DB CODPA	;key code 03BH, key function - PAUSE
	.DB NONE	;key code 03CH, key function - none
	.DB NONE	;key code 03DH, key function - none
	.DB NONE	;key code 03EH, key function - none
	.DB CODST	;key code 03FH, key function - STOP

	.END
