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