Thụng bỏo bằng văn bản trờn màn hỡnh LCD

Một phần của tài liệu bai giang VXL potx (Trang 156 - 161)

Vớ dụ này sử dụng loại LCD phổ biến nhất để hiển thị văn bản trong hai dũng với 16 ký tự mỗi dũng. Để tiết kiệm chõn IO của vi điều khiển, chỉ cú 4 chõn được sử dụng cho giao tiếp dữ liệụ Bằng cỏch này, mỗi byte được truyền đi theo hai bước: đầu tiờn là 4 bit cao sau đú là 4 bit thấp.

LCD cần phải được khởi tạo tại đầu chương trỡnh (trước khi sử dụng cỏc tớnh năng ghi đọc LCD). Bờn cạnh đú, cỏc phần của chương trỡnh lặp đi lặp lại trong chương trỡnh tạo ra một chương trỡnh con đặc biệt. Tất cả điều này cú vẻ rất phức tạp, nhưng toàn bộ chương trỡnh về cơ bản thực hiện một số hoạt động đơn giản và hiển thị dũng chữ “LCD

display”.

************************************************************************* ;* PROGRAM NAME : Lcd.ASM

;* DESCRIPRTION : Program for testing LCD displaỵ 4-bit communication ;* is used. Program does not check BUSY flag but uses program delay ;* between 2 commands. PORT1 is used for connection

;* to the microcontroller. ;************************************************************************ ;BASIC DIRECTIVES $MOD53 $TITLE(LCD.ASM) $PAGEWIDTH(132) $DEBUG $OBJECT $NOPAGING ;Stack DSEG AT 0E0h

Bộ mụn Kỹ thuật mỏy tớnh – Khoa Điện tử - Trường ĐH Kỹ thuật Cụng nghiệp 157 Stack_Start: DS 020h Start_ađress EQU 0000h ;Reset vectors CSEG AT 0 ORG Start_ađress JMP Inic ORG Start_ađress+100h

MOV IE,#00 ; All interrupts are disabled MOV SP,#Stack_Start

Inic: CALL LCD_inic ; Initialize LCD ;*************************************************

;* MAIN PROGRAM

;*************************************************

START: MOV A,#80h ; Next character will appear on the first

CALL LCD_status ; location in the first line

;of LCD displaỵ

MOV A,#' ' ; Display character ‘ ’.

CALL LCD_putc ; Call subroutine for

;character transmission.

MOV A,#' ' ; Display character ‘ ’.

CALL LCD_putc

MOV A,#' ' ; Display character ‘ ’.

CALL LCD_putc

MOV A,#' ' ; Display character ‘ ’.

CALL LCD_putc

MOV A,#' ' ; Display character ‘ ’.

CALL LCD_putc

MOV A,#' ' ; Display character ‘ ’.

CALL LCD_putc

MOV A,#'L' ; Display character ‘L’.

CALL LCD_putc

MOV A,#'C' ; Display character ‘C’.

CALL LCD_putc

MOV A,#'D' ; Display character ‘D’.

CALL LCD_putc

MOV A,#0c0h ; Next character will appear;

; on the first

CALL LCD_status ; location in the second line

;of LCD displaỵ

MOV A,#' ' ; Display character ‘ ’.

CALL LCD_putc ; Call subroutine for

;character transmission.

MOV A,#' ' ; Display character ‘ ’.

CALL LCD_putc

MOV A,#' ' ; Display character ‘ ’.

CALL LCD_putc

MOV A,#' ' ; Display character ‘ ’.

CALL LCD_putc

MOV A,#'D' ; Display character ‘D’.

CALL LCD_putc

MOV A,#'ớ ; Display character ‘i’.

CALL LCD_putc

Bộ mụn Kỹ thuật mỏy tớnh – Khoa Điện tử - Trường ĐH Kỹ thuật Cụng nghiệp 158

CALL LCD_putc

MOV A,#'p' ; Display character ‘p’.

CALL LCD_putc

MOV A,#'l' ; Display character ‘l’.

CALL LCD_putc

MOV A,#'ỏ ; Display character ‘a’.

CALL LCD_putc

MOV A,#'ý ; Display character ‘y’.

CALL LCD_putc

MOV R0,#20d ; Wait time (20x10ms)

CALL Delay_10ms

MOV DPTR,#LCD_DB ; Clear display

MOV A,#6d

CALL LCD_inic_status

MOV R0,#10d ; Wait time(10x10ms)

CALL Delay_10ms JMP START

;********************************************* ;* Subroutine for wait time (T= r0 x 10ms) ;*********************************************

Delay_10ms: MOV R5,00h ; 1+(1+(1+2*r7+2)*r6+2)*r5 approximately

MOV R6,#100d ; (if r7>10) MOV R7,#100d ; 2*r5*r6*r7

DJNZ R7,$ ; $ indicates current instruction.

DJNZ R6,$-4 DJNZ R5,$-6 RET

;************************************************************************* ;* SUBROUTINE: LCD_inic

;* DESCRIPTION: Subroutine for LCD initialization. ;*

;* (is used with 4-bit interface, under condition that pins DB4-7 on LCD ;* are connected to pins PX.4-7 on microcontroller’s ports,

; ịẹ four higher

;* bits on the port are used). ;*

;* NOTE: It is necessary to define port pins for controlling LCD operation: ;* LCD_enable, LCD_read_write,

; LCD_reg_select,similar to port for connection to LCD.

;* It is also necessary to define ađresses for the first character in each ;* linẹ

;************************************************************************* LCD_enable BIT P1.3 ; Bit for activating pin E on LCD.

LCD_read_write BIT P1.1 ; Bit for activating pin RW on LCD. LCD_reg_select BIT P1.2 ; Bit for activating pin RS on LCD. LCD_port SET P1 ; Port for connection to LCD.

Busy BIT P1.7 ; Port pin on which Busy flag appears.

LCD_Start_I_red EQU 00h ; Ađress of the first message character ; in the first line of LCD displaỵ LCD_Start_II_red EQU 40h ; Ađress of the first message character

; in the second line of LCD displaỵ

LCD_DB: DB 00111100b ; 0 -8b, 2/1 lines, 5x10/5x7 format

DB 00101100b ; 1 -4b, 2/1 lines, 5x10/5x7 format DB 00011000b ; 2 -Display/cursor shift, right/left

Bộ mụn Kỹ thuật mỏy tớnh – Khoa Điện tử - Trường ĐH Kỹ thuật Cụng nghiệp 159

DB 00001100b ; 3 -Display ON, cursor OFF, ;cursor blink off

DB 00000110b ; 4 -Increment mode, display shift off DB 00000010b ; 5 -Display/cursor home

DB 00000001b ; 6 -Clear display

DB 00001000b ; 7 -Display OFF, cursor OFF, ; cursor blink off

LCD_inic:

;***************************************** MOV DPTR,#LCD_DB

MOV A,#00d ; Triple initialization in 8-bit

CALL LCD_inic_status_8 ; mode is performed at the beginning

MOV A,#00d ; (in case of slow increment of

CALL LCD_inic_status_8 ; power supply when the power supply is on MOV A,#00d

lcall LCD_inic_status_8

MOV A,#1d ; Change from 8-bit into

CALL LCD_inic_status_8 ; 4-bit mode MOV A,#1d

CALL LCD_inic_status

MOV A,#3d ; As from this point the program executes in ;4-bit mode CALL LCD_inic_status MOV A,#6d CALL LCD_inic_status MOV A,#4d CALL LCD_inic_status RET LCD_inic_status_8: ;****************************************** PUSH B MOVC A,@A+DPTR

CLR LCD_reg_select ; RS=0 - Write command CLR LCD_read_write ; R/W=0 - Write data on LCD

MOV B,LCD_port ; Lower 4 bits from LCD port are memorized ORL B,#11110000b

ORL A,#00001111b ANL A,B

MOV LCD_port,A ; Data is moved from A to LCD port SETB LCD_enable ; high-to-low transition signal

; is generated on the LCD's EN pin CLR LCD_enable

MOV B,#255d ; Time delay in case of improper reset

DJNZ B,$ ; during initialization DJNZ B,$ DJNZ B,$ POP B RET LCD_inic_status:

Bộ mụn Kỹ thuật mỏy tớnh – Khoa Điện tử - Trường ĐH Kỹ thuật Cụng nghiệp 160 ;************************************************************************* MOVC A,@A+DPTR CALL LCD_status RET ;************************************************************************* ;* SUBROUTINE: LCD_status

;* DESCRIPTION: Subroutine for defining LCD status.

;************************************************************************* LCD_status: PUSH B MOV B,#255d DJNZ B,$ DJNZ B,$ DJNZ B,$

CLR LCD_reg_select ; RS=O: Command is sent to LCD CALL LCD_port_out

SWAP A ; Nibles are swapped in accumulator

DJNZ B,$ DJNZ B,$ DJNZ B,$

CLR LCD_reg_select ; RS=0: Command is sent to LCD CALL LCD_port_out

POP B RET

;************************************************************************ ;* SUBROUTINE: LCD_putc

;* DESCRIPTION: Sending character to be displayed on LCD.

;************************************************************************ LCD_putc: PUSH B

MOV B,#255d DJNZ B,$

SETB LCD_reg_select ; RS=1: Character is sent to LCD CALL LCD_port_out

SWAP A ; Nibles are swapped in accumulator

DJNZ B,$

SETB LCD_reg_select ; RS=1: Character is sent to LCD CALL LCD_port_out

POP B RET

;************************************************************************* ;* SUBROUTINE: LCD_port_out

;* DESCRIPTION: Sending commands or characters on LCD display

;************************************************************************* LCD_port_out: PUSH ACC

PUSH B

MOV B,LCD_port ; Lower 4 bits of LCD port are memorized ORL B,#11110000b

ORL A,#00001111b ANL A,B

Bộ mụn Kỹ thuật mỏy tớnh – Khoa Điện tử - Trường ĐH Kỹ thuật Cụng nghiệp 161

SETB LCD_enable ; high-to-low transition signal ; is generated on the LCD's EN pin CLR LCD_enable

POP B POP ACC RET

END ; End of program

Một phần của tài liệu bai giang VXL potx (Trang 156 - 161)

Tải bản đầy đủ (PDF)

(169 trang)