; UARTF84s.ASM: BitBang 9600 bps @ XT=4 MHz to see effect (BANKSEL)
; 8/1/2006 have Echo! but it's incorrect Chr. LED=>OFF when 1st key hit. OK!
; .14 (was 13, 12, 15); add DLY52us at biginning to get correct "X"

	processor 16f84
	include   <p16f84.inc>
        __config  _XT_OSC & _WDT_OFF & _PWRTE_OFF

RX      equ     1       ; RX=RA1
TX      equ     0       ; TX=RA0
CARRY   equ     0       ; CARRY = STATUS<0>

; Declare variables at RAM memory locations

XMTREG  equ    0x20     ; 0x21    ; 8/1/06 OK?
RCVREG  equ    0x1F     ; 0x22
Count   equ    0x1E     ; 0x23
CountA  equ    0x1D     ; 0x24 - Danger! F84 only has 36 bytes RAM!

; Program
	org     0         ; start at address 0

        ; Set port B as output and initialize RX/TX
        BANKSEL TRISA           ; BANK 1 (page 1)
        BSF     TRISA, RX       ; PORTA1=RX
        BCF     TRISA, TX       ; PORTA0=TX  7/21/06 was TRISB, TX!! wrong!
        BCF     TRISB, 0        ; PORTB0=OUTPUT

        BANKSEL PORTB           ; BANK 0 (page 0)
        BSF     PORTB, 0        ; LED=ON at RB0 (pin 6)

        BSF     PORTA, RX       ;\ INITIALIZE
        BSF     PORTA, TX       ;/
        CALL    DLY52us
        CALL    DLY52us         ; 8/1/2006
        MOVLW   0x58            ; that is 'X'
        MOVWF   XMTREG
AGAIN:
        CALL    XMT_CHR
        CALL    RCV_CHR
        MOVF    RCVREG, W
        MOVWF   XMTREG
        goto    AGAIN
;------------------------------------------------------------------------------
XMT_CHR:
        movlw   8
        movwf   Count
        BCF     PORTA, TX       ; START BIT
        CALL    DLY52us
        CALL    DLY52us
NEXTX:       
        rrf     XMTREG, f          ; LSB -> CARRY 
        btfsc   STATUS, CARRY
        BSF     PORTA, TX
        btfsS   STATUS, CARRY
        BcF     PORTA, TX
        CALL    DLY52us
        CALL    DLY52us
        decfsz  Count, f
        goto    NEXTX
STOPBIT:
        BSF     PORTA, TX
        CALL    DLY52us
        CALL    DLY52us
        RETURN
;---------------------------------------------------------------------------
RCV_CHR:

wait:
        btfsc   PORTA, RX       ; START BIT
        goto    wait
;after test, need to remove!  BANKSEL PORTB           ; BANK 0 (page 0)
 ;       BcF     PORTB, 0  ; LED=OFF for test at RB0 (pin 6) - Works!! 8/1/06
        CALL    DLY52us

        movlw   8
        movwf   Count
        clrf    RCVREG          ;\  clear both before using 
        BCF     STATUS, CARRY   ;/
NEXRX:
        CALL    DLY52us         
        CALL    DLY52us         ; in Mid of another new Bit time

        rrf     RCVREG, f          ; RCVREG<7) was from CARRY
        btfsc   PORTA, RX
        BSF     RCVREG, 7       ; this action overwrites the CARRY effect!
                                ; so only '1' will be marked here, others='0'
        decfsz  Count, f
        goto    NEXRX
        RETURN
;----------------------------------------------------------------------------
DLY52us:
        NOP
        NOP
        movlw   .14            ; 8/1/06 Correct wrong CHR?
        movwf   CountA         ; because many other instructions
LOOP:
        DECFSZ  CountA, f
        goto    LOOP
	RETURN
;-----------------------------------------------------------------
	end     

