 TITLE  "wTX.asm"       
 List P=12F683, R=DEC
 INCLUDE "p12F683.inc"

; data segment
 CBLOCK 0x20                   
 del  					; variables for passing the parameters
 length0, length1, period		; transmission parameters
 bt, cnt, synchro			; local variables	
 ENDC

; code segment
 PAGE
 __CONFIG b'001111000100'
  org 0                      		; start program at the beginning of mem
	bcf	STATUS, RP0      	; switch to BANK 0
	clrf	GPIO			; initialize GPIO
	clrf 	INTCON			; disable all interrupts
	movlw	7
	movwf	CMCON0			; disable the comparator
	clrf	ADCON0			; ADC OFF
  	bsf    	STATUS, RP0      	; change to BANK 1
	clrf	VRCON ^ 0x80		; disable the voltage reference
	movlw	2
	movwf	ANSEL ^ 0x80 		; enable GP1 as analog input
	clrf	PIE1 ^ 0x80		; disable external interrupts
	clrf	IOC ^ 0x80 		; disable interrupt on change	
	clrf	WPU ^ 0x80
	bsf	OPTION_REG ^0x80, 7 	; disable pull-ups 
	movlw	1
	movwf	TRISIO ^ 0x80		; enable GP0 for input
  	bcf    	STATUS, RP0        	; back to BANK 0

	call	delayL			; delay before an operation

	movlw	148/4			; length of the 0 pulse
	movwf	length0
	movlw	200/4			; length of the 1 pulse	
	movwf	length1
	movlw	250/4			; period (250mksek)
	movwf	period
	movlw	100			; synchro impulse length
	movwf	synchro		

loop
	movlw	52
	movwf	bt
	call 	sendByte
	goto 	loop

sendBit					; transmit the C bit
	btfsc	STATUS, C
	goto 	snd1

	movf	length0, w	
	bsf	GPIO, 2			; start the 0-pulse
	addlw	-1
	btfss	STATUS, Z
	goto	$-2
		
	movf	length0, w
	subwf	period, w	
	bcf	GPIO, 2			; start the pause
	addlw	-1
	btfss	STATUS, Z
	goto	$-2
	return

snd1
	movf	length1, w	
	bsf	GPIO, 2			; start the 1-pulse
	addlw	-1
	btfss	STATUS, Z
	goto	$-2
		
	movf	length1, w
	subwf	period, w	
	bcf	GPIO, 2			; start the pause
	addlw	-1
	btfss	STATUS, Z
	goto	$-2
	return

sendSynchro
	movf	synchro, w
	bsf	GPIO, 2			; start the synchro impulse
	addlw	-1
	btfss	STATUS, Z
	goto 	$-2
	bcf	GPIO, 2			; stop the synchro impulse
	movlw	30			; pause after sending a synch
	addlw	-1
	btfss	STATUS, Z
	goto 	$-2
	return

; procedures
sendByte				; transmit a byte in bt
	call 	sendSynchro
	movlw	8			; send 8 bits
	movwf	cnt
sndLoop
	rlf	bt, f			; get the bit to transfer into C
	call 	sendBit
	decfsz	cnt, f	
	goto 	sndLoop
	return

delay					; delay for del milliseconds
	movlw 	200
	sublw	1			; this loop takes 5us*200 = 1ms
	sublw	0			; for PIC12F683 @ 4 Mhz
	btfss	STATUS, Z
	goto 	$-3

	decfsz	del, f
	goto 	delay
	return

delayL					; a 2 sec delay
	movlw	10
	movwf	cnt

	movlw	200
	movwf	del
	call 	delay
	decfsz	cnt, f
	goto	$-4
	return
 end
