用C#操作IO端口2-控制液晶/熒光顯示器-2

作者: Levent S。  翻譯:Aweay<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

編碼

在你開始編碼前,你需要了解基本的HD44780控制器指令。下表是來自日立網站上的介紹,爲了好理解,我加入了一些額外的信息。

Instruction

Code

Description

Execution time**

RS

R/W

DB7

DB6

DB5

DB4

DB3

DB2

DB1

DB0

Clear display

0

0

0

0

0

0

0

0

0

1

Clears display and returns cursor to the home position (address 0)。

1。64mS

Cursor home

0

0

0

0

0

0

0

0

1

*

Returns cursor to home position (address 0)。 Also returns display being shifted to the original position。 DDRAM contents remains unchanged。

1。64mS

Entry mode set

0

0

0

0

0

0

0

1

I/D

S

Sets cursor move direction (I/D), specifies to shift the display (S)。 These operations are performed during data read/write。 I/D = 0 --> cursor is in decrement position。 I/D = 1 --> cursor is in increment position。 S = 0 --> Shift is invisible。 S = 1 --> Shift is visible

40uS

Display On/Off control

0

0

0

0

0

0

1

D

C

B

Sets On/Off of all display (D), cursor On/Off (C) and blink of cursor position character (B)。 D = 0 --> Display off。 D = 1 --> Displan on。 C = 0 --> Cursor off。 C = 1 --> Cursor on。 B = 0 --> Cursor blink off。 B = 1 --> Cursor blink on。

40uS

Cursor/display shift

0

0

0

0

0

1

S/C

R/L

*

*

Sets cursor-move or display-shift (S/C), shift direction (R/L)。 DDRAM contents remains unchanged。 S/C = 0 --> Move cursor。 S/C = 1 --> Shift display。 R/L = 0 --> Shift left。 R/L = 1 --> Shift right

40uS

Function set

0

0

0

0

1

DL

N

F

*

*

Sets interface data length (DL), number of display line (N) and character font(F)。 DL = 0 --> 4 bit interface。 DL = 1 --> 8 bit interface。 N = 0 --> 1/8 or 1/11 Duty (1 line)。 N = 1 --> 1/16 Duty (2 lines)。 F = 0 --> 5x7 dots。 F = 1 --> 5x10 dots。

40uS

Set CGRAM address

0

0

0

1

CGRAM address

Sets the CGRAM address。 CGRAM data is sent and received after this setting。

40uS

Set DDRAM address

0

0

1

DDRAM address

Sets the DDRAM address。 DDRAM data is sent and received after this setting。

40uS

Read busy-flag and address counter

0

1

BF

CGRAM / DDRAM address

Reads Busy-flag (BF) indicating internal operation is being performed and reads CGRAM or DDRAM address counter contents (depending on previous instruction)。 I used some delay functions in my code which are ThreadSleep if you don't want to use these you can check the Busy Flag and make your LCD speedy。 BF = 0 --> Can accept instruction。 BF = 1 --> Internal operation in progress no additional operation can be accepted。

0uS

Write to CGRAM or DDRAM

1

0

write data

Writes data to CGRAM or DDRAM。

40uS

Read from CGRAM or DDRAM

1

1

read data

Reads data from CGRAM or DDRAM。

40uS

* = Not important, Can be "1" or "0"
** = Execution Time is a time needed which the LCD needs for the operation。

CGRAM is character generator RAM, this ram can hold user defined graphic characters。 This capability gains these modules popularity because of this you can make bargraphs and your own language's special characters(Maybe Chinese, Korean, Turkish, Greek, etc。)。

DDRAM is the Display Data RAM for these modules which represents the hexadecimal Display data adresses。 See below:

Char。  

Line 1

Line 2

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

80

81

82

83

84

85

86

87

88

89

8A

8B

8C

8D

8E

8F

 

C0

C1

C2

C3

C4

C5

C6

C7

C8

C9

CA

CB

CC

CD

CE

CF

 

至此,我們已經知道了足夠的信息,現在我們可以編寫代碼讓我們的LCD活動起來了。在我的程序裏,有三個非常重要的函數:Prepare_LCD|move_to_specific|button_Write_to_Screen_Click。

下面的代碼是Prepare_LCD函數:

move_to_specific函數可以移動光標到屏幕上任何位置:

看下面的Ascii碼和二進制碼轉換表,你可以明白button_Write_to_screen_Click函數的作用:

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />CSDN_Dev_Image_2003-11-241655210.gif

現在你可以看看這個函數到底是如何實現的,我不打算把代碼放在這裏,因爲我不想讓我的這篇文章難於閱讀。

注意:如果LCD執行的時間非常快,你可以激活Thread。Sleep函數。

寫在最後

有人可能要問“我自己編寫程序控制LCD有什麼好處?”其實這完全是個人愛好,但如果想象一下,我們用自己編寫的LCD顯示從網絡上獲取的CodeProject的新聞,那是多麼美妙的事情。

關於譯者Aweay

是學生。他目前大四,對基於組件技術的軟件設計有深入研究。任CSDN C++Builder板塊版主,可以通過 siney@yeah。net 與他聯繫,同時歡迎你訪問他的網站 http://siney。yeah。net

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章