;*****************************************
; acquisice dati colonne, righe e n° di colonne
; stampa bene tutti i caratteri max 9*9
; all'avvio ultimo tastierino da eeprom
; @: cancella display 
; 05 Dic. 2010
; IT9DPX - FRANCO
; provato-ok
;*****************************************

   PROCESSOR       16F88
   RADIX           DEC
   INCLUDE         "P16F88.INC" 

   ERRORLEVEL      -302
   ERRORLEVEL      -305

   
    __CONFIG  H'2007', H'3F41'  ; 
    __CONFIG  H'2008', H'3FFC'  ; IE switch off, fail-save off

           
    ORG   20H
CONT_V    RES 10
NU_COL    EQU 0X29
CONT_O    RES 10
BUFF     RES 1 
RIT1     RES 1
RIT2     RES 1
CONTA    RES 1
CONTB    RES 1
BUFFT    RES 1
CENT     RES 1
DECN     RES 1
BINAR    RES 1
ORIZ     RES 1
VERT     RES 1
ORIZ_VER RES 1
PRODTT   RES 1
MOLT     RES 1

;NU_RIG   RES 1
BUFFL    RES 1
FLAG     RES 1 ;BIT-0-1
BUFF_C   RES 1

#define   RS   PORTA,2
#define   CLC  PORTA,3

   ORG     0x2100 ;EEPROM
   DATA    34, 52, 71, 90, 108, 0, 0, 0, 0
   DATA    4 ;numero di colonne
   DATA    26, 39, 52, 65, 0, 0, 0, 0, 0, 0
   
   ORG     00H 

VIA      
   BSF    STATUS,RP0
 ;commuta 2° banco
   MOVLW  0007
   MOVWF  CMCON
 ;spegne compar. porte I/O on
   MOVLW   00000001B
   MOVWF    ANSEL
   ; coNv.a/d su gp0
   MOVLW   00000000B
   MOVWF    ADCON1
   ; Right justified. 
   BCF     OPTION_REG,7
 ;pull-up on
   MOVLW  B'00000000'
   MOVWF   TRISB
 ;rb3 in
   MOVLW  B'00010011'
   MOVWF   TRISA
   BCF    STATUS,RP0
 ;commuta 1° banco
   MOVLW   00000001B
   MOVWF    ADCON0
 ; ADC ON
   MOVLW  B'00000000'
   MOVWF  INTCON
 ;accende interrupt
   CLRF    PORTB
   CLRF    PORTA
   CALL    LEGMEM
   BCF     FLAG,1
   CALL    RESLCD

   ;GOTO  MOLTIPLICA ;prova
           
;---------------------
INIZIO
   CALL   LEGGO_ORIZ
   BTFSS  ORIZ,7
   CALL   CONTROLLO
   CALL    CLR_DISP
ANCORA
   CALL   LEGGO_ORIZ     
   BTFSC  ORIZ,7
   GOTO   ANCORA
   CALL   TROVA
;confronta il carattere attuale col precedente
   BTFSC  FLAG,0
   GOTO   ANCORA  
     XORWF  BUFFL,F
     BTFSS  STATUS,Z
     GOTO   RES_CONT
     MOVWF  BUFFL
;se identici increment "CONTA"    
   INCF   CONTA
   BTFSS  CONTA,7
   GOTO   ANCORA
;stampa se almeno 128 chr di seguito sono identici
    ADDLW   .63     
    CALL    TXCHR
       MOVLW   .1
       XORWF   BUFFL,W
       BTFSC   STATUS,Z
       CALL    CLR_DISP
    CLRF   CONTA
;attende che non vi sia nessuna pressione su touche
CTRL
   CALL   ASP_PRES    
   GOTO    ANCORA
;azzera "CONTA" salva nuovo chr e ripete daccapo
RES_CONT
   MOVWF  BUFFL
   CLRF   CONTA
   GOTO   ANCORA
;cancella display
CLR_DISP
   MOVLW  0X01 ;clr schermo
   CALL   TXCMD
   MOVLW  0X07 ;schermo a destra
   CALL   TXCMD
   MOVLW  0X8F ;1°riga 15°chr
   CALL   TXCMD
   RETURN
;------------
ASP_PRES
   CALL   LEGGO_ORIZ
   BTFSS  ORIZ,7
   GOTO   ASP_PRES
   RETURN
;------------------
;------------------
LEGGO_ORIZ
   BSF    STATUS,RP0
 ;commuta 2° banco    
   MOVLW  B'10000000'
   MOVWF   TRISB
 ;rb7-in rb6-out
   MOVLW  B'00010010'
   MOVWF   TRISA
 ;ra1-in  ra0-out
   MOVLW   00000010B
   MOVWF    ANSEL
 ;gp1 = analog.input
   BCF    STATUS,RP0
 ;commuta 1° banco
   MOVLW   00001001B
   MOVWF    ADCON0
 ;adc on - su ra1
     BCF   PORTB,6
     BSF   PORTA,0
     BTFSS  FLAG,1
     GOTO   NO_SET1 ;CTRL
   MOVLW  0X82 ;1°riga 2°chr
   CALL   TXCMD
NO_SET1
   CALL  AVVIA_ADC
   MOVWF  ORIZ
;----------------
LEGGO_VERT
   BSF    STATUS,RP0
 ;commuta 2° banco    
   MOVLW  B'01000000'
   MOVWF   TRISB
 ;rb7-out rb6-in
   MOVLW  B'00010001'
   MOVWF   TRISA
 ;ra1-out  ra0-in
   MOVLW   00000001B
   MOVWF    ANSEL
 ;gp0 =analog.input
   BCF    STATUS,RP0
 ;commuta 1° banco
   MOVLW   00000001B
   MOVWF    ADCON0
 ;adc on - su ra0
     BCF   PORTB,7
     BSF   PORTA,1
     BTFSS  FLAG,1 ;CTRL
     GOTO   NO_SET2
   MOVLW  0XC2 ;2°riga 2°chr
   CALL   TXCMD
NO_SET2
   CALL  AVVIA_ADC
   MOVWF  VERT
   RETURN
;-------------------
;-------------------
AVVIA_ADC
   BSF    ADCON0,2 ;avvia conv.
CE_DATO
   BTFSC  ADCON0,2
   GOTO   CE_DATO
   MOVFW  ADRESH
   MOVWF  BUFFT
     BTFSS  FLAG,1
     RETURN
;--conv. bin/dec ---
INIZIA1
    MOVWF  BINAR
    CLRF    CENT
CONV_BCD1
    INCF    CENT
    MOVLW   .100
    SUBWF   BINAR,F
    BTFSC   STATUS,C
    GOTO    CONV_BCD1
    MOVLW   .100
    ADDWF   BINAR,F
    DECF    CENT
INIZIA
    CLRF    DECN
CONV_BCD
    INCF    DECN
    MOVLW   .10
    SUBWF   BINAR,F
    BTFSC   STATUS,C
    GOTO    CONV_BCD
    MOVLW   .10
    ADDWF   BINAR,F
    DECF    DECN
;invia al display
      MOVFW  CENT
      CALL   TXNUM
   MOVFW  DECN
   CALL   TXNUM
      MOVFW  BINAR
      CALL   TXNUM
   MOVLW  " "     
   CALL   TXCHR
     MOVFW  BUFFT
     RETURN
;----------------------
;----------------------
TROVA
   BCF     FLAG,0
   MOVFW   VERT
   MOVWF   ORIZ_VER
   MOVLW   CONT_V
   MOVWF   FSR
   CALL    TROVA2
   MOVWF   VERT
      MOVFW   ORIZ
      MOVWF   ORIZ_VER
      MOVLW   CONT_O
      MOVWF   FSR
      CALL    TROVA2
      MOVWF   ORIZ
   BTFSC   FLAG,0
   RETURN
;----moltiplica-----
   MOVFW  NU_COL
   MOVWF  MOLT
   CLRF   PRODTT
CALC_CHR
   RRF    MOLT
   BTFSS  STATUS,C
   GOTO   NOADC
   MOVFW  VERT
   ADDWF  PRODTT
NOADC
   BCF    STATUS,C
   RLF    VERT
   MOVFW  MOLT
   BTFSS  STATUS,Z
   GOTO   CALC_CHR
;somma oriz
   MOVFW  PRODTT
   ADDWF  ORIZ,W
   ADDLW  .1
   RETURN
;..................
TROVA2
   CALL    CERCA_VAL
   BTFSS   STATUS,C
   RETLW   .0
   CALL    CERCA_VAL
   BTFSS   STATUS,C
   RETLW   .1
   CALL    CERCA_VAL
   BTFSS   STATUS,C
   RETLW   .2
   CALL    CERCA_VAL
   BTFSS   STATUS,C
   RETLW   .3
   CALL    CERCA_VAL
   BTFSS   STATUS,C
   RETLW   .4
   CALL    CERCA_VAL
   BTFSS   STATUS,C
   RETLW   .5
   CALL    CERCA_VAL
   BTFSS   STATUS,C
   RETLW   .6
   CALL    CERCA_VAL
   BTFSS   STATUS,C
   RETLW   .7
   CALL    CERCA_VAL
   BTFSS   STATUS,C
   RETLW   .8
   BSF     FLAG,0 ;no pressione
   RETURN
;--------------------
CERCA_VAL
   MOVFW   INDF
   INCF    FSR
   SUBWF   ORIZ_VER,W
   RETURN
;----------------------
;--trova verticali-----
CONTROLLO
   BSF    FLAG,1
   CLRF   CONTA
   CLRF   BUFF_C
   MOVLW  CONT_V
   MOVWF  FSR

CONTRL1
   CALL   LEGGO_ORIZ
   MOVLW  0XC6 ;2°riga 6°chr
   CALL   TXCMD
   INCF   CONTA
   MOVFW  CONTA
   ANDLW  .7
   CALL   TXNUM
     BTFSS  CONTA,6 ;64 chr
     GOTO   CONTRL1
   MOVFW   VERT
   SUBWF   BUFF_C,F
   BTFSC   STATUS,C
   GOTO    CONTROLLO_2
     CLRF   CONTA
     MOVWF  INDF
    MOVWF  BUFF_C
   INCF   FSR
;implementa bistabile
CONTRL2
   CALL   ASP_PRES
CONTRL21
   CALL   LEGGO_ORIZ
   BTFSC  ORIZ,7
   GOTO   CONTRL21
   GOTO   CONTRL1 
;trova orizzontali
CONTROLLO_2
   CLRF   CONTA
   CLRF   CONTB ;conta n° colonne
   CLRF   BUFF_C
   MOVLW  CONT_O
   MOVWF  FSR
CONTROLLO_21
   CALL   LEGGO_ORIZ
   MOVLW  0XC6 ;2°riga 6°chr
   CALL   TXCMD
   INCF   CONTA
   MOVFW  CONTA
   ANDLW  .7
   CALL   TXNUM
     BTFSS  CONTA,6 ;64 chr
     GOTO   CONTROLLO_21
   MOVFW   ORIZ
   SUBWF   BUFF_C,F
   BTFSC   STATUS,C
   GOTO    FINE_CONTROLLO
     CLRF   CONTA
     MOVWF  INDF
    MOVWF  BUFF_C
   INCF   FSR
   INCF   CONTB  ;conta n° colonne
;implementa bistabile
CONTRL3
   CALL   ASP_PRES
CONTRL31
   CALL   LEGGO_ORIZ
   BTFSC  ORIZ,7
   GOTO   CONTRL31
   GOTO   CONTROLLO_21
FINE_CONTROLLO
   BCF    FLAG,1
;---------------------
;---------------------
SCRMEM1
   MOVFW   CONTB
   MOVWF   NU_COL
   BCF     STATUS,RP0
   BSF     STATUS,RP1
   CLRF    EEADR
   BCF     STATUS,RP1
   MOVLW   0X20
   MOVWF   FSR
RIP_MEM   
   MOVFW   INDF
   CALL    SCR_MEM1
   BTFSC   FSR,6
   RETURN
   INCF    FSR
   GOTO    RIP_MEM
SCR_MEM1
   BSF     STATUS,RP1
   MOVWF  EEDATA
   BSF    STATUS,RP0
   BCF   EECON1, EEPGD   
   BSF    EECON1,WREN  
   MOVLW  55H
   MOVWF  EECON2
   MOVLW  0xAA
   MOVWF  EECON2
   BSF    EECON1,WR     
CTRLS1
   BTFSC  EECON1,WR
   GOTO   CTRLS1 
   BCF    STATUS,RP0
   INCF    EEADR
   BCF     STATUS,RP1
   RETURN
;------------------
;------------------
LEGMEM
   BCF     STATUS,RP0
   BSF     STATUS,RP1
   CLRF    EEADR
   MOVLW   0X20
   MOVWF   FSR
RIP_LEG
   CALL    LEG_MEM
   MOVWF   INDF
   BTFSC   FSR,6
   RETURN
   INCF    FSR
   GOTO    RIP_LEG 
LEG_MEM
   BSF     STATUS,RP1
   BSF     STATUS,RP0
   BCF   EECON1, EEPGD
   BSF     EECON1,RD	
   NOP			        
   BCF     STATUS,RP0
   MOVFW   EEDATA
   INCF    EEADR
   BCF    STATUS,RP1
   RETURN
;---------------------
;-----reset display.---
RESLCD
   MOVLW   0x20
   CALL    TXCMD
   MOVLW   0x20
   CALL    TXCMD
   MOVLW   0x28
   CALL    TXCMD
   MOVLW   0x0E
   CALL    TXCMD
   MOVLW   0x06
   CALL    TXCMD
   MOVLW   0x01
   CALL    TXCMD
   MOVLW   0x02
   CALL    TXCMD
   RETURN
;..tx 2 nibble lcd...
TX_2NIB
   BCF     CLC
   SWAPF   BUFF,W ;inv.byte
   ANDLW   0X0F ;tx 1° nibble
   MOVWF   PORTB
   NOP
   BSF     CLC
   NOP
   NOP  ;clock
   NOP
   BCF     CLC
   MOVF    BUFF,W
   ANDLW   0X0F ;tx 2° nibble
   MOVWF   PORTB
   NOP
   BSF     CLC
   NOP
   NOP  ;clock
   NOP
   BCF     CLC
   MOVLW   .3
   MOVWF   RIT1
DEL1
   DECFSZ  RIT2,F
   GOTO    DEL1
   DECFSZ  RIT1,F
   GOTO    DEL1
   RETURN
;----------------
RIGA1
   MOVLW   0x80
   GOTO    TXCMD
RIGA2
   MOVLW   0xC0
TXCMD
   BCF     RS
   GOTO    TX_DATO
TXNUM
   ADDLW   30H
TXCHR
   BSF     RS
TX_DATO
   MOVWF   BUFF
   GOTO    TX_2NIB
;--------------------
   END





