
;*************************
; clock 4 Mhz
; legge t-on metodo sottrazione
; esegue s = t * 344 / 2
; conv.bin-bcd
; invia al display misura della distanza
; invia impulso 20us circa RB0 ed azzera timer1
; riceve correttamente eco su rb3
; IT9DPX - FRANCO 5 GEN 2013
;*************************

   PROCESSOR       16F628
   RADIX           DEC
   INCLUDE         "P16F628.INC"     
    ERRORLEVEL      -302
   ERRORLEVEL      -305

    __CONFIG        3F0AH

#define     RS    PORTA,1
#define     EN    PORTA,0
;RB4-5-6-7 OUT LCD

        
        ORG     20H
RITARDO  RES     2
TEMPH    RES     1
TEMPL    RES     1
REGTEMP  RES     1
HBYTE    RES     1
MBYTE    RES     1
LBYTE    RES     1
SALVW    RES     1
SALVT    RES     1
CONV     RES     1
COUNT    RES     1    
TEMP     RES     1          
R0       RES     1    
R1       RES     1  
R2       RES     1    
R3       RES     1
DDRAM    RES     1
MNDO     RES     2
PTTO     RES     2
MORE     RES     2
BITC     RES     1
FLAG     RES     1

        ORG     00H
        GOTO    VIA

        ORG     04
; al sopraggiungere di un fronte di
; salita o di discesa su rb3 il valore 
; di tmr1l e tmr1h sarà catturato in ccpr1l
; ccpr1h. Sarà quindi azzerato tmr1l e
; tmr1h ed elaborato il valore ccpr1l
; e ccpr1h, trovando il perido "T".

INT
        MOVWF   SALVW
        MOVFW   STATUS
        MOVWF   SALVT

;...CATTURA...
CATTURA
  BTFSS   CCP1CON,0
  GOTO    LD_T1

LD_T2         ;fronte ascendente
  BCF     CCP1CON,0
  ; salvo i dati
    MOVFW   CCPR1L
    MOVWF   TEMPL
    MOVFW   CCPR1H
    MOVWF   TEMPH
  GOTO    SALTA

LD_T1         ;fronte discendente
  BSF     CCP1CON,0
  ; eseguo sottrazione coi precedenti
     MOVFW   TEMPL
     SUBWF   CCPR1L,W
     MOVWF   LBYTE        
     BTFSS   STATUS,C
     INCF    TEMPH

     MOVFW   TEMPH
     SUBWF   CCPR1H,W
     MOVWF   MBYTE
  ; fine sottrazione
  BSF     CONV,2 

SALTA
  BCF     PIR1,2
  BSF   INTCON,7
    MOVFW   SALVT
    MOVWF   STATUS
    MOVFW   SALVW
  RETFIE               

;-----------------------

VIA      
        BSF    STATUS,RP0
      ;commuta 2° banco
        BCF     OPTION_REG,7 ;pull-up
        MOVLW   8
        MOVWF   TRISB  ;rb3 in
        MOVLW   0
        MOVWF   TRISA
        MOVLW  B'00000100'
        MOVWF  PIE1
    ;   abilita int. CCP1IE
   
        BCF    STATUS,RP0
      ;commuta 1° banco
        MOVLW  B'00000001' 
        MOVWF  T1CON
     ;tmr1 on
        MOVLW  B'00000101' 
        MOVWF  CCP1CON
     ;mode rising
        MOVLW  0007
        MOVWF  CMCON
     ;spegne compar. porte I/O on
        MOVLW   0X80
        MOVWF   DDRAM       
        CLRF    CONV
        call    INIZIAL    ;inizializa LCD        
        MOVLW  B'11000000'
        MOVWF  INTCON
      ;abilita interrupt

    GOTO IMPULSO
 
RECALL
        NOP 
        BTFSS   CONV,2
        GOTO    RECALL
        CLRF    CONV
        BCF   INTCON,7

  CALL   MOLTIPLICA
  CALL   DIVISO2
; carica dati conversione
   MOVFW  PTTO+1
   MOVWF  HBYTE  
  MOVFW  MORE
  MOVWF  MBYTE
   MOVFW  MORE+1
   MOVWF  LBYTE

    CALL  BINBCD
    CALL  VIS_DISP
       ; da calcolare in modo da avere dopo tutte
       ;  le funzioni un impulso ogni >55 mS circa.
       movlw   .30   ;pausa 40 ms
       call    msDelay


IMPULSO
;---- impulso per il sensore ----
       BSF   PORTB,0
       movlw   .6     ;a 4 mhz e con .6        
       MOVWF   RITARDO ; impulso di 20 us circa
DECREM2
      DECFSZ  RITARDO
      GOTO    DECREM2
       BCF   PORTB,0
;---- fine impulso -----
;--- azzero timer 1L ed 1H -----
    CLRF  TMR1L
    CLRF  TMR1H
  BSF   INTCON,7
  GOTO   RECALL
;------------------
VIS_DISP
     MOVLW   H'81' ;DD RAM 2° CIFRA
     CALL    TXCMD
;  SWAPF  R0,W
;  ANDLW  0X0F
;  CALL   TXNUM
  MOVFW  R0
  ANDLW  0X0F
    BTFSS   STATUS,Z
    GOTO    NOZERO
    MOVLW   " "
    CALL    TXDATO   
    GOTO    SIZERO
NOZERO    
  CALL   TXNUM
SIZERO
    SWAPF  R1,W
    ANDLW  0X0F
    CALL   TXNUM
    MOVFW  R1
    ANDLW  0X0F
    CALL   TXNUM

 MOVLW  ","
 CALL   TXDATO

  SWAPF  R2,W
  ANDLW  0X0F
  CALL   TXNUM

 MOVLW  " "
 CALL   TXDATO
 MOVLW  "c"
 CALL   TXDATO
 MOVLW  "m"
 CALL   TXDATO

  RETURN     
;........................        
BINBCD

INIZ
       ; CLRF    HBYTE          
        MOVLW   24
        MOVWF   COUNT
CTRL7                   ; ignora zeri 
        BTFSC   HBYTE,7
        GOTO    CTRL7S
        RLF     LBYTE
        RLF     MBYTE
        RLF     HBYTE
        DECFSZ  COUNT
        GOTO    CTRL7
        RETLW   0
CTRL7S
        BCF     STATUS,0
        
        CLRF    R3
        CLRF    R2
        CLRF    R1
        CLRF    R0
LOOP16
        RLF     LBYTE
        RLF     MBYTE
        RLF     HBYTE
        RLF     R3
        RLF     R2
        RLF     R1
        RLF     R0
        DECFSZ  COUNT
        GOTO    ADJDEC
        RETLW   0
ADJDEC
        MOVLW   R3
        MOVWF   FSR
        CALL    ADJBCD
        MOVLW   R2
        MOVWF   FSR
        CALL    ADJBCD
        MOVLW   R1
        MOVWF   FSR
        CALL    ADJBCD
        MOVLW   R0
        MOVWF   FSR
        CALL    ADJBCD
        GOTO    LOOP16
ADJBCD
        MOVLW   3
        ADDWF   0,W
        MOVWF   TEMP
        BTFSC   TEMP,3
        MOVWF   0
        MOVLW   30H
        ADDWF   0,W
        MOVWF   TEMP
        BTFSC   TEMP,7
        MOVWF   0
        RETLW   0

;............................
INIZIAL
       movlw   .30    ;Wait 30 ms 
       call    msDelay
       MOVLW   30H   ;Set LCD command mode
       MOVWF   PORTB ;Send a reset sequence to LCD

       bsf     EN  ;Enables LCD
       movlw   .5          ;Wait 5 ms
       call    msDelay
       
       bcf     EN  ;Disables LCD
       movlw   .1          ;Wait 1ms
       call    msDelay

       CALL    CARICA
       CALL    CARICA
       MOVLW   20H       ;Set LCD command mode
       MOVWF   PORTB
       
       CALL    CARICA
       movlw   28H  ;Set 4 bit TX, 2 RIGHE
       call    TXCMD
       movlw   06H  ;Entry mode set, increment, no shift
       call    TXCMD

       movlw   0FH  ;Display ON, Curson ON, Blink ON
       call    TXCMD           
 
       movlw   01H ;Clear display
       call    TXCMD        
       movlw   .2   ;Wait 2 ms
       call    msDelay 

;............................
 
       MOVLW   H'C8' ;DD RAM 18° CIFRA
       CALL    TXCMD

       MOVLW   "I"
       call    TXDATO
       MOVLW   "T"
       call    TXDATO 
       MOVLW   "9"
       call    TXDATO  
       MOVLW   "D"
       call    TXDATO 
       MOVLW   "P"
       call    TXDATO  
       MOVLW   "X"
       call    TXDATO 

       CALL    PAUSA
       return

;..............................
TXNUM
   ADDLW   30H
TXDATO
          bsf     RS
          call    TXBYTE
          return
TXCMD
          bcf     RS
          call    TXBYTE
          return
TXBYTE 
         movwf   REGTEMP ;Save value to send
         MOVLW   H'0F'    ;tx 4 bits alti
         ANDWF   PORTB,F
         MOVFW   REGTEMP
         ANDLW   H'F0'
         IORWF   PORTB,F          
             
         CALL    CARICA
    
         MOVLW   H'0F'     ;tx 4 bits bassi
         ANDWF   PORTB,F
         SWAPF   REGTEMP,W
         ANDLW   H'F0'
         IORWF   PORTB,F                       
CARICA
         bsf     EN  ;Enables LCD
         movlw   .1         ;Wait 1ms
         call    msDelay
         bcf     EN  ;Disables LCD
         movlw   .1         ;Wait 1ms
         call    msDelay

         return
;------------------------
msDelay
          movwf   RITARDO+1
          clrf    RITARDO+0
RITLOOP
          nop
          decfsz  RITARDO+0,F
          goto    RITLOOP
          nop
          decfsz  RITARDO+1,F
          goto    RITLOOP
          return
;-----------------------------
PAUSA
       movlw   .250    ;Wait 250 ms
       call    msDelay  
       movlw   .250    ;Wait 250 ms
       call    msDelay   
    RETURN  
;-----------------------------
MOLTIPLICA
; dati moltiplicatore 158h = 344d
   MOVLW   01H
   MOVWF   MORE
   MOVLW   58H
   MOVWF   MORE+1
; dati moltiplicando
   MOVFW   MBYTE
   MOVWF   MNDO
   MOVFW   LBYTE
   MOVWF   MNDO+1
; azzero prodotto
   CLRF   PTTO
   CLRF   PTTO+1
; numero di bit
   MOVLW  .16+1
   MOVWF  BITC

   GOTO    ROTAZ
ANCORA
     BTFSS   STATUS,C
     GOTO    ROTAZ
   MOVFW  MNDO+1
   ADDWF  PTTO+1

   BTFSS  STATUS,C
   GOTO   NO_C
   INCF   PTTO
     BTFSC  STATUS,Z ;doppio carry
     BSF    FLAG,7   ;doppio carry

NO_C
   MOVFW  MNDO
   ADDWF  PTTO
     BTFSC  FLAG,7   ;doppio carry
     BSF    STATUS,C ;doppio carry
     BCF    FLAG,7   ;doppio carry
ROTAZ
   RRF    PTTO
   RRF    PTTO+1
   RRF    MORE   ;diviene PTTO+2
   RRF    MORE+1 ;diviene PTTO+3
BITDEC
   DECFSZ  BITC
   GOTO    ANCORA
   RETURN

;----------------------------------
DIVISO2
  BCF  STATUS,C
   RRF  PTTO
   RRF  PTTO+1
   RRF  MORE
   RRF  MORE+1
  RETURN

  END



