Author: Levent S. Translation: aweay
coding
Before you start coding, you need to know the basic HD44780 controller instruction. The following table is an introduction from the Hitachi website. For the sake of understanding, I have added some additional information.
INSTRUCTION CODE DESCRIPTION EXECUTION TIME ** R / W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 CLEAR Display 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 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). Thase Operations Are Performed Database. 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 DCB 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 think. Cgram is Character Generator Ram, This Ram Can Hold User Defined Graphic Characters. This Capability Gains The Modules Popular 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 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 85 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F C0 C1 C2 C3 C4 C5 C6 C8 CA CA CB CC CD CE CF
At this point, we already know enough information, now we can write code to make our LCD activities. In my program, there are three very important functions: prepare_lcd | move_to_specific | button_write_to_screen_click. The following code is a prepare_lcd function:
PRIVATE VOID PREPARE_LCD (int cursor_status)
{
/ * Look at the instruction Table to make these comments makesend * /
/ * Thread. Sleep () Function is not needed for some type of lcd instructions
* And also this is changeable from an LCD
* To Another So tryout the best for your module * /
/ * Sends 12 (d) = 1100 binary to open the entire display and
* Makes a Delay That LCD Needs for Execution * /
IF (Cursor_Status == 0)
Portaccess. Output (data, 12); // thread. Sleep (1);
// the delays can be smaller, Check Busy Flag Info in the article
/ * Sends 14 (d) = 1110 binary to open the entire display
* And Makes The Cursor Active and Also
* Makes a Delay That LCD Needs for Execution * /
IF (cursor_status == 1)
Portaccess. Output (Data, 14); // thread. Sleep (1);
// the delays can be smaller, Check Busy Flag Info in the article
/ * Sends 15 (d) = 1111 binary to open the entire display, makes
* The Cursor Active and Blink and Also
* Makes a Delay That LCD Needs for Execution * /
IF (cursor_status == 2)
Portaccess. Output (Data, 15); // thread. Sleep (1);
// the delays can be smaller, Check Busy Flag Info in the article
/ * Makes The Enable Pin High and Register Pin Low * /
Portaccess. Output (Control, 8); thread. Sleep (1);
// the delays can be smaller, Check Busy Flag Info in the article
/ * Makes The Enable Pin Low for LCD to Read ITS
* Data Pins and Also Register PIN LOW * /
Portaccess. Output (Control, 9); thread. Sleep (1);
// the delays can be smaller, Check Busy Flag Info in the article / * Clears Entire Display and Sets DDRAM Address
* 0 in Address Counter * /
Portaccess. Output (Data, 1); // Thread. Sleep (1);
// the delays can be smaller, Check Busy Flag Info in the article
/ * Makes The Enable Pin High and Register Pin Low * /
Portaccess. Output (Control, 8); thread. Sleep (1);
// the delays can be smaller, Check Busy Flag Info in the article
/ * Makes The Enable Pin Low for LCD to Read ITS
* Data Pins and Also Register PIN LOW * /
Portaccess. Output (Control, 9); thread. Sleep (1);
// the delays can be smaller, Check Busy Flag Info in the article
/ * We are setting the interface data length to 8 bits
* with selecting 2-line Display and 5 x 7-Dot Character Font.
* Lets Turn The Display on So We Have to send * /
Portaccess. Output (Data, 56); // Thread. Sleep (1);
// the delays can be smaller, Check Busy Flag Info in the article
/ * Makes The Enable Pin High and Register Pin Low * /
Portaccess. Output (Control, 8); thread. Sleep (1);
// the delays can be smaller, Check Busy Flag Info in the article
/ * Makes The Enable Pin Low for LCD to Read ITS
* Data Pins and Also Register PIN LOW * /
Portaccess. Output (Control, 9); thread. Sleep (1);
// the delays can be smaller, Check Busy Flag Info in the article
}
The MOVE_TO_SPECIFIC function can move the cursor to any location on the screen:
Private void move_to_specific (int Line, int column)
{
/ * Makes the RS PIN LOW * /
Portaccess. Output (Control, 8); // Thread. Sleep (1);
IF (line == 1)
{
/ * Sets ram address so what the cursor is posiented
* at a specific column of the 1st line. * /
Portaccess. Output (Data, 127 Column); // Thread. Sleep (1);
IF (line == 2)
{
/ * Sets Ram Address So That The Cursor
* Is postroled at a specific column of the 2nd line. * /
Portaccess. Output (DATA, 191 Column); // Thread. Sleep (1);
}
}
Look at the ASCII code and binary code conversion table below, you can understand the role of the button_write_to_screen_click function:
Now you can see how this function is implemented, I don't plan to put the code here because I don't want to make my article difficult to read.
Note: If the LCD execution is very fast, you can activate Thread. Sleep function.
Written in the last
Some people may ask "What is the benefit of my own writing program to control the LCD?" In fact, this is completely personal, but if we imagine, we use our own LCD to display the code from the Internet, how wonderful thing is .
About translator aweay
It is a student. He is currently in the largest research on the software design based on component technology. All 9CBS C Builder Plate Primary, you can pass Siney @ Yeah. Net contact him while welcoming you to visit his website http: // siney. Yeah. Net.