Stap 2: Hoe om af te drukken op een specifieke positie van de Cursor
Laten we doorgaan met de herziening van de hd44780 LCD bibliotheek codevoorbeelden. Gelieve te verwijzen naar de eerdere post voor links naar de broncode en een uitleg van wat we zijn bezig.
Nu zullen we kijken in het volgende codevoorbeeld:
CursorPosition.c
Opmerking: In de bovenstaande afbeelding zijn de regels die met '0' en '1 beginnen' lijn een.
De lijnen die met '2' en '3 beginnen' zijn lijn twee.
De display heb is een ERM2004-1. Het ondersteunt twee 40 karakter lijnen verdeeld over 4 20 teken lijnen op het beeldscherm.
Als u dit nog niet gedaan, verwijs naar de laatste post, voor de code repository link en download de broncode zodat u langs kunt volgen.
We zullen eerst weergeven het gehele codevoorbeeld en vervolgens bespreken de code door de sectie.
#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);}
De eerste sectie van de code wordt uitgelegd in de vorige sectie.
De functie hd44780_send_command() stelt de hd44780 interface om opdrachten te verzenden naar de IR, instructie register. Deze opdrachten zijn alleen de weergave voor het schrijven instellen.
Als u geïnteresseerd bent, gelieve te verwijzen naar de specificatie van de hd4780 voor details. Verwijs naar de vorige post voor een link naar de specificatie.
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);
De hd44780_setCursorPosition() functie roept een instructie, genaamd ' DDRAM ingesteld adres "
De argumenten doorgegeven de regel instellen en cursor positie zodat de volgende tijdgegevens wordt verzonden naar het display, dat gegevens aan de juiste locatie is geplaatst.
De hd44780_write_string()-functie neemt de doorgegeven tekenreeks en afdrukken van één teken tegelijk naar de eerste locatie die is opgegeven door de hd44780_setCursorPosition() functie. De AC adres teller werkt het magie, dus u niet hoeft te vrezen voor verhogen of verlagen de cursorpositie.