;******************************************************************************
;  DeclaraciĆ³ de constants del display
;******************************************************************************
;conexionat:
;       p1.4..p1.7 = D4..D7
;       p1.0 = RS
;       p1.2 = E
;       0 = R/W
LCD_INS SET 11111110B
LCD_DAT SET 00000001B
LCD_ENA SET 00000100B

LCD_LINEA2 SET 0C0H
LCD_LINEA1 SET 080H                     
LCD_CLRSCR SET 001H                     ;RETARDO DE 1.64 mS

;*******************************************************************************************
INICIALITZA_LCD:;***************************************************************************
;*******************************************************************************************

;0      X000 0000B = 00H                ->              IOPORT1
;1      X000 0001B = 01H
;15     X000 1111B = 0FH
  

  PUSH WSR
  LDB WSR,#00h
  
  CALL DELAY_10mS
  CALL DELAY_10mS

  LDB IOPORT1,#30H AND LCD_INS                  
  CALL WRITE_LCD_4BITS
  CALL DELAY_10mS

  LDB IOPORT1,#30H AND LCD_INS
  CALL WRITE_LCD_4BITS
  CALL DELAY_10mS

  LDB IOPORT1,#30H AND LCD_INS
  CALL WRITE_LCD_4BITS  
  CALL DELAY_10mS

  LDB IOPORT1,#20H AND LCD_INS
  CALL WRITE_LCD_4BITS
  CALL DELAY_10mS

;AHORA YA PODEMOS USAR LA FUNCION DE INSTRUCION LCD
  LDB bAux,#28H                         ;FUNCTION SET- 4 bits dades, 2 lines, caracter font
  CALL INSTRUCCIO_LCD

  LDB bAux,#0FH                         ;DISPLAY ON/OFF-DISPLAY ON, CURSON ON, CURSOR BLINK
  CALL INSTRUCCIO_LCD

  LDB bAux,#06H                         ;ENTRY MODE SET-INCREMENT CURSOR, CURSOR MOVEMENT
  CALL INSTRUCCIO_LCD

  LDB bAux,#01H                         ;CLEAR DISPLAY-
  CALL INSTRUCCIO_LCD
  CALL DELAY_10mS                       ;ESTE RETARDO DEBERIA DE SER DE 1.64mS

  POP WSR
  RET

;*******************************************************************************************
WRITE_LCD_4BITS:;***************************************************************************
;*******************************************************************************************
;AQUESTA FUNCIO ES NOMES PER US INTERN DE LA LLIBRERIA DEL DISPLAY
  NOP
  NOP
  XORB IOPORT1,#LCD_ENA
  NOP
  NOP
  NOP
  NOP
  NOP
  NOP
  NOP
  NOP
  XORB IOPORT1,#LCD_ENA
  NOP
  RET

;*******************************************************************************************
INSTRUCCIO_LCD:;****************************************************************************
;*******************************************************************************************
  PUSH WSR
  LDB WSR,#00H
  LDB IOPORT1,#0F0H
  ANDB IOPORT1,bAux  
  ANDB IOPORT1,#LCD_INS                  ;RS=0
  CALL WRITE_LCD_4BITS

  SHL bAux,#4H
  NOP
  NOP
  NOP
  NOP
  NOP

  LDB IOPORT1,#0F0H
  ANDB IOPORT1,bAux
  ANDB IOPORT1,#LCD_INS
  CALL WRITE_LCD_4BITS  
  CALL DELAY_40uS
  POP WSR
  RET

;*******************************************************************************************
DADA_LCD:;**********************************************************************************
;*******************************************************************************************
  PUSH WSR
  LDB WSR,#00H
  LDB IOPORT1,#0F0H
  ANDB IOPORT1,bAux                     ;parte alta primero
  ORB IOPORT1,#LCD_DAT                  ;RS=1
  CALL WRITE_LCD_4BITS

  SHL bAux,#4H
  NOP
  NOP
  NOP
  NOP
  NOP

  LDB IOPORT1,#0F0H
  ANDB IOPORT1,bAux
  ORB IOPORT1,#LCD_DAT
  CALL WRITE_LCD_4BITS  
  CALL DELAY_40uS
  POP WSR
  RET



;*******************************************************************************************
CLRSCR_LCD:;********************************************************************************
;*******************************************************************************************
  LDB bAux,#LCD_CLRSCR
  CALL INSTRUCCIO_LCD
  CALL DELAY_10mS
  RET

;*******************************************************************************************
DELAY_10mS:;********************************************************************************
;*******************************************************************************************
  LD TIMER_LCD,#2000D
DELAY_10mS_NEXT:
  DEC TIMER_LCD
  CMP TIMER_LCD,0
  JNE DELAY_10mS_NEXT
  RET
  

;*******************************************************************************************
DELAY_40uS:;********************************************************************************
;*******************************************************************************************
  LD TIMER_LCD,#40D
DELAY_40uS_NEXT:
  DEC TIMER_LCD
  CMP TIMER_LCD,0
  JNE DELAY_10mS_NEXT  
  RET


;*******************************************************************************************
_GOTOXY MACRO X,Y;**************************************************************************
;*******************************************************************************************
;   1<=x<=16  ,  1<=Y<=2
   LDB bAux,#080H OR ((Y-1)*040H) OR (X-1)
   CALL INSTRUCCIO_LCD
   endm