Improved version of Simon Ford's TextDisplay library, with addressing and timing fixes. Supports up to 20x4 text displays.
TextLCD.cpp@0:2c5bba968d7c, 2010-02-14 (annotated)
- Committer:
- bikeNomad
- Date:
- Sun Feb 14 00:28:08 2010 +0000
- Revision:
- 0:2c5bba968d7c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bikeNomad | 0:2c5bba968d7c | 1 | /* mbed TextLCD Library |
bikeNomad | 0:2c5bba968d7c | 2 | * Copyright (c) 2007-2009 sford |
bikeNomad | 0:2c5bba968d7c | 3 | * Released under the MIT License: http://mbed.org/license/mit |
bikeNomad | 0:2c5bba968d7c | 4 | * |
bikeNomad | 0:2c5bba968d7c | 5 | * Modified by Ned Konz to provide better support for 4-line LCDs and ones with other controller chips. |
bikeNomad | 0:2c5bba968d7c | 6 | */ |
bikeNomad | 0:2c5bba968d7c | 7 | |
bikeNomad | 0:2c5bba968d7c | 8 | #include "TextLCD.h" |
bikeNomad | 0:2c5bba968d7c | 9 | #include "mbed.h" |
bikeNomad | 0:2c5bba968d7c | 10 | |
bikeNomad | 0:2c5bba968d7c | 11 | /* |
bikeNomad | 0:2c5bba968d7c | 12 | * useful info found at http://www.a-netz.de/lcd.en.php |
bikeNomad | 0:2c5bba968d7c | 13 | * |
bikeNomad | 0:2c5bba968d7c | 14 | * Initialisation |
bikeNomad | 0:2c5bba968d7c | 15 | * ============== |
bikeNomad | 0:2c5bba968d7c | 16 | * |
bikeNomad | 0:2c5bba968d7c | 17 | * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state |
bikeNomad | 0:2c5bba968d7c | 18 | * |
bikeNomad | 0:2c5bba968d7c | 19 | * - wait approximately 15 ms so the display is ready to execute commands |
bikeNomad | 0:2c5bba968d7c | 20 | * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now). |
bikeNomad | 0:2c5bba968d7c | 21 | * - 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. |
bikeNomad | 0:2c5bba968d7c | 22 | * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now. |
bikeNomad | 0:2c5bba968d7c | 23 | * - Execute the "clear display" command |
bikeNomad | 0:2c5bba968d7c | 24 | * |
bikeNomad | 0:2c5bba968d7c | 25 | * Timing |
bikeNomad | 0:2c5bba968d7c | 26 | * ====== |
bikeNomad | 0:2c5bba968d7c | 27 | * |
bikeNomad | 0:2c5bba968d7c | 28 | * Nearly all commands transmitted to the display need 40us for execution. |
bikeNomad | 0:2c5bba968d7c | 29 | * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position" |
bikeNomad | 0:2c5bba968d7c | 30 | * These commands need 1.64ms for execution. These timings are valid for all displays working with an |
bikeNomad | 0:2c5bba968d7c | 31 | * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you |
bikeNomad | 0:2c5bba968d7c | 32 | * can use the busy flag to test if the display is ready to accept the next command. |
bikeNomad | 0:2c5bba968d7c | 33 | * |
bikeNomad | 0:2c5bba968d7c | 34 | * _e is kept low except when being used. |
bikeNomad | 0:2c5bba968d7c | 35 | * _rw is kept 0 (write) apart from actions that use it differently |
bikeNomad | 0:2c5bba968d7c | 36 | * _rs is set by the data/command writes |
bikeNomad | 0:2c5bba968d7c | 37 | */ |
bikeNomad | 0:2c5bba968d7c | 38 | |
bikeNomad | 0:2c5bba968d7c | 39 | TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, |
bikeNomad | 0:2c5bba968d7c | 40 | PinName d2, PinName d3, uint16_t rows, uint16_t cols) : _rw(rw), _rs(rs), |
bikeNomad | 0:2c5bba968d7c | 41 | _e(e), _d(d0, d1, d2, d3), _rows(rows), _columns(cols) { |
bikeNomad | 0:2c5bba968d7c | 42 | |
bikeNomad | 0:2c5bba968d7c | 43 | _rw = 0; |
bikeNomad | 0:2c5bba968d7c | 44 | wait_us(1); // min. 100nsec delay |
bikeNomad | 0:2c5bba968d7c | 45 | _e = 0; |
bikeNomad | 0:2c5bba968d7c | 46 | _rs = 0; // command mode |
bikeNomad | 0:2c5bba968d7c | 47 | _d.output(); |
bikeNomad | 0:2c5bba968d7c | 48 | |
bikeNomad | 0:2c5bba968d7c | 49 | reset(); |
bikeNomad | 0:2c5bba968d7c | 50 | cls(); |
bikeNomad | 0:2c5bba968d7c | 51 | } |
bikeNomad | 0:2c5bba968d7c | 52 | |
bikeNomad | 0:2c5bba968d7c | 53 | void TextLCD::reset() { |
bikeNomad | 0:2c5bba968d7c | 54 | wait_ms(15); |
bikeNomad | 0:2c5bba968d7c | 55 | // e is low at this point, as is rw. |
bikeNomad | 0:2c5bba968d7c | 56 | // 2. Send 0x3 and wait 150 ms (will stay in 8-bit mode if already there) |
bikeNomad | 0:2c5bba968d7c | 57 | writeHalfByte(0x3); |
bikeNomad | 0:2c5bba968d7c | 58 | wait_ms(5); |
bikeNomad | 0:2c5bba968d7c | 59 | // 3. Send 0x3 and wait 150 ms (will go to 8-bit mode if was in 4-bit without any garbage nibble) |
bikeNomad | 0:2c5bba968d7c | 60 | writeHalfByte(0x3); |
bikeNomad | 0:2c5bba968d7c | 61 | wait_ms(5); |
bikeNomad | 0:2c5bba968d7c | 62 | // 4. Send 0x3 and wait 250 ms (will go to 8-bit mode even if garbage nibble was previously received) |
bikeNomad | 0:2c5bba968d7c | 63 | writeHalfByte(0x3); |
bikeNomad | 0:2c5bba968d7c | 64 | wait_ms(5); |
bikeNomad | 0:2c5bba968d7c | 65 | // 5. Send 0x2 and wait 200 ms (should go to 4-bit mode now) |
bikeNomad | 0:2c5bba968d7c | 66 | writeHalfByte(0x2); |
bikeNomad | 0:2c5bba968d7c | 67 | wait_ms(5); |
bikeNomad | 0:2c5bba968d7c | 68 | // 7. Send LCD setup sequence (eg 0x2, 0x8 (=0x28), 0x0, 0x8 (=0x08), etc.) |
bikeNomad | 0:2c5bba968d7c | 69 | writeCommand(0x28); // Function set 001 BW N F - - |
bikeNomad | 0:2c5bba968d7c | 70 | wait_ms(15); |
bikeNomad | 0:2c5bba968d7c | 71 | |
bikeNomad | 0:2c5bba968d7c | 72 | writeCommand(0x08); // display off, cursor invisible |
bikeNomad | 0:2c5bba968d7c | 73 | wait_ms(15); |
bikeNomad | 0:2c5bba968d7c | 74 | |
bikeNomad | 0:2c5bba968d7c | 75 | writeCommand(0x01); |
bikeNomad | 0:2c5bba968d7c | 76 | wait_ms(15); // 1.64ms command |
bikeNomad | 0:2c5bba968d7c | 77 | |
bikeNomad | 0:2c5bba968d7c | 78 | writeCommand(0x0C); // display enabled, cursor invisible |
bikeNomad | 0:2c5bba968d7c | 79 | wait_ms(15); |
bikeNomad | 0:2c5bba968d7c | 80 | |
bikeNomad | 0:2c5bba968d7c | 81 | writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes |
bikeNomad | 0:2c5bba968d7c | 82 | wait_ms(15); |
bikeNomad | 0:2c5bba968d7c | 83 | |
bikeNomad | 0:2c5bba968d7c | 84 | locate(0,0); |
bikeNomad | 0:2c5bba968d7c | 85 | } |
bikeNomad | 0:2c5bba968d7c | 86 | |
bikeNomad | 0:2c5bba968d7c | 87 | // memory starts at 0x80, and is 0x40 chars long per row |
bikeNomad | 0:2c5bba968d7c | 88 | // However, rows 2 and 3 of 4-line displays are actually adjacent to rows 0 and 1. |
bikeNomad | 0:2c5bba968d7c | 89 | // 16x4 displays are addressed the same way as 20x4 ones. |
bikeNomad | 0:2c5bba968d7c | 90 | |
bikeNomad | 0:2c5bba968d7c | 91 | void TextLCD::character(uint16_t column, uint16_t row, int c) { |
bikeNomad | 0:2c5bba968d7c | 92 | int address; |
bikeNomad | 0:2c5bba968d7c | 93 | address = 0x80 + ((row & ~2) * 0x40) + column; |
bikeNomad | 0:2c5bba968d7c | 94 | if (row > 1) |
bikeNomad | 0:2c5bba968d7c | 95 | address += 20; |
bikeNomad | 0:2c5bba968d7c | 96 | writeCommand(address); |
bikeNomad | 0:2c5bba968d7c | 97 | writeData(c); |
bikeNomad | 0:2c5bba968d7c | 98 | } |
bikeNomad | 0:2c5bba968d7c | 99 | |
bikeNomad | 0:2c5bba968d7c | 100 | void TextLCD::writeHalfByte(uint16_t value) { |
bikeNomad | 0:2c5bba968d7c | 101 | _e = 1; |
bikeNomad | 0:2c5bba968d7c | 102 | wait_us(1); |
bikeNomad | 0:2c5bba968d7c | 103 | _d = value & 0x0F; // send data on bus |
bikeNomad | 0:2c5bba968d7c | 104 | wait_us(1); // setup time |
bikeNomad | 0:2c5bba968d7c | 105 | _e = 0; // strobe |
bikeNomad | 0:2c5bba968d7c | 106 | wait_us(1); // hold time |
bikeNomad | 0:2c5bba968d7c | 107 | } |
bikeNomad | 0:2c5bba968d7c | 108 | |
bikeNomad | 0:2c5bba968d7c | 109 | void TextLCD::writeByte(uint16_t value) { |
bikeNomad | 0:2c5bba968d7c | 110 | writeHalfByte(value>>4); |
bikeNomad | 0:2c5bba968d7c | 111 | writeHalfByte(value); |
bikeNomad | 0:2c5bba968d7c | 112 | } |
bikeNomad | 0:2c5bba968d7c | 113 | |
bikeNomad | 0:2c5bba968d7c | 114 | void TextLCD::writeCommand(uint16_t command) { |
bikeNomad | 0:2c5bba968d7c | 115 | _rs = 0; |
bikeNomad | 0:2c5bba968d7c | 116 | writeByte(command); |
bikeNomad | 0:2c5bba968d7c | 117 | waitUntilDone(); |
bikeNomad | 0:2c5bba968d7c | 118 | } |
bikeNomad | 0:2c5bba968d7c | 119 | |
bikeNomad | 0:2c5bba968d7c | 120 | void TextLCD::writeData(uint16_t data) { |
bikeNomad | 0:2c5bba968d7c | 121 | _rs = 1; |
bikeNomad | 0:2c5bba968d7c | 122 | writeByte(data); |
bikeNomad | 0:2c5bba968d7c | 123 | waitUntilDone(); |
bikeNomad | 0:2c5bba968d7c | 124 | } |
bikeNomad | 0:2c5bba968d7c | 125 | |
bikeNomad | 0:2c5bba968d7c | 126 | void TextLCD::cls() { |
bikeNomad | 0:2c5bba968d7c | 127 | writeCommand(0x01); |
bikeNomad | 0:2c5bba968d7c | 128 | wait_us(2000); // 1.64ms command |
bikeNomad | 0:2c5bba968d7c | 129 | locate(0,0); |
bikeNomad | 0:2c5bba968d7c | 130 | } |
bikeNomad | 0:2c5bba968d7c | 131 | |
bikeNomad | 0:2c5bba968d7c | 132 | // This should be changed to use readAddressAndBusy() when that works. |
bikeNomad | 0:2c5bba968d7c | 133 | void TextLCD::waitUntilDone() { |
bikeNomad | 0:2c5bba968d7c | 134 | wait_us(60); |
bikeNomad | 0:2c5bba968d7c | 135 | } |
bikeNomad | 0:2c5bba968d7c | 136 | |
bikeNomad | 0:2c5bba968d7c | 137 | // Return the busy/address byte. |
bikeNomad | 0:2c5bba968d7c | 138 | // The busy flag is the high bit. |
bikeNomad | 0:2c5bba968d7c | 139 | // Not yet working reliably. |
bikeNomad | 0:2c5bba968d7c | 140 | uint16_t TextLCD::readAddressAndBusy() { |
bikeNomad | 0:2c5bba968d7c | 141 | _d.input(); |
bikeNomad | 0:2c5bba968d7c | 142 | _rw = 1; |
bikeNomad | 0:2c5bba968d7c | 143 | wait_us(1); |
bikeNomad | 0:2c5bba968d7c | 144 | _e = 1; |
bikeNomad | 0:2c5bba968d7c | 145 | wait_us(1); |
bikeNomad | 0:2c5bba968d7c | 146 | _e = 0; |
bikeNomad | 0:2c5bba968d7c | 147 | |
bikeNomad | 0:2c5bba968d7c | 148 | uint16_t retval = _d.read() << 4; |
bikeNomad | 0:2c5bba968d7c | 149 | |
bikeNomad | 0:2c5bba968d7c | 150 | wait_us(1); |
bikeNomad | 0:2c5bba968d7c | 151 | _e = 1; |
bikeNomad | 0:2c5bba968d7c | 152 | wait_us(1); |
bikeNomad | 0:2c5bba968d7c | 153 | _e = 0; |
bikeNomad | 0:2c5bba968d7c | 154 | |
bikeNomad | 0:2c5bba968d7c | 155 | retval |= _d.read(); |
bikeNomad | 0:2c5bba968d7c | 156 | _rw = 0; |
bikeNomad | 0:2c5bba968d7c | 157 | |
bikeNomad | 0:2c5bba968d7c | 158 | _d.output(); |
bikeNomad | 0:2c5bba968d7c | 159 | return retval; |
bikeNomad | 0:2c5bba968d7c | 160 | } |
bikeNomad | 0:2c5bba968d7c | 161 |