;***************************************************************
;*                      Car Alarm System           	       *
;*                           Sender                            *
;*                       Version 1 (4MHz)                      *
;*                            1996                             *
;***************************************************************
	LIST		P=PIC16C71, R=HEX
	INCLUDE		F:\MPASM\INCLUDE\p16c71.inc

	__CONFIG _XT_OSC & _WDT_OFF & _CP_OFF

;----- Port B bits -----

IRLED		equ	3

;----- Local DATA -----

DelayCount	equ	0x0c
Dig1		equ	0x0d
Dig2		equ	0x0e
Dig3		equ	0x0f
Dig4		equ	0x10
Dig5		equ	0x11
Dig6		equ	0x12
Dig7		equ	0x13
Dig8		equ	0x14
Count		equ	0x15
DelayCountLow	equ	0x16
DelayCountHi	equ	0x17

;----- CODE -----

	org	0

	goto	Start

;Start of interrupt routine

	org	0x04

	btfss	INTCON,RBIF
	goto	Iret

	comf	PORTB,W
	andlw	b'11000000'
	btfsc	STATUS,Z
	goto	Iret
	movlw	0x1e
	call	LongDelay
	comf	PORTB,W
	andlw	b'11000000'
	btfsc	STATUS,Z
	goto	Iret
	xorlw	0x03
	movwf	Dig1

	movlw	0xc2
	movwf	Dig2

	movlw	0xa7
	movwf	Dig3

	movlw	0x4f
	movwf	Dig4

	movlw	0xe9
	movwf	Dig5

	movlw	0x85
	movwf	Dig6

	movlw	0x6b
	movwf	Dig7

	movlw	0x1d
	movwf	Dig8

	movlw	0x40
	movwf	Count			;64 bits transmition 

	bsf	PORTB,IRLED
	nop
	nop
	movlw	0x02
	call	Delay			;delay 2*3-1+5=10 mks
	bcf	PORTB,IRLED

	movlw	0xc4
	call	Delay			;delay	196*3-1+5=592 mks
	call	Delay			;delay	196*3-1+5=592 mks

NextDig:
	bsf	PORTB,IRLED
	bcf	STATUS,C
	rrf	Dig8,F
	rrf	Dig7,F
	rrf	Dig6,F
	rrf	Dig5,F
	rrf	Dig4,F
	rrf	Dig3,F
	rrf	Dig2,F
	rrf	Dig1,F
	nop
	nop
	nop
	nop
	bcf	PORTB,IRLED
        
	movlw	0x0a
	call	Delay			;delay 10*3-1+5=34

	btfss	STATUS,C
	goto	Send0

	movlw	0x84
	call	Delay			;delay 132*3-1+5=400 mks

Send0:
	movlw	0x71
	call	Delay			;delay 113*3-1+5=343 mks

	decfsz	Count,F
	goto	NextDig

	nop

	bsf	PORTB,IRLED
	nop
	nop
	movlw	0x02
	call	Delay			;delay 2*3-1+5=10 mks
	bcf	PORTB,IRLED

	movlw	0xc4                               
	call	Delay			;delay	196*3-1+5=592 mks
	call	Delay			;delay	196*3-1+5=592 mks

	bsf	PORTB,IRLED
	nop
	nop
	movlw	0x02
	call	Delay			;delay 2*3-1+5=10 mks
	bcf	PORTB,IRLED

Iret:
	bcf	INTCON,RBIF
	retfie

;End of interrupt routine


Delay
	movwf	DelayCount	;W*3-1+5 mks
DelayStart:
	decfsz	DelayCount,F
	goto	DelayStart
	return

LongDelay   			;W ms delay with 4MHz oscilator
	movwf	DelayCountHi
	clrf	DelayCountLow
DelayLoop:
	nop
	incfsz	DelayCountLow,F
	goto	DelayLoop
	decfsz	DelayCountHi,F
	goto	DelayLoop
	return

Start:
	clrf	PORTA
	clrf	PORTB
	clrf	ADCON0
	clrf	ADRES
	bsf	STATUS,RP0	;Select bank 1
	movlw	b'00011111'     ;Set all as input 
	movwf	TRISA
	movlw	b'11110111'	;RB<3> output the rest are input 
	movwf	TRISB
	movlw	b'11111111'	;Disable Pull-Up resistors
	movwf	OPTION_REG
	bcf	STATUS,RP0	;Select bank 0
	movlw	b'10001000'	;Enable interrupts
	movwf	INTCON

SenderSleep:
	sleep
	goto	SenderSleep


	org	0x3ff		;the last ROM cell in pic16c71
	goto	Start		;if got here then restart the program

	end