;--------------------------------------------------------
; Title: PHILIPS RC5 protocol emitter
; Description:
;	To be done.
;
; Conventions used:
;	"__"		indicates subroutine or routine
;	"_"			indicates label
;	uppercase	indicates constant or instruction or dfv
;	lowercase	indicates user variable
; Date:	March 2011
; Author: KBT, WARKBT & PS3KBT
;--------------------------------------------------------
; File	"ir-duplicator.asm"
;--------------------------------------------------------
		errorlevel -207
		list p=16F628a, r=dec
#include <p16f628a.inc>
;--------------------------------------------------------
;--------------------------------------------------------
; Config word
; (_HS_OSC||_INTOSC_OSC_NOCLKOUT)&
; _WDT_OFF&
; _LVP_OFF&
; _DATA_CP_OFF&
; _MCLRE_OFF&
; _PWRTE_ON
;--------------------------------------------------------
		__config	0x3f50
;--------------------------------------------------------
; Global variables 0x70 to 0x7f
;--------------------------------------------------------
w2			EQU	0x70	; User W auxiliar
w3			EQU	0x71	; User W auxiliar
;--------------------------------------------------------
; Reset vector 
;--------------------------------------------------------
rst		code	00h
		pagesel	__START
        GOTO	__START
;--------------------------------------------------------
; Main routine (debugged OK) LV0
;
; Description:
;	Self explanatory.
;
;--------------------------------------------------------
		code
__START
		;------------------------------------------------
		; Set memory bank selection
		;------------------------------------------------
		BCF		STATUS,7
		BCF		STATUS,6
		BCF		STATUS,5
		;------------------------------------------------
		; Disable comparators (many people suggest this)
		;------------------------------------------------
		MOVLW	0x07
		MOVWF	CMCON
		;------------------------------------------------
		; Set as output all the port B and A
		;------------------------------------------------
		BSF		STATUS,5
		CLRF	TRISB
		CLRF	TRISA
		;------------------------------------------------
		; Set initial values for port A and B
		;------------------------------------------------
		BCF		STATUS,5
		CLRF	PORTB
		CLRF	PORTA
		;------------------------------------------------
		; Endless main loop
		;------------------------------------------------
		CALL	__DELAY
		MOVLW	0x50
		MOVWF	w3
		CALL	__SEQ
		DECFSZ	w3
		GOTO	$-2
		GOTO	$-6
		;------------------------------------------------
		; Exit
		;------------------------------------------------
		RETURN
;--------------------------------------------------------
; Delay routine base on timer1 (debugged OK)
;
; Description:
;	It use timer 1 (16 bits) overflows by constant 0xff.
;
;--------------------------------------------------------
__DELAY
		MOVLW	0xff
		MOVWF	w2
		BCF		T1CON,0
		CLRF	TMR1H
		CLRF	TMR1L
		BCF		PIR1,0
		MOVLW	0x05
		MOVWF	T1CON
		BTFSS	PIR1,0 
		GOTO	$-1
		DECFSZ	w2,1 
		GOTO	$-6
		BCF		T1CON,0
		;------------------------------------------------
		; Exit
		;------------------------------------------------
		RETURN
;--------------------------------------------------------
; 890 us delay routine base on timer1 (debugged OK)
;
; Description:
;	It use timer 1 (16 bits) overflows.
;
;--------------------------------------------------------
__DELAY_890
		BCF		PORTA,1
		BCF		T1CON,0
		MOVLW	0xfc
		MOVWF	TMR1H
		MOVLW	0x7f
		MOVWF	TMR1L
		BCF		PIR1,0
		MOVLW	0x05
		MOVWF	T1CON
		BTFSS	PIR1,0 
		GOTO	$-1
		BCF		T1CON,0
		BCF		PORTA,1
		;------------------------------------------------
		; Exit
		;------------------------------------------------
		RETURN
;--------------------------------------------------------
__DELAY_890_PWM
		BCF		PORTA,1
		BCF		T1CON,0
		MOVLW	0xfc
		MOVWF	TMR1H
		MOVLW	0x7f
		MOVWF	TMR1L
		BCF		PIR1,0
		MOVLW	0x05
		MOVWF	T1CON
	_DELAY_890_PWM_IL
		;Toggle A1
		MOVF	PORTA,0
		XORLW	0x02
		MOVWF	PORTA
		; 7 cyles
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		; 14 cycles
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		; 21 cycles
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		; 21+2+2+3=28 cycles
		BTFSS	PIR1,0
		GOTO	_DELAY_890_PWM_IL
		BCF		T1CON,0
		BCF		PORTA,1
		;------------------------------------------------
		; Exit
		;------------------------------------------------
		RETURN
;--------------------------------------------------------
__DELAY_90000
		BCF		T1CON,0
		CLRF	TMR1H
		CLRF	TMR1L
		BCF		PIR1,0
		MOVLW	0x05
		MOVWF	T1CON
		BTFSS	PIR1,0 
		GOTO	$-1
		BCF		T1CON,0
		MOVLW	0xa0
		MOVWF	TMR1H
		CLRF	TMR1L
		BCF		PIR1,0
		MOVLW	0x05
		MOVWF	T1CON
		BTFSS	PIR1,0 
		GOTO	$-1
		BCF		T1CON,0
		;------------------------------------------------
		; Exit
		;------------------------------------------------
		RETURN
;--------------------------------------------------------
__ZERO
		CALL	__DELAY_890_PWM
		CALL	__DELAY_890
		RETURN
;--------------------------------------------------------
__ONE
		CALL	__DELAY_890
		CALL	__DELAY_890_PWM
		RETURN
;--------------------------------------------------------
; Executes the RC5 sequence
;
; Description:
;	Uses A1.
;
;--------------------------------------------------------
__SEQ
		CALL	__ONE
		CALL	__ONE
		CALL	__ZERO
		;address
		CALL	__ZERO
		CALL	__ZERO
		CALL	__ZERO
		CALL	__ZERO
		CALL	__ZERO
		;
		CALL	__ZERO
		CALL	__ONE
		CALL	__ZERO
		CALL	__ZERO
		CALL	__ZERO
		CALL	__ZERO
		;
		BCF		PORTA,1
		CALL	__DELAY_90000
		RETURN
;--------------------------------------------------------
		end