CHƯƠNG TRÌNH ĐIỀU KHIỂN LCD DÙNG VI ĐIỀU KHIỂN 89S52:

Một phần của tài liệu Giao tiếp ứng dụng trong vi xử lý (Trang 44 - 47)

V. VI ĐIỀU KHIỂN GIAO TIẾP VỚI LCD:

6.CHƯƠNG TRÌNH ĐIỀU KHIỂN LCD DÙNG VI ĐIỀU KHIỂN 89S52:

Chương trình sau sẽ điều khiển LCD hiển thị 2 hàng với nội dung được cho ở cuối chương trình:

Ví dụ 10:

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ;chuong trinh dieu khien LCD 16X2 tren kit vi dieu khien

;DUNG PORT 0 KET DOI VOI CAC DUONG DU LIEU CUA LCD P0-7 ->D0-7 ;DUNG 3 BIT CUA PORT2: P20,P21,P22 DIEU KHIEN E,R/W,RS

;tren man hinh LCD se hien thi noi dung moi

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

E BIT P2.0

rw BIT P2.1

rs BIT P2.2

ORG 0000H

mov 0a2h,#0

LCALL khtaolcd ;khoi tao lcd

LCALL first_line ;goi chtr con hien thi hang thu nhat LCALL scond_line ;goi chtr con hien thi hang thu hai SJMP $

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ;chtr con khoi tao LCD

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

khtaolcd: setb e ;Enable

clr rs ;RS low

clr rw ;RW low

MOV a,#38h ;tu dieu khien LCD

LCALL KTAO

LCALL ddelay41 ;delay 4.1 mSec MOV A,#38h ;function set

LCALL KTAO

LCALL ddelay100 ;delay MOV A,#38h ;function

LCALL KTAO

MOV A,#0ch ;tu dieu khien display on

LCALL KTAO

MOV A,#01h ;tu dieu khien Clear display

LCALL KTAO

MOV A,#06h ;tu dieu khien entry mode set

LCALL KTAO

MOV A,#80h ;thiet lap dia chi LCD (set DD RAM) (adsbygoogle = window.adsbygoogle || []).push({});

LCALL KTAO

MOV A,#0fh ;enable display cursor

LCALL KTAO

RET

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ;chuong trinh con delay 4.1 ms

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ddelay41: mov r6,#90h del412: mov r7,#200 djnz r7,$ djnz r6,del412 ret ;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ;chuong trinh con delay 255 microgiay

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

ddelay100: mov r7,#00

djnz r7,$

ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ;Feed command/data to the LCD module

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx command_byte:

clr rs ;RS low for a command byte ljmp bdelay

data_byte: setb rs ;RS high for a data byte

bdelay: clr rw ;R/W low for a write mode

clr e nop

setb e ;Enable pulse

nop nop

mov byteout,#0ffh ;configure port1 to input mode

setb rw ;set RW to read

clr rs ;set RS to command

clr e ;generate enable pulse nop nop setb e lcall ddelay100 ret ;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ;Chuong trinh con hien thi noi dung hang thu 1 tren LCD

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx first_line: MOV A,#080h ;set DDRAM

LCALL KTAO

MOV DPTR,#FLINE_DATA

lcall Write

ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ;Chuong trinh con hien thi noi dung hang thu 2 tren LCD

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx scond_line: mov a,#0c0h ;set DDRAM

LCALL KTAO (adsbygoogle = window.adsbygoogle || []).push({});

mov dptr,#sline_data

lcall write

ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ;chuong trinh con khoi tao LCD

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

KTAO: mov byteout,a

lcall command_byte RET

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ;chtr con goi data hien thi ra LCD

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx write: MOV A,#0

MOVC A,@a+dptr CJNE A,#99h,Writea RET

Writea: mov byteout,a acall data_byte inc dptr SJMP Write ;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ; Data bytes ;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx FLINE_DATA: DB 'NGUYEN DINH PHU ',099h

SLINE_DATA: DB 'DAI HOC SPKT HCM',099h END

Bài tập 11: Hãy thiết kế mạch điều khiển 1 LCD giống như trên và 1 bàn phím ma trận 4×4

và viết chương trình sau cho khi nhấn phím số nào thì trên màn hình hiển thị số đó tại vị trí tận cùng bên trái, các số trước dịch sang trái.

Bài tập 12: Hãy thiết kế mạch điều khiển 1 LCD giống như trên và 1 bàn phím ma trận 4×4

và viết chương trình đồng hồ hiển thị giờ phút giây, ngày tháng năm.

Một phần của tài liệu Giao tiếp ứng dụng trong vi xử lý (Trang 44 - 47)