for personal use
TextLCD.cpp@0:eebebe91fd28, 2020-06-19 (annotated)
- Committer:
- freezeburger
- Date:
- Fri Jun 19 15:11:14 2020 +0000
- Revision:
- 0:eebebe91fd28
[mbed] converted /TextLCD/TextLCD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
freezeburger | 0:eebebe91fd28 | 1 | /* mbed TextLCD Library |
freezeburger | 0:eebebe91fd28 | 2 | * Copyright (c) 2007-2009 sford |
freezeburger | 0:eebebe91fd28 | 3 | * Released under the MIT License: http://mbed.org/license/mit |
freezeburger | 0:eebebe91fd28 | 4 | */ |
freezeburger | 0:eebebe91fd28 | 5 | |
freezeburger | 0:eebebe91fd28 | 6 | #include "TextLCD.h" |
freezeburger | 0:eebebe91fd28 | 7 | |
freezeburger | 0:eebebe91fd28 | 8 | #include "mbed.h" |
freezeburger | 0:eebebe91fd28 | 9 | #include "error.h" |
freezeburger | 0:eebebe91fd28 | 10 | |
freezeburger | 0:eebebe91fd28 | 11 | using namespace mbed; |
freezeburger | 0:eebebe91fd28 | 12 | |
freezeburger | 0:eebebe91fd28 | 13 | /* |
freezeburger | 0:eebebe91fd28 | 14 | * useful info found at http://www.a-netz.de/lcd.en.php |
freezeburger | 0:eebebe91fd28 | 15 | * |
freezeburger | 0:eebebe91fd28 | 16 | * |
freezeburger | 0:eebebe91fd28 | 17 | * Initialisation |
freezeburger | 0:eebebe91fd28 | 18 | * ============== |
freezeburger | 0:eebebe91fd28 | 19 | * |
freezeburger | 0:eebebe91fd28 | 20 | * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state |
freezeburger | 0:eebebe91fd28 | 21 | * |
freezeburger | 0:eebebe91fd28 | 22 | * - wait approximately 15 ms so the display is ready to execute commands |
freezeburger | 0:eebebe91fd28 | 23 | * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now). |
freezeburger | 0:eebebe91fd28 | 24 | * - 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. |
freezeburger | 0:eebebe91fd28 | 25 | * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now. |
freezeburger | 0:eebebe91fd28 | 26 | * - Execute the "clear display" command |
freezeburger | 0:eebebe91fd28 | 27 | * |
freezeburger | 0:eebebe91fd28 | 28 | * Timing |
freezeburger | 0:eebebe91fd28 | 29 | * ====== |
freezeburger | 0:eebebe91fd28 | 30 | * |
freezeburger | 0:eebebe91fd28 | 31 | * Nearly all commands transmitted to the display need 40us for execution. |
freezeburger | 0:eebebe91fd28 | 32 | * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position" |
freezeburger | 0:eebebe91fd28 | 33 | * These commands need 1.64ms for execution. These timings are valid for all displays working with an |
freezeburger | 0:eebebe91fd28 | 34 | * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you |
freezeburger | 0:eebebe91fd28 | 35 | * can use the busy flag to test if the display is ready to accept the next command. |
freezeburger | 0:eebebe91fd28 | 36 | * |
freezeburger | 0:eebebe91fd28 | 37 | * _e is kept high apart from calling clock |
freezeburger | 0:eebebe91fd28 | 38 | * _rw is kept 0 (write) apart from actions that uyse it differently |
freezeburger | 0:eebebe91fd28 | 39 | * _rs is set by the data/command writes |
freezeburger | 0:eebebe91fd28 | 40 | */ |
freezeburger | 0:eebebe91fd28 | 41 | |
freezeburger | 0:eebebe91fd28 | 42 | TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, |
freezeburger | 0:eebebe91fd28 | 43 | PinName d2, PinName d3, int columns, int rows) : _rw(rw), _rs(rs), |
freezeburger | 0:eebebe91fd28 | 44 | _e(e), _d(d0, d1, d2, d3), _columns(columns), _rows(rows) { |
freezeburger | 0:eebebe91fd28 | 45 | |
freezeburger | 0:eebebe91fd28 | 46 | // _rows = 2; |
freezeburger | 0:eebebe91fd28 | 47 | // _columns = 16; |
freezeburger | 0:eebebe91fd28 | 48 | // Mon, 27 Apr 2009 23:32:34 +0200 |
freezeburger | 0:eebebe91fd28 | 49 | // Kevin Konradt: |
freezeburger | 0:eebebe91fd28 | 50 | // When using a LCD with 1 row x 16 characters |
freezeburger | 0:eebebe91fd28 | 51 | // instead of 2x16, try changing _columns to 8. |
freezeburger | 0:eebebe91fd28 | 52 | // (display seems to split the 16 characters into |
freezeburger | 0:eebebe91fd28 | 53 | // 2 virtual rows with 8 characters each.) |
freezeburger | 0:eebebe91fd28 | 54 | |
freezeburger | 0:eebebe91fd28 | 55 | _rw = 0; |
freezeburger | 0:eebebe91fd28 | 56 | _e = 1; |
freezeburger | 0:eebebe91fd28 | 57 | _rs = 0; // command mode |
freezeburger | 0:eebebe91fd28 | 58 | |
freezeburger | 0:eebebe91fd28 | 59 | // Should theoretically wait 15ms, but most things will be powered up pre-reset |
freezeburger | 0:eebebe91fd28 | 60 | // so i'll disable that for the minute. If implemented, could wait 15ms post reset |
freezeburger | 0:eebebe91fd28 | 61 | // instead |
freezeburger | 0:eebebe91fd28 | 62 | // wait(0.015); |
freezeburger | 0:eebebe91fd28 | 63 | |
freezeburger | 0:eebebe91fd28 | 64 | // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) |
freezeburger | 0:eebebe91fd28 | 65 | for(int i=0; i<3; i++) { |
freezeburger | 0:eebebe91fd28 | 66 | writeNibble(0x3); |
freezeburger | 0:eebebe91fd28 | 67 | wait(0.00164); // this command takes 1.64ms, so wait for it |
freezeburger | 0:eebebe91fd28 | 68 | } |
freezeburger | 0:eebebe91fd28 | 69 | writeNibble(0x2); // 4-bit mode |
freezeburger | 0:eebebe91fd28 | 70 | |
freezeburger | 0:eebebe91fd28 | 71 | writeCommand(0x28); // Function set 001 BW N F - - |
freezeburger | 0:eebebe91fd28 | 72 | writeCommand(0x0C); |
freezeburger | 0:eebebe91fd28 | 73 | writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes |
freezeburger | 0:eebebe91fd28 | 74 | |
freezeburger | 0:eebebe91fd28 | 75 | cls(); |
freezeburger | 0:eebebe91fd28 | 76 | } |
freezeburger | 0:eebebe91fd28 | 77 | |
freezeburger | 0:eebebe91fd28 | 78 | int TextLCD::_putc(int value) { |
freezeburger | 0:eebebe91fd28 | 79 | if(value == '\n') { |
freezeburger | 0:eebebe91fd28 | 80 | newline(); |
freezeburger | 0:eebebe91fd28 | 81 | } else { |
freezeburger | 0:eebebe91fd28 | 82 | writeData(value); |
freezeburger | 0:eebebe91fd28 | 83 | } |
freezeburger | 0:eebebe91fd28 | 84 | return value; |
freezeburger | 0:eebebe91fd28 | 85 | } |
freezeburger | 0:eebebe91fd28 | 86 | |
freezeburger | 0:eebebe91fd28 | 87 | int TextLCD::_getc() { |
freezeburger | 0:eebebe91fd28 | 88 | return 0; |
freezeburger | 0:eebebe91fd28 | 89 | } |
freezeburger | 0:eebebe91fd28 | 90 | |
freezeburger | 0:eebebe91fd28 | 91 | void TextLCD::newline() { |
freezeburger | 0:eebebe91fd28 | 92 | _column = 0; |
freezeburger | 0:eebebe91fd28 | 93 | _row++; |
freezeburger | 0:eebebe91fd28 | 94 | if(_row >= _rows) { |
freezeburger | 0:eebebe91fd28 | 95 | _row = 0; |
freezeburger | 0:eebebe91fd28 | 96 | } |
freezeburger | 0:eebebe91fd28 | 97 | locate(_column, _row); |
freezeburger | 0:eebebe91fd28 | 98 | } |
freezeburger | 0:eebebe91fd28 | 99 | |
freezeburger | 0:eebebe91fd28 | 100 | void TextLCD::locate(int column, int row) { |
freezeburger | 0:eebebe91fd28 | 101 | if(column < 0 || column >= _columns || row < 0 || row >= _rows) { |
freezeburger | 0:eebebe91fd28 | 102 | error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows); |
freezeburger | 0:eebebe91fd28 | 103 | return; |
freezeburger | 0:eebebe91fd28 | 104 | } |
freezeburger | 0:eebebe91fd28 | 105 | |
freezeburger | 0:eebebe91fd28 | 106 | _row = row; |
freezeburger | 0:eebebe91fd28 | 107 | _column = column; |
freezeburger | 0:eebebe91fd28 | 108 | int address = 0x80 + (_row * 40) + _column; // memory starts at 0x80, and is 40 chars long per row |
freezeburger | 0:eebebe91fd28 | 109 | writeCommand(address); |
freezeburger | 0:eebebe91fd28 | 110 | } |
freezeburger | 0:eebebe91fd28 | 111 | |
freezeburger | 0:eebebe91fd28 | 112 | void TextLCD::cls() { |
freezeburger | 0:eebebe91fd28 | 113 | writeCommand(0x01); // Clear Display |
freezeburger | 0:eebebe91fd28 | 114 | wait(0.00164f); // This command takes 1.64 ms |
freezeburger | 0:eebebe91fd28 | 115 | locate(0, 0); |
freezeburger | 0:eebebe91fd28 | 116 | } |
freezeburger | 0:eebebe91fd28 | 117 | |
freezeburger | 0:eebebe91fd28 | 118 | void TextLCD::reset() { |
freezeburger | 0:eebebe91fd28 | 119 | cls(); |
freezeburger | 0:eebebe91fd28 | 120 | } |
freezeburger | 0:eebebe91fd28 | 121 | |
freezeburger | 0:eebebe91fd28 | 122 | void TextLCD::clock() { |
freezeburger | 0:eebebe91fd28 | 123 | wait(0.000040f); |
freezeburger | 0:eebebe91fd28 | 124 | _e = 0; |
freezeburger | 0:eebebe91fd28 | 125 | wait(0.000040f); // most instructions take 40us |
freezeburger | 0:eebebe91fd28 | 126 | _e = 1; |
freezeburger | 0:eebebe91fd28 | 127 | } |
freezeburger | 0:eebebe91fd28 | 128 | |
freezeburger | 0:eebebe91fd28 | 129 | void TextLCD::writeNibble(int value) { |
freezeburger | 0:eebebe91fd28 | 130 | _d = value; |
freezeburger | 0:eebebe91fd28 | 131 | clock(); |
freezeburger | 0:eebebe91fd28 | 132 | } |
freezeburger | 0:eebebe91fd28 | 133 | |
freezeburger | 0:eebebe91fd28 | 134 | void TextLCD::writeByte(int value) { |
freezeburger | 0:eebebe91fd28 | 135 | writeNibble(value >> 4); |
freezeburger | 0:eebebe91fd28 | 136 | writeNibble(value >> 0); |
freezeburger | 0:eebebe91fd28 | 137 | } |
freezeburger | 0:eebebe91fd28 | 138 | |
freezeburger | 0:eebebe91fd28 | 139 | void TextLCD::writeCommand(int command) { |
freezeburger | 0:eebebe91fd28 | 140 | _rs = 0; |
freezeburger | 0:eebebe91fd28 | 141 | writeByte(command); |
freezeburger | 0:eebebe91fd28 | 142 | } |
freezeburger | 0:eebebe91fd28 | 143 | |
freezeburger | 0:eebebe91fd28 | 144 | void TextLCD::writeData(int data) { |
freezeburger | 0:eebebe91fd28 | 145 | _rs = 1; |
freezeburger | 0:eebebe91fd28 | 146 | writeByte(data); |
freezeburger | 0:eebebe91fd28 | 147 | _column++; |
freezeburger | 0:eebebe91fd28 | 148 | if(_column >= _columns) { |
freezeburger | 0:eebebe91fd28 | 149 | newline(); |
freezeburger | 0:eebebe91fd28 | 150 | } |
freezeburger | 0:eebebe91fd28 | 151 | } |