mbed-os
Fork of mbed-os by
features/unsupported/tests/mbed/freopen/TextLCD.cpp@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed TextLCD Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2007-2009 sford |
elessair | 0:f269e3021894 | 3 | * Released under the MIT License: http://mbed.org/license/mit |
elessair | 0:f269e3021894 | 4 | */ |
elessair | 0:f269e3021894 | 5 | |
elessair | 0:f269e3021894 | 6 | #include "TextLCD.h" |
elessair | 0:f269e3021894 | 7 | #include "mbed.h" |
elessair | 0:f269e3021894 | 8 | |
elessair | 0:f269e3021894 | 9 | /* |
elessair | 0:f269e3021894 | 10 | * useful info found at http://www.a-netz.de/lcd.en.php |
elessair | 0:f269e3021894 | 11 | * |
elessair | 0:f269e3021894 | 12 | * Initialisation |
elessair | 0:f269e3021894 | 13 | * ============== |
elessair | 0:f269e3021894 | 14 | * |
elessair | 0:f269e3021894 | 15 | * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state |
elessair | 0:f269e3021894 | 16 | * |
elessair | 0:f269e3021894 | 17 | * - wait approximately 15 ms so the display is ready to execute commands |
elessair | 0:f269e3021894 | 18 | * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now). |
elessair | 0:f269e3021894 | 19 | * - The display is in 8 bit mode, so if you have only connected 4 data pins you should only transmit the higher nibble of each command. |
elessair | 0:f269e3021894 | 20 | * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now. |
elessair | 0:f269e3021894 | 21 | * - Execute the "clear display" command |
elessair | 0:f269e3021894 | 22 | * |
elessair | 0:f269e3021894 | 23 | * Timing |
elessair | 0:f269e3021894 | 24 | * ====== |
elessair | 0:f269e3021894 | 25 | * |
elessair | 0:f269e3021894 | 26 | * Nearly all commands transmitted to the display need 40us for execution. |
elessair | 0:f269e3021894 | 27 | * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position" |
elessair | 0:f269e3021894 | 28 | * These commands need 1.64ms for execution. These timings are valid for all displays working with an |
elessair | 0:f269e3021894 | 29 | * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you |
elessair | 0:f269e3021894 | 30 | * can use the busy flag to test if the display is ready to accept the next command. |
elessair | 0:f269e3021894 | 31 | * |
elessair | 0:f269e3021894 | 32 | * _e is kept high apart from calling clock |
elessair | 0:f269e3021894 | 33 | * _rw is kept 0 (write) apart from actions that uyse it differently |
elessair | 0:f269e3021894 | 34 | * _rs is set by the data/command writes |
elessair | 0:f269e3021894 | 35 | */ |
elessair | 0:f269e3021894 | 36 | |
elessair | 0:f269e3021894 | 37 | TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, |
elessair | 0:f269e3021894 | 38 | PinName d2, PinName d3, const char *name) : TextDisplay(name), _rw(rw), _rs(rs), |
elessair | 0:f269e3021894 | 39 | _e(e), _d(d0, d1, d2, d3) { |
elessair | 0:f269e3021894 | 40 | |
elessair | 0:f269e3021894 | 41 | _rw = 0; |
elessair | 0:f269e3021894 | 42 | _e = 1; |
elessair | 0:f269e3021894 | 43 | _rs = 0; // command mode |
elessair | 0:f269e3021894 | 44 | |
elessair | 0:f269e3021894 | 45 | // Should theoretically wait 15ms, but most things will be powered up pre-reset |
elessair | 0:f269e3021894 | 46 | // so i'll disable that for the minute. If implemented, could wait 15ms post reset |
elessair | 0:f269e3021894 | 47 | // instead |
elessair | 0:f269e3021894 | 48 | // wait(0.015); |
elessair | 0:f269e3021894 | 49 | |
elessair | 0:f269e3021894 | 50 | // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) |
elessair | 0:f269e3021894 | 51 | for(int i=0; i<3; i++) { |
elessair | 0:f269e3021894 | 52 | writeByte(0x3); |
elessair | 0:f269e3021894 | 53 | wait(0.00164); // this command takes 1.64ms, so wait for it |
elessair | 0:f269e3021894 | 54 | } |
elessair | 0:f269e3021894 | 55 | writeByte(0x2); // 4-bit mode |
elessair | 0:f269e3021894 | 56 | |
elessair | 0:f269e3021894 | 57 | writeCommand(0x28); // Function set 001 BW N F - - |
elessair | 0:f269e3021894 | 58 | writeCommand(0x0C); |
elessair | 0:f269e3021894 | 59 | writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes |
elessair | 0:f269e3021894 | 60 | cls(); |
elessair | 0:f269e3021894 | 61 | } |
elessair | 0:f269e3021894 | 62 | |
elessair | 0:f269e3021894 | 63 | void TextLCD::character(int column, int row, int c) { |
elessair | 0:f269e3021894 | 64 | int address = 0x80 + (row * 40) + column; // memory starts at 0x80, and is 40 chars long per row |
elessair | 0:f269e3021894 | 65 | writeCommand(address); |
elessair | 0:f269e3021894 | 66 | writeData(c); |
elessair | 0:f269e3021894 | 67 | } |
elessair | 0:f269e3021894 | 68 | |
elessair | 0:f269e3021894 | 69 | int TextLCD::columns() { |
elessair | 0:f269e3021894 | 70 | return 16; |
elessair | 0:f269e3021894 | 71 | } |
elessair | 0:f269e3021894 | 72 | |
elessair | 0:f269e3021894 | 73 | int TextLCD::rows() { |
elessair | 0:f269e3021894 | 74 | return 2; |
elessair | 0:f269e3021894 | 75 | } |
elessair | 0:f269e3021894 | 76 | |
elessair | 0:f269e3021894 | 77 | void TextLCD::writeByte(int value) { |
elessair | 0:f269e3021894 | 78 | _d = value >> 4; |
elessair | 0:f269e3021894 | 79 | wait(0.000040f); // most instructions take 40us |
elessair | 0:f269e3021894 | 80 | _e = 0; |
elessair | 0:f269e3021894 | 81 | wait(0.000040f); |
elessair | 0:f269e3021894 | 82 | _e = 1; |
elessair | 0:f269e3021894 | 83 | _d = value >> 0; |
elessair | 0:f269e3021894 | 84 | wait(0.000040f); |
elessair | 0:f269e3021894 | 85 | _e = 0; |
elessair | 0:f269e3021894 | 86 | wait(0.000040f); // most instructions take 40us |
elessair | 0:f269e3021894 | 87 | _e = 1; |
elessair | 0:f269e3021894 | 88 | } |
elessair | 0:f269e3021894 | 89 | |
elessair | 0:f269e3021894 | 90 | void TextLCD::writeCommand(int command) { |
elessair | 0:f269e3021894 | 91 | _rs = 0; |
elessair | 0:f269e3021894 | 92 | writeByte(command); |
elessair | 0:f269e3021894 | 93 | } |
elessair | 0:f269e3021894 | 94 | |
elessair | 0:f269e3021894 | 95 | void TextLCD::writeData(int data) { |
elessair | 0:f269e3021894 | 96 | _rs = 1; |
elessair | 0:f269e3021894 | 97 | writeByte(data); |
elessair | 0:f269e3021894 | 98 | } |