;dat project oct 98 16c54 processor version 1.0

;required -		generate 2.4ms start bit
;				 1.2ms "1"
;				 0.6ms "0"
;  clock is 4.43MHz	

;			read 7 of 8-way dil switch for command codes
;			generate remaining 5 bits to give 12 bit sequence, presettable
;			start pushbutton 
;			
;			sircs output high = off
;			output low = active, "1" or "0" depends on pulse length
;	 p=16C54

tmr0    equ     01h     ;rtcc
status  equ     03h
porta   equ     05h     ;ports 0-1 outputs, sircs, line-3, 2,3 to be defined
portb   equ     06h     ;ports 0-6 sircs code inputs, porta,7 start button
		
count	equ	09h	;temporary count register
timer	equ	10	;temporary store for 0.6ms time block
dil	equ	11	;dil switch as read
hibyte	equ	012h	;upper byte of sircs, set 1110, upper four bits unused
lobyte	equ	013h	;bits 0-6 from dil sw, bit 7 set at zero

#define	c	status,0;carry bit
#define	sircs	porta,0	;sircs output port
#define	but	portb,7	;start button, active high


;*********************************************** main programme

	org     1ffh
reset   goto    start
	org     0

start   call    init

main	clrwdt
	call	readsw
	btfss	dil,7		;tests for pushbutton active high
	goto	main		;if not loop back
	call	debounce
	btfss	dil,7
	goto	main	
	
	call	build		;if button still active, build sircs sequence
	call	tx		;then send it
	call	gap

	goto	main

;**************************************************************initialise

init    movlw   00h		;
        option         		;prescaler set to 1:2
        
        movlw   b'11110000'
        tris    porta  		;sets port a to outputs 
	

       
	movlw   b'11111111'	
        tris    portb  		;sets port b to  inputs

	clrf	tmr0		;clear rtcc prescaler
   	bsf	sircs		;set sircs ouput high (inactive)
	
	clrf	tmr0
	retlw   0

;*************************************************************debounce pushbutton

debounce
	movlw	040h
	movwf	debounce
loop6	decfsz	debounce,1
	goto	loop6
	retlw	0

;*************************************************************delay 600usec

dly6	clrwdt
	movlw	01fh		;
	movwf	timer
	clrf	tmr0		;clear rtcc

loop1	btfss	tmr0,3		;
	goto	loop1

	clrf	tmr0

	decfsz	timer,1		;decrement timer
	goto	loop1

	retlw	0		;  



;*************************************************************read dil switch
readsw	movf	portb,0
	movwf	dil		;read dil switch and start button and load to dil register
	
	retlw	0


;*************************************************************build sircs code

build	
	movf	dil,0
	movwf	lobyte		;move current dil sw setting to lobyte

	bcf	dil,7		;ensure bit 7 is zero
	clrf	hibyte
	movlw	b'11111110'	; 
	movwf	hibyte		;load hibyte with 4 msbits

	retlw	0


;*************************************************************transmit sircs

tx	clrwdt
				;output is porta,0
	movlw	04h
	movwf	count
loop2	bcf	sircs		;clear (active) sircs output port
	call	dly6
	decfsz	count,1
	goto	loop2		;wait 4 * 0.6ms
	bsf	sircs		;complete start bit

	call	dly6		;wait for pause period

				;now do lobyte
				;"1"=2*dly6 and "0"=1*dly6 low, "pause"=1*dly6 high

	movlw	08h
	movwf	count		;load count with 8 (for lobyte shift)
	bcf	lobyte,7
loop3	rrf	lobyte,1	;shift through carry

	bcf	sircs		;set output active low
	btfss	c		;test carry bit
	goto	delay2		;if clear go to first 0.6ms delay
	call	dly6
delay2	call	dly6		;if set arrive here, direct or via previous delay
	bsf	sircs		;end active pulse
	call	dly6		;wait for inactive pause
	decfsz	count,1
	goto	loop3		;do it 8 times

				;then do hibyte- first four shifts only	valid	
	movlw	04h
	movwf	count		;load count with 4 (for hibyte shift)

loop4	rrf	hibyte,1	;shift through carry

	bcf	sircs		;set output active low
	btfss	c		;test carry bit
	goto	delay3		;if clear go to first 0.6ms delay
	call	dly6
delay3	call	dly6		;if set arrive here, direct or via previous delay
	bsf	sircs		;end active pulse
	call	dly6		;wait for inactive pause
	decfsz	count,1
	goto	loop4		;do it 4 times
	bsf	sircs		;ensure sircs is high on finishing code			
	retlw	0

;************************************************************* gap between pulse codes

gap	movlw	010h		;gap length approx ##64*0.6ms
	movwf	count
loop5	call	dly6
	decfsz	count,1
	goto	loop5
	retlw	0
;*************************************************************


	end		
