;**********************************************************************
; Analyzes IR remote control signals                                  *
;**********************************************************************
;                                                                     *
;    Filename:	    iralyze.asm                                       *
;    Date:          22/8/2002                                         *
;    File Version:  $Revision: 1.1 $                                    *
;                                                                     *
;    Author:        Prashant Bhandary                                 *
;                                                                     *
;**********************************************************************
;
; 
; Currently done for a 16F876
;
	list      p=16f876            ; list directive to define processor
	#include <p16f876.inc>        ; processor specific variable definitions
	
	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _HS_OSC & _WRT_ENABLE_ON & _LVP_OFF & _DEBUG_OFF & _CPD_OFF 

;
dly1    equ     04h
dly2    equ     00h
dly3    equ     00h
baud19_2 equ	.31	;19.2K baud with high speed enable(10MHz crystal)
;
; Error codes
ERR_HEADER	equ	0x80	; Indicates error byte to follow
ERR_NO_SIGNAL	equ	0x41	; Indicates no signal received
ERR_TIMEOUT	equ	0x42	; Indicates end of signal
MaxTimeout	equ	0x80	; Timeout count for delay after signal
;
; Data
	ORG     20H     ; Data Origin
;
DlyHsb  RES     1       ; MSB of delay count
DlyMsb  RES     1       ; MSB of delay count
DlyLsb  RES     1       ; LSB of delay count
;
; Timeout delay waiting for start of signal
Wait1	RES	1
Wait2	RES	1
Wait3	RES	1
;
PortA_	RES	1	; Store last state of Port A
InpBit	equ	0
InpMask	equ	(1<<InpBit)
TicCount	RES	1	; No. of samples
Timeout	RES	1	; Hold timeout once signal received
ErrReg	RES	1	; Holds error to be sent
TxBuf	RES	1	; Holds byte to be transmitted
;
w_temp        EQU     0x70        ; variable used for context saving 
status_temp   EQU     0x71        ; variable used for context saving
;
; Program
;
	ORG     0       ; Reset vector
;
	nop
	goto    main
;
;
	ORG     4       ; Interrupt vector
	retfie                    ; return from interrupt
;
;
;	ORG     8       ; Program starts here
;
main    bsf     STATUS,RP0
	movlw   1
	movwf   TRISA   ; Set Port A to all outputs except RA0
	movlw	7
	movwf	ADCON1	; Disable A/D port
	; Setup serial port
	movlw	baud19_2
	movwf	SPBRG
	bcf     STATUS,RP0
	bsf	RCSTA,SPEN	; Enable serial port
	bsf	RCSTA,CREN	; Enable receive
	bsf     STATUS,RP0
	bsf	TXSTA,TXEN
	bsf	TXSTA,BRGH
	bcf     STATUS,RP0

delay	movlw	dly1
	movwf	DlyHsb
loop4	movlw	dly2
	movwf	DlyMsb
loop3   movlw	dly3
	movwf	DlyLsb
loop2	btfss	RCSTA,OERR
	goto	NoOver
	bcf	RCSTA,CREN
	bsf	RCSTA,CREN
	
NoOver
	btfss	PIR1,RCIF
	goto	NoRecv
	movf	RCREG,0
	;movwf	TXREG
	xorlw	'R'
	btfss	STATUS,Z
	goto	NoRecv
	clrf	ErrReg
	call	ReadIR
ChkTx
	btfss	PIR1,TXIF
	goto	ChkTx
	movf	ErrReg,0
	movwf	TXREG
ChkTx2
	btfss	PIR1,TXIF
	goto	ChkTx2
NoRecv
	decfsz	DlyLsb,1
	goto	loop2
	decfsz	DlyMsb,1
	goto	loop3
	decfsz	DlyHsb,1
	goto	loop4
	movlw	2
	xorwf	PORTA,1
	goto	delay
;
ReadIR
	clrf	TicCount
	clrf	Timeout
	clrf	Wait1
	clrf	Wait2
	movlw	.38
	movwf	Wait3
	clrf	PortA_
WaitStart
	movf	PORTA,0			;1
	xorwf	PortA_,0		;2
	andlw	InpMask			;3
	btfss	STATUS,Z		;4/5
	goto	StartFound		;5
	decfsz	Wait1,1
	goto	WaitStart
	decfsz	Wait2,1
	goto	WaitStart
	decfsz	Wait3,1
	goto	WaitStart
	movlw	ERR_NO_SIGNAL
	movwf	ErrReg
	movlw	ERR_HEADER
	movwf	TXREG
	return

StartFound
	movf	PORTA,0			;7
	movwf	PortA_			;8
	incf	TicCount,1		;9
	goto	Delay1			;10

LoopRead
	movf	PORTA,0			;1
	xorwf	PortA_,0		;2
	andlw	InpMask			;3
	btfss	STATUS,Z		;4/5
	goto	Bchange			;5
	;
	incf	TicCount,1		;6
	movf	TicCount,0		;7
	xorlw	.127			;8
	btfss	STATUS,Z		;9/10
	goto	Delay1			;10
	;
	; count reached 127	
	incf	Timeout,1		;11
	movlw	MaxTimeout		;12
	xorwf	Timeout,0		;13
	btfsc	STATUS,Z		;14/15
	goto	ErrorTOut		;15

	clrf	TicCount		;16
	movlw	.127			;17
	movwf	TxBuf			;18
	btfsc	PortA_,InpBit		;19/20
	bsf	TxBuf,7			;20
	movf	TxBuf,0			;21
	movwf	TXREG			;22
	goto	LoopRead		;24

ErrorTOut
	movlw	ERR_TIMEOUT		;16
	movwf	ErrReg
	movlw	ERR_HEADER
	movwf	TXREG
	return

Bchange
	clrf	Timeout			;7
	incf	TicCount,0		;8
	movwf	TxBuf			;9
	btfsc	PortA_,InpBit		;10/11
	bsf	TxBuf,7			;11
	movf	TxBuf,0			;12
	movwf	TXREG			;13
	clrf	TicCount		;14
	movf	PORTA,0			;15
	movwf	PortA_			;16
	goto	Delay2			;18

Delay1					;11
	nop				;12
	nop				;13
	nop				;14
	nop				;15
	nop				;16
	nop				;17
Delay2					;18
	nop				;18
	nop				;19
	nop				;20
	nop				;21
	nop				;22
	goto	LoopRead		;24
;
	return
;
	end

