单片机汇编语言模块化编程-LCD1602驱动

;===================================
; Function     : LCD1602 drives
; Programer : MicroAC
; Date            : 2012.03.06
;===================================
name LCD1602
;You should know that A-register as
;transper if you gonna delier data.

;MCU'SEPCIAL REGISTER

;ACC      DATA    0E0H
;B            DATA    0F0H
;SP         DATA    81H
;DPL      DATA    82H
;DPH      DATA    83H

;DEFINE CONTROL PIN HERE & DATA PORT
RS   BIT P2.4
RW  BIT P2.5
E      BIT P2.6

DAT EQU P0


;SOME CONTROLER FLAG OR DAT

CHECK BIT 20H.0    ;Bit rang you can search.
BUSY  EQU 80H

;SOME PARATERMER DEFINE


DEL_TIM EQU 100    ;SETTING TIME YOU WANT DELAY.

;***********************************
;--FUNCTION             :  MAIN
;--IN PARAMETER     : NONE
;--OUTPARAMETER  : NONE
;--PROTEST                : NONE
;***********************************
MAIN SEGMENT CODE
START:
     ....add your instruction here!
     AJMP START

;***********************************
;--FUNCTION      :  InitLCD
;--IN PARAMETER  : CHECK , A
;--OUTPARAMETER  : NONE
;--PROTEST   : NONE
;***********************************
InitLCD PROC NEAR
    MOV  R1,#3                                          ;CIRCLE 3 TIMES
CIRCLE:
 MOV  DAT,#0                                            ;COMMAND 0X38,NO CHECK BF
 MOV  A,#38H
 CLR  CHECK
 CALL WriteCom                                       ;WRITE COMMAND
 LCALL DELAY    ;DELAY
 DJNZ  R1,CIRCLE
;COMMAND 0X38,CHECK BF & SETTING BEGINNING
 MOV   A,#38H                                            ;NO DELAY
 SETB  CHECK
 CALL WriteCom                                       ;WRITE COMMAND
;COMMAND 0X08,CHECK BF & SETTING BEGINNING
 MOV   A,#08H                                            ;NO DELAY
 SETB  CHECK
 CALL WriteCom                                       ;WRITE COMMAND
;COMMAND 0X01,CHECK BF & SETTING BEGINNING
 MOV   A,#01H                                           ;NO DELAY
 SETB  CHECK
 CALL WriteCom                                      ;WRITE COMMAND
;COMMAND 0X06,CHECK BF & SETTING BEGINNING
 MOV   A,#06H                                           ;NO DELAY
 SETB  CHECK
 CALL WriteCom                                      ;WRITE COMMAND
;COMMAND 0X0C,CHECK BF & SETTING BEGINNING
 MOV   A,#0CH                                          ;NO DELAY
 SETB  CHECK
 CALL WriteCom                                      ;WRITE COMMAND
 CALL DELAY                                            ;DELAY
 RET
InitLCD ENDP
;***********************************
;--FUNCTION              :  WriteDat
;--IN PARAMETER      : CHECK , A
;--OUTPARAMETER   : NONE
;--PROTEST                 : NONE
;*********************************** 
WriteDat PROC NEAR

 JNB CHECK,NOCHE                              ;NEEDN'T CHECKE BUSY-BF


 CLR CHECK                                             ;CLR CHECK FLAG FOR NEXT CHECK.


 CALL ReadStatus


NOCHE:
 
    MOV DAT,A                                             ;DAT<-(A),COMMAND RESTORE AT A-REGISTER
    SETB  RS
    CLR   RW
    SETB  E
    CLR   E
    SETB  E
     RET
WriteDat ENDP
;***********************************
;--FUNCTION              :  WriteCom
;--IN PARAMETER      : CHECK,A
;--OUTPARAMETER   : NONE
;--PROTEST                 : NONE
;***********************************
WriteCom PROC NEAR

 JNB CHECK,NOCHE                                    ;NEEDN'T CHECKE BUSY-BF


 CALL ReadStatus
NOCHE:
 
    MOV DAT,A                                                  ;DAT<-(A),COMMAND RESTORE AT A-REGISTER
   CLR   RS
   CLR   RW
   SETB  E
   CLR   E
   SETB  E

    RET
WriteCom ENDP
;***********************************
;--FUNCTION             :  ReadStatus
;--IN PARAMETER     : NONE
;--OUTPARAMETER  : NONE
;--PROTEST                : NONE
;***********************************
ReadStatus PROC NEAR
   
    MOV  DAT,#0FFH                                     ;PIN READ MODULE
    CLR  RS
    SETB RW
    SETB E
    CLR  E
BLOOP:                                                           ;GET BUSY BIT -BF
     MOV  A,DAT
    ANL  A,#BUSY  
    JBC  ACC.7 ,BLOOP
    SETB E
    RET
ReadStatus ENDP
;***********************************
;--FUNCTION              :  DispOneChar
;--IN PARAMETER      : A
;--OUTPARAMETER   : NONE
;--PROTEST                 : A
;***********************************
DispOneChar  PROC NEAR
    PUSH ACC                                    ;NEED TO PROTECT DISPLAY DATA.
    CLR  A                                            ;CLEAR  A
    ORL  A , #(80H+ 4)                       ;CHANG INTO INSTRUCTION(SET ADDRESS)
    SETB CHECK                               ;AND DISPLAY ONE LINE WITH CHECK BF
    CALL WriteCom
    POP  ACC                                       ;DISPLAY DATA OUT.
    SETB CHECK
    CALL WriteDat
    CALL DELAY
       RET
DispOneChar ENDP
;***********************************
;--FUNCTION             : DEALY
;--IN PARAMETER     : DEL_TIM
;--OUTPARAMETER  : NONE
;--PROTEST               : PSW
;***********************************
DEALY PROC FAR
    PUSH PSW
    SETB RS0                                      ;USING WORK REGISTER 3
    SETB RS1
    MOV R7,#DEL_TIM
    MOV R6,#200
LOOP:
    DJNZ R6,$
    DJNZ R7,LOOP
    POP PSW
    RETS                                              ;FAR PROCEDURE RETURN
DEALY ENDP
 END

你可能感兴趣的:(单片机,汇编语言)