   LIST      P=16F877A
      #include   "p16F877A.INC"
         __CONFIG    _CP_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC & _BODEN_OFF &_LVP_OFF &_CPD_OFF ;&_WRT_ENABLE_OFF   
      ERRORLEVEL   -302




;////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
; MACROS
;////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

BANK0      MACRO
      BCF   STATUS,RP0
      BCF   STATUS,RP1
      ENDM

BANK1      MACRO
      BSF   STATUS,RP0
      BCF   STATUS,RP1
      ENDM

BANK2      MACRO
      BSF   STATUS,RP1
      BCF   STATUS,RP0
      ENDM

BANK3      MACRO
      BSF   STATUS,RP1
      BSF   STATUS,RP0
      ENDM




#DEFINE envia_codi      PORTC,6  ;port per enviar el codi
#DEFINE polsador        PORTC,3  ;para hacer pruebas de envio de datos

;////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

PDel0      EQU   0X20
PDel1      EQU   0X21   


;////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

      ORG   0X0000
      GOTO   INICIO
      ORG   0X04
      return
      ORG   0X05

INICIO

	  BANK0
	 CLRF   PORTA
	 CLRF   PORTB
	 CLRF   PORTC
	 CLRF   PORTD
	
      BANK1
      MOVLW   0x06       ;PINS DEL PUERTO A Y PUERTO E TODOS DIGITALES.
      MOVWF   ADCON1
      CLRW
      MOVWF   TRISA
      MOVWF   TRISB
      MOVWF   TRISD
	  movlw b'00001000' ; C3 entrada pulsador
	  MOVWF   TRISC
	  BANK0


   
 Main
	btfsc polsador	;esperamos que pulse
	goto Main		;no han pulsado aún, volvemos
    call DEMORA40ms	;pausa antirrebotes
   
     call    envia_paraula
     call    DEMORA8ms
     call    DEMORA8ms
     call    envia_paraula
     call    DEMORA8ms
     call    DEMORA8ms
	 goto 	 Main





envia_paraula

   call bit_alt     ;Una paraula la formen 9 "digits"
   call bit_alt     ;els 5 primers son de la direccio
   call bit_baix    ;i els altres 4 de la dada.
   call bit_baix    ;Aquests són els cinc
   call bit_alt     ;digits de la direccio.
   
   call bit_alt     ;Aquests són els 4 "digits"
   call bit_alt     ;que formen la dada.
   call bit_high_imp; AGREGO UN HIGH IMPEDANCE COMO EJEMPLO
   call bit_baix
   return




bit_alt

   call DEMORA05ms
   bsf envia_codi
   call DEMORA3.5ms
   bcf envia_codi
   call DEMORA05ms
   bsf envia_codi
   call DEMORA3.5ms
   bcf envia_codi
   return

bit_baix
   call DEMORA05ms
   bsf envia_codi
   call DEMORA05ms
   bcf envia_codi
   call DEMORA3.5ms
   bsf envia_codi
   call DEMORA05ms
   bcf  envia_codi
   call DEMORA3.5ms
	return

bit_high_imp
   call DEMORA05ms
   bsf envia_codi
   call DEMORA3.5ms
   bcf envia_codi
   call DEMORA05ms
   bsf envia_codi
   call DEMORA05ms
   bcf  envia_codi
   call DEMORA3.5ms
	return





;--------------------------------------------------------------
; Demora 0,5ms
DEMORA05ms  movlw     .123      ; 1 set numero de repeticion 
        movwf     PDel0     ; 1 |
PLoop01  clrwdt              ; 1 clear watchdog
        decfsz    PDel0, 1  ; 1 + (1) es el tiempo 0  ?
        goto      PLoop01    ; 2 no, loop
PDelL11  goto PDelL21         ; 2 ciclos delay
PDelL21  clrwdt              ; 1 ciclo delay
        return              ; 2+2 Fin.
;--------------------------------------------------------------



;--------------------------------------------------------------
; Demora 3,5ms
DEMORA3.5ms  movlw     .9       ; 1 set numero de repeticion  (B)
        movwf     PDel0     ; 1 |
PLoop12  movlw     .96       ; 1 set numero de repeticion  (A)
        movwf     PDel1     ; 1 |
PLoop22  clrwdt              ; 1 clear watchdog
        decfsz    PDel1, 1  ; 1 + (1) es el tiempo 0  ? (A)
        goto      PLoop22    ; 2 no, loop
        decfsz    PDel0,  1 ; 1 + (1) es el tiempo 0  ? (B)
        goto      PLoop12    ; 2 no, loop
PDelL12  goto PDelL22         ; 2 ciclos delay
PDelL22  clrwdt              ; 1 ciclo delay
        return              ; 2+2 Fin.
;--------------------------------------------------------------



;--------------------------------------------------------------
; Demora 8ms
DEMORA8ms  movlw     .9       ; 1 set numero de repeticion  (B)
        movwf     PDel0     ; 1 |
PLoop13  movlw     .221      ; 1 set numero de repeticion  (A)
        movwf     PDel1     ; 1 |
PLoop23  clrwdt              ; 1 clear watchdog
PDelL13  goto PDelL23         ; 2 ciclos delay
PDelL23  
        decfsz    PDel1, 1  ; 1 + (1) es el tiempo 0  ? (A)
        goto      PLoop23    ; 2 no, loop
        decfsz    PDel0,  1 ; 1 + (1) es el tiempo 0  ? (B)
        goto      PLoop13    ; 2 no, loop
PDelL33  goto PDelL43         ; 2 ciclos delay
PDelL43  goto PDelL53         ; 2 ciclos delay
PDelL53  clrwdt              ; 1 ciclo delay
        return              ; 2+2 Fin.
;--------------------------------------------------------------




;--------------------------------------------------------------
; Demora 40ms
DEMORA40ms  movlw     .93       ; 1 set numero de repeticion  (B)
        movwf     PDel0     ; 1 |
PLoop14  movlw     .71      ; 1 set numero de repeticion  (A)
        movwf     PDel1     ; 1 |
PLoop24  clrwdt              ; 1 clear watchdog
        decfsz    PDel1, 1  ; 1 + (1) es el tiempo 0  ? (A)
        goto      PLoop24    ; 2 no, loop
        decfsz    PDel0,  1 ; 1 + (1) es el tiempo 0  ? (B)
        goto      PLoop14    ; 2 no, loop
PDelL14  goto PDelL24         ; 2 ciclos delay
PDelL24  clrwdt              ; 1 ciclo delay
        return              ; 2+2 Fin.
;--------------------------------------------------------------
         END