Étape 2: Comment imprimer à un emplacement précis du curseur
Continuons d’examiner les exemples de code de bibliothèque LCD hd44780. Veuillez vous reporter à l’article précédent pour des liens vers le code source et une explication de ce que nous y travaillons.
Nous allons maintenant examiner l’exemple de code suivant :
CursorPosition.c
Remarque : Dans l’image ci-dessus, les lignes qui commencent par « 0 » et « 1 » sont ligne.
Les lignes qui commencent par « 2 » et « 3 » sont deux.
L’écran que j’ai est un ERM2004-1. Il prend en charge deux 40 lignes de caractère réparti sur 20 4 lignes de caractères à l’écran.
Si vous n'avez pas fait, se référer au dernier post, pour le lien référentiel de code et télécharger le code source, donc vous pouvez suivre le long.
Nous tout d’abord afficher l’exemple de code complet et nous discuterons ensuite le code section par section.
#include #include "hd44780_4bit_lib.h"/* * This code example uses the hd44780_setCursorPosition() functioh * to set the starting cursor position using row, col information * This cursor info is then written to the DDRAM address. * Using this method it is very easy to postion text on the dissplay */void main() { WDTCTL = WDTPW | WDTHOLD; // Disable watchdog timer // according to spec give the display 10ms to come up to voltage __delay_cycles(10000 * CPUSPEED); // set up data length, number of lines and font // notice we have setup the display for 2 lines and // using the 5 x 8 character size hd44780_init(N_2LINE, FONT_8); // if needed set up the cursor // hd44780_send_command(HD44780_CMD_DISPLAY_ON_CURSOR); // hd44780_send_command(HD44780_CMD_DISPLAY_ON_CURSOR_BLINK); hd44780_send_command(HD44780_CMD_RETURN_HOME); hd44780_send_command(HD44780_CMD_CLEAR_DISPLAY); // set Increment direction and cursor direction hd44780_send_command(HD44780_CMD_INCREMENT); hd44780_send_command(HD44780_CMD_MOVE_CURSOR_RIGHT); // hd44780_send_command(HD44780_CMD_RETURN_HOME); //hd44780_send_command(HD44780_CMD_CLEAR_DISPLAY); // The display I am testing with is a two line 40 character display // The display shows 20 characters per line so each line wraps twice // for what looks like a 4 line display. // On line one if you write past the 20th character the text // shows up on what appears // to be the third line. // Same goes for the second line, any character past the // 20th character is written // to what appears to be the fourth line. // hd44780_setCursorPosition(0, 0); hd44780_write_string("0123456789"); hd44780_setCursorPosition(0, 10); hd44780_write_string("0123456789"); hd44780_setCursorPosition(0, 20); hd44780_write_string("11234567890123456789"); hd44780_setCursorPosition(1, 0); hd44780_write_string("21234567890123456789"); hd44780_setCursorPosition(1, 20); hd44780_write_string("31234567890123456789"); hd44780_send_command(HD44780_CMD_RETURN_HOME);}
La première section du code est expliquée dans la section précédente.
La fonction hd44780_send_command() met en place l’interface hd44780 pour envoyer des commandes à l’IR, le registre d’instruction. Ces commandes sont juste mise en place de l’écran pour l’écriture.
Si vous êtes intéressé, veuillez vous référer à la spécification hd4780 pour plus de détails. Voir le post précédent pour un lien pour la spécification.
hd44780_setCursorPosition(0, 0); hd44780_write_string("0123456789"); hd44780_setCursorPosition(0, 10); hd44780_write_string("0123456789"); hd44780_setCursorPosition(0, 20); hd44780_write_string("11234567890123456789"); hd44780_setCursorPosition(1, 0); hd44780_write_string("21234567890123456789"); hd44780_setCursorPosition(1, 20); hd44780_write_string("31234567890123456789"); hd44780_send_command(HD44780_CMD_RETURN_HOME);
La fonction hd44780_setCursorPosition() appelle une Instruction, appelée « adresse DDRAM Set "
Les arguments passés ensemble vers le haut de la ligne et cursor position afin que la prochaine fois données est envoyé à l’écran, que les données soit positionnées à l’emplacement correct.
La fonction hd44780_write_string() prend la chaîne passée et imprime un caractère à la fois à l’emplacement de départ spécifié par la fonction hd44780_setCursorPosition(). Le compteur d’adresse AC fonctionne il magie si vous n’avez pas à vous soucier d’incrémentation ou décrémentation de la position du curseur.