;------------------------------------------------------------
; Código assembler generado por Niple V5.5.1
; Proyecto: 
; Autor: Andres Manzoni
; Fecha: 01/07/2010
; PIC: 16F676
; Velocidad de reloj: Int 4 Mhz
; Descripcion: 
;------------------------------------------------------------


 LIST    P=PIC16F676


_INCRC_OSC_NOCLKOUT   equ  0x3FFC
_WDT_OFF              equ  0x3FF7
_PWRTE_ON             equ  0x3FEF
_MCLRE_ON             equ  0x3FFF
_BODEN_OFF            equ  0x3FBF
_CPD_OFF              equ  0x3FFF
_CP_OFF               equ  0x3FFF

 __config _INCRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _BODEN_OFF & _CPD_OFF & _CP_OFF


;------------------------------------------------------------
;                  Declaración de Registros
;------------------------------------------------------------
w                     equ  0x0000
status                equ  0x0003
porta                 equ  0x0005
portc                 equ  0x0007
pclath                equ  0x000a
cmcon                 equ  0x0019
adresh                equ  0x001e
adcon0                equ  0x001f
salida                equ  0x0021
t_retorno             equ  0x0022
t_inyec               equ  0x0023
time                  equ  0x0024
funcion               equ  0x0025
esclavo               equ  0x0026
id                    equ  0x0027
_np_temp1             equ  0x0028
_np_temp2             equ  0x0029
_np_temp3             equ  0x002a
_np_temp4             equ  0x002b
_np_temp5             equ  0x002c
_np_tiempo            equ  0x002d
_np_veces             equ  0x002e
_np_tiempo1           equ  0x002f
_np_tiempo2           equ  0x0030
_np_rs232_t           equ  0x0031
_np_rs232_t1          equ  0x0032
_np_espera1           equ  0x0033
_np_espera2           equ  0x0034
_np_actint_a          equ  0x0035
_np_actint_b          equ  0x0036
_np_banderas          equ  0x0037
t_max                 equ  0x0039
t_min                 equ  0x003a
_np_tmp_var1          equ  0x003b
_np_tiempo3           equ  0x003c
trisa                 equ  0x0085
trisc                 equ  0x0087
ansel                 equ  0x0091
adcon1                equ  0x009f


;------------------------------------------------------------
;                  Declaración de Bits
;------------------------------------------------------------
adfm                  equ  7   ;
adon                  equ  0   ;
c                     equ  0   ;carry / borrow bit
go_done               equ  1   ;
ra4                   equ  4   ;bit 4 del puerto a
ra5                   equ  5   ;bit 5 del puerto a
rc0                   equ  0   ;bit 0 del puerto c
rc3                   equ  3   ;bit 3 del puerto c
rc4                   equ  4   ;bit 4 del puerto c
rc5                   equ  5   ;bit 5 del puerto c
rp0                   equ  5   ;registrer bank select bit
rp1                   equ  6   ;registrer bank select bit
z                     equ  2   ;bit cero
rele0                 equ  0   ;
rele1                 equ  1   ;
rele2                 equ  2   ;
rele3                 equ  3   ;
rele4                 equ  4   ;
rx_ok                 equ  0   ;bandera de recepción de dato ok


;------------------------------------------------------------
;               Declaración de Registros EEPROM
;------------------------------------------------------------


;------------------------------------------------------------
;                        Inicio
;------------------------------------------------------------

   org 0x00
   goto paso2


;------------------------------------------------------------
;                      programa principal
;------------------------------------------------------------
paso2
   bcf status,rp0                   ;cambiar a banco 0
   clrf salida
   clrf t_retorno
   clrf t_inyec
   clrf time
   clrf funcion
   clrf esclavo
   clrf id
   clrf _np_temp1
   clrf _np_temp2
   clrf _np_temp3
   clrf _np_temp4
   clrf _np_temp5
   clrf _np_tiempo
   clrf _np_veces
   clrf _np_tiempo1
   clrf _np_tiempo2
   clrf _np_rs232_t
   clrf _np_rs232_t1
   clrf _np_espera1
   clrf _np_espera2
   clrf _np_actint_a
   clrf _np_actint_b
   clrf _np_banderas
   clrf t_max
   clrf t_min
   clrf _np_tmp_var1
   clrf _np_tiempo3
   clrf porta
   movlw b'00000111'                ;puerto a = e/s digital
   movwf cmcon
   movlw b'00010000'                ;configuracion a/d = dava
   bsf status,rp0                   ;cambiar a banco 1
   movwf adcon1
   bcf status,rp0                   ;cambiar a banco 0
   clrf adcon0
   bsf adcon0,6
   bsf status,rp0                   ;cambiar a banco 1
   bsf ansel,0
   bcf ansel,1
   bsf ansel,2
   bcf ansel,3
   movlw b'00000111'                ;configurar el puerto a como xxssxava
   movwf trisa
   bcf status,rp0                   ;cambiar a banco 0
   clrf portc
   bsf status,rp0                   ;cambiar a banco 1
   bcf ansel,4
   bcf ansel,5
   bcf ansel,6
   bcf ansel,7
   movlw b'00000100'                ;configurar el puerto c como xxsssess
   movwf trisc
   bcf status,rp0                   ;cambiar a banco 0
   bsf adcon0,adon                  ;activar activar el conversor a/d.
paso5
   movlw d'001'
   movwf esclavo
   movlw d'000'
   movwf id
   bcf adcon0,2
   bcf adcon0,3
   bcf adcon0,4
   bcf adcon0,1
   bcf adcon0,adfm                  ;alinear el resultado a la izquierda

   clrf _np_temp3
   clrf _np_temp4
   movlw .8
   movwf _np_temp5
paso7_ciclos
   call leer_ad
   movf adresh,w                    ;recuperar el registro alto de la conversion a/d
   movwf t_retorno
   addwf _np_temp4,1
   btfsc status,c
   incf _np_temp3,1
   movlw .1                         ;temporizador 1 mse
   call tmp_1_mc
   decfsz _np_temp5,1
   goto paso7_ciclos
   bcf status,c
   rrf _np_temp3,1
   rrf _np_temp4,1
   bcf status,c
   rrf _np_temp3,1
   rrf _np_temp4,1
   bcf status,c
   rrf _np_temp3,1
   rrf _np_temp4,1
   movf _np_temp4,w
   movwf t_retorno
   bcf adcon0,2
   bsf adcon0,3
   bcf adcon0,4
   bcf adcon0,1
   bcf adcon0,adfm                  ;alinear el resultado a la izquierda

   clrf _np_temp3
   clrf _np_temp4
   movlw .8
   movwf _np_temp5
paso8_ciclos
   call leer_ad
   movf adresh,w                    ;recuperar el registro alto de la conversion a/d
   movwf t_inyec
   addwf _np_temp4,1
   btfsc status,c
   incf _np_temp3,1
   movlw .1                         ;temporizador 1 mse
   call tmp_1_mc
   decfsz _np_temp5,1
   goto paso8_ciclos
   bcf status,c
   rrf _np_temp3,1
   rrf _np_temp4,1
   bcf status,c
   rrf _np_temp3,1
   rrf _np_temp4,1
   bcf status,c
   rrf _np_temp3,1
   rrf _np_temp4,1
   movf _np_temp4,w
   movwf t_inyec
   bcf portc,rc0
   clrf _np_espera1                 ;esperar el byte 1
   clrf _np_espera2
paso10_esperar_byte_1
   incf _np_espera2,1
   btfss status,z
   goto paso10_bit_inicio_1
   incf _np_espera1,1
   btfss status,z
   goto paso10_bit_inicio_1
   bcf _np_banderas,rx_ok
   goto paso10_termina
paso10_bit_inicio_1
   btfsc portc,2
   goto paso10_esperar_byte_1
   call rs232_rc2_9600
   movf _np_temp1,w
   movwf id
   clrf _np_espera1                 ;esperar el byte 2
   clrf _np_espera2
paso10_esperar_byte_2
   incf _np_espera2,1
   btfss status,z
   goto paso10_bit_inicio_2
   incf _np_espera1,1
   btfss status,z
   goto paso10_bit_inicio_2
   bcf _np_banderas,rx_ok
   goto paso10_termina
paso10_bit_inicio_2
   btfsc portc,2
   goto paso10_esperar_byte_2
   call rs232_rc2_9600
   movf _np_temp1,w
   movwf funcion
paso10_termina
   goto paso10_salir
paso10_err
   bcf _np_banderas,rx_ok           ;error de comunicaciones.
paso10_salir
   movf id,w                        ;si el reg id = esclavo
   xorwf esclavo,w                  ;si el reg id = esclavo
   btfss status,z
   goto paso18
   movf funcion,w                   ;si el reg funcion = d'32'
   xorlw d'32'
   btfss status,z
   goto paso19
   bsf portc,rc0
   movlw .24                        ;temporizador 24 mse
   call tmp_1_mc
   movf esclavo,w                   ;enviar el dato esclavo
   movwf _np_temp1
   call rs232_tc1_9600
   movf t_retorno,w                 ;enviar el dato t_retorno
   movwf _np_temp1
   call rs232_tc1_9600
   movf t_inyec,w                   ;enviar el dato t_inyec
   movwf _np_temp1
   call rs232_tc1_9600
   movf salida,w                    ;enviar el dato salida
   movwf _np_temp1
   call rs232_tc1_9600
paso16
   movf salida,w
   movwf funcion
paso17
   movf funcion,w
   movwf salida
paso18
   call usr_marcha
   goto paso5                       ;cierra el ciclo
paso19
   movf funcion,w                   ;si el reg funcion = d'64'
   xorlw d'64'
   btfss status,z
   goto paso23
   bcf portc,rc0
   movlw .24                        ;temporizador 24 mse
   call tmp_1_mc
   clrf _np_espera1                 ;esperar el byte 1
   clrf _np_espera2
paso22_esperar_byte_1
   incf _np_espera2,1
   btfss status,z
   goto paso22_bit_inicio_1
   incf _np_espera1,1
   btfss status,z
   goto paso22_bit_inicio_1
   bcf _np_banderas,rx_ok
   goto paso22_termina
paso22_bit_inicio_1
   btfsc portc,2
   goto paso22_esperar_byte_1
   call rs232_rc2_9600
   movf _np_temp1,w
   movwf t_max
   clrf _np_espera1                 ;esperar el byte 2
   clrf _np_espera2
paso22_esperar_byte_2
   incf _np_espera2,1
   btfss status,z
   goto paso22_bit_inicio_2
   incf _np_espera1,1
   btfss status,z
   goto paso22_bit_inicio_2
   bcf _np_banderas,rx_ok
   goto paso22_termina
paso22_bit_inicio_2
   btfsc portc,2
   goto paso22_esperar_byte_2
   call rs232_rc2_9600
   movf _np_temp1,w
   movwf t_min
   clrf _np_espera1                 ;esperar el byte 3
   clrf _np_espera2
paso22_esperar_byte_3
   incf _np_espera2,1
   btfss status,z
   goto paso22_bit_inicio_3
   incf _np_espera1,1
   btfss status,z
   goto paso22_bit_inicio_3
   bcf _np_banderas,rx_ok
   goto paso22_termina
paso22_bit_inicio_3
   btfsc portc,2
   goto paso22_esperar_byte_3
   call rs232_rc2_9600
   movf _np_temp1,w
   movwf time
paso22_termina
   goto paso22_salir
paso22_err
   bcf _np_banderas,rx_ok           ;error de comunicaciones.
paso22_salir
   goto paso16                      ;cierra el ciclo
paso23
   movlw d'32'                      ;si el reg funcion < d'32'
   subwf funcion,w
   btfss status,c
   goto paso17                      ;cierra el ciclo
   goto paso18                      ;cierra el ciclo


;------------------------------------------------------------
;                  Declaración de Subrutinas
;------------------------------------------------------------

usr_marcha
   bcf status,rp0                   ;cambiar a banco 0
   btfsc salida,rele0
   bsf porta,ra5
   btfss salida,rele0
   bcf porta,ra5
   btfsc salida,rele1
   bsf porta,ra4
   btfss salida,rele1
   bcf porta,ra4
   btfsc salida,rele2
   bsf portc,rc3
   btfss salida,rele2
   bcf portc,rc3
   btfsc salida,rele3
   bsf portc,rc4
   btfss salida,rele3
   bcf portc,rc4
   btfsc salida,rele4
   bsf portc,rc5
   btfss salida,rele4
   bcf portc,rc5
   btfss porta,ra5                  ;si el bit ra5 = 1
   goto usr_0_marcha16
   movf t_retorno,w                 ;si el reg t_retorno <= t_min
   subwf t_min,w
   btfss status,c
   goto usr_0_marcha14
   bsf porta,ra4
usr_0_marcha10
   btfss portc,rc3                  ;si el bit rc3 = 1
   goto usr_0_marcha12
   movf time,w                      ;temporizador variable
   btfsc status,z
   goto usr_0_marcha11_no
   movwf _np_tmp_var1
usr_0_marcha11_tmp_var
   movlw .1
   call tmp_1_mgc
   decfsz _np_tmp_var1
   goto usr_0_marcha11_tmp_var
usr_0_marcha11_no
usr_0_marcha12
   bcf portc,rc3
usr_0_marcha13
   goto usr_marcha_salir
   goto usr_0_marcha12              ;cierra el ciclo
usr_0_marcha14
   movf t_max,w                     ;si el reg t_retorno >= t_max
   subwf t_retorno,w
   btfss status,c
   goto usr_0_marcha10              ;cierra el ciclo
   bcf porta,ra4
   goto usr_0_marcha10              ;cierra el ciclo
usr_0_marcha16
   bcf porta,ra5
   goto usr_marcha_salir
usr_marcha_salir
   bcf status,rp0                   ;cambiar a banco 0
   return


leer_ad
   ;realizar conversion a/d
   movlw .200
   movwf _np_temp1
leer_ad_esperar
   decfsz _np_temp1,1
   goto leer_ad_esperar
   bsf adcon0,go_done
   nop
   nop
leer_ad_esperar_fin
   btfsc adcon0,go_done
   goto leer_ad_esperar_fin
   return

tmp_1_mc
   movwf _np_tiempo
   goto tmp_1_mc_1
tmp_1_mc_bucle1
   nop
   nop
   nop
   nop
   nop
   nop
   nop
tmp_1_mc_1
   movlw .246
   movwf _np_tiempo1
tmp_1_mc_bucle2
   nop
   decfsz _np_tiempo1,1
   goto tmp_1_mc_bucle2
   nop
   nop
   nop
   nop
   nop
   decfsz _np_tiempo,1
   goto tmp_1_mc_bucle1
   return


rs232_rc2_9600
   ;recibir un dato rs232 por portc,2
   ;_np_temp1 = dato recibido
   ;_np_temp2 = contador de bits
   bcf status,rp0                   ;cambiar a banco 0
   bsf _np_banderas,rx_ok
   clrf _np_temp1
   btfsc portc,2
   goto rs232_rc2_9600_ruido
   call rs232_9600_mbit
   btfsc portc,2
   goto rs232_rc2_9600_ruido
   movlw 9
   movwf _np_temp2
rs232_rc2_9600_rx_otro
   bsf status,c
   btfss portc,2
   bcf status,c
   rrf _np_temp1,1
   call rs232_9600_bit
   decfsz _np_temp2,1
   goto rs232_rc2_9600_rx_otro
   goto rs232_rc2_9600_salir
rs232_rc2_9600_ruido
   bcf _np_banderas,rx_ok
rs232_rc2_9600_salir
   return


rs232_tc1_9600
   ;enviar un dato rs232 por portc,1 (por codigo)
   ;_np_temp1 = dato a enviar
   ;_np_temp2 = contador de bits
   movlw .8
   bcf status,rp0                   ;cambiar a banco 0
   movwf _np_temp2
   bcf portc,1
   call rs232_9600_bit
rs232_tc1_9600_tx_otro
   bcf portc,1
   bcf status,c
   rrf _np_temp1,1
   btfsc status,c
   bsf portc,1
   call rs232_9600_bit
   decfsz _np_temp2,1
   goto rs232_tc1_9600_tx_otro
   bsf portc,1
   call rs232_9600_bit
   return


tmp_1_mgc
   movwf _np_tiempo
   goto tmp_1_mgc_1
tmp_1_mgc_bucle2
   nop
   nop
   nop
   nop
   nop
   nop
   nop
tmp_1_mgc_1
   movlw .10
   movwf _np_tiempo1
tmp_1_mgc_bucle1
   nop
   nop
   movlw .99
   movwf _np_tiempo2
tmp_1_mgc_bucle0
   nop
   nop
   nop
   movlw .200
   movwf _np_tiempo3
tmp_1_mgc_bucle
   nop
   nop
   decfsz _np_tiempo3,1
   goto tmp_1_mgc_bucle
   nop
   nop
   nop
   decfsz _np_tiempo2,1
   goto tmp_1_mgc_bucle0
   nop
   nop
   nop
   decfsz _np_tiempo1,1
   goto tmp_1_mgc_bucle1
   decfsz _np_tiempo,1
   goto tmp_1_mgc_bucle2
   return


rs232_9600_bit
   movlw .9
   movwf _np_rs232_t
   goto rs232_9600_bit_2
rs232_9600_bit_bucle
   nop
   nop
   nop
   nop
   nop
   nop
   nop
rs232_9600_bit_2
   decfsz _np_rs232_t,1
   goto rs232_9600_bit_bucle
   nop
   nop
   nop
   nop
   nop
   nop
   nop
   nop
   nop
   return
rs232_9600_mbit
   movlw .4
   movwf _np_rs232_t
   goto rs232_9600_mbit_2
rs232_9600_mbit_bucle
   nop
   nop
   nop
   nop
   nop
   nop
   nop
rs232_9600_mbit_2
   decfsz _np_rs232_t,1
   goto rs232_9600_mbit_bucle
   nop
   nop
   nop
   nop
   nop
   nop
   nop
   nop
   nop
   return


;------------------------------------------------------------
;                  DATOS EN MEMORIA EEPROM
;------------------------------------------------------------
   org  0x2100
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff
   data   0xff

 End