Extended TextLCD: uses Bus or Port output for added performance, with display/cursor control, with LCDType as a class, with character generator programming capability.
TextLCD.cpp@2:2773889d6143, 2011-02-05 (annotated)
- Committer:
- osmeest
- Date:
- Sat Feb 05 23:20:53 2011 +0000
- Revision:
- 2:2773889d6143
- Parent:
- 0:5dc369cc8778
Republish after name clash with testbed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
osmeest | 0:5dc369cc8778 | 1 | /* mbed TextLCD Library, for a 4-bit LCD based on HD44780 |
osmeest | 0:5dc369cc8778 | 2 | * Copyright (c) 2007-2010, sford, http://mbed.org |
osmeest | 0:5dc369cc8778 | 3 | * |
osmeest | 0:5dc369cc8778 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
osmeest | 0:5dc369cc8778 | 5 | * of this software and associated documentation files (the "Software"), to deal |
osmeest | 0:5dc369cc8778 | 6 | * in the Software without restriction, including without limitation the rights |
osmeest | 0:5dc369cc8778 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
osmeest | 0:5dc369cc8778 | 8 | * copies of the Software, and to permit persons to whom the Software is |
osmeest | 0:5dc369cc8778 | 9 | * furnished to do so, subject to the following conditions: |
osmeest | 0:5dc369cc8778 | 10 | * |
osmeest | 0:5dc369cc8778 | 11 | * The above copyright notice and this permission notice shall be included in |
osmeest | 0:5dc369cc8778 | 12 | * all copies or substantial portions of the Software. |
osmeest | 0:5dc369cc8778 | 13 | * |
osmeest | 0:5dc369cc8778 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
osmeest | 0:5dc369cc8778 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
osmeest | 0:5dc369cc8778 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
osmeest | 0:5dc369cc8778 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
osmeest | 0:5dc369cc8778 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
osmeest | 0:5dc369cc8778 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
osmeest | 0:5dc369cc8778 | 20 | * THE SOFTWARE. |
osmeest | 0:5dc369cc8778 | 21 | */ |
osmeest | 0:5dc369cc8778 | 22 | |
osmeest | 0:5dc369cc8778 | 23 | #include "ext_text_lcd/TextLCD.h" |
osmeest | 0:5dc369cc8778 | 24 | #include "mbed.h" |
osmeest | 0:5dc369cc8778 | 25 | |
osmeest | 0:5dc369cc8778 | 26 | namespace ext_text_lcd { |
osmeest | 0:5dc369cc8778 | 27 | |
osmeest | 0:5dc369cc8778 | 28 | namespace { |
osmeest | 0:5dc369cc8778 | 29 | int makeLcdAddress(const TextLCD::LCDType &type, int column, int row) { |
osmeest | 0:5dc369cc8778 | 30 | return column + row * 0x40; |
osmeest | 0:5dc369cc8778 | 31 | } |
osmeest | 0:5dc369cc8778 | 32 | |
osmeest | 0:5dc369cc8778 | 33 | int makeLcdAddress40(const TextLCD::LCDType &type, int column, int row) { |
osmeest | 0:5dc369cc8778 | 34 | return column + row * 40; |
osmeest | 0:5dc369cc8778 | 35 | } |
osmeest | 0:5dc369cc8778 | 36 | |
osmeest | 0:5dc369cc8778 | 37 | int makeLcdAddress4Rows(const TextLCD::LCDType &type, int column, int row) { |
osmeest | 0:5dc369cc8778 | 38 | return column + (row % 2) * 0x40 + (row / 2) * 20; |
osmeest | 0:5dc369cc8778 | 39 | } |
osmeest | 0:5dc369cc8778 | 40 | } |
osmeest | 0:5dc369cc8778 | 41 | |
osmeest | 0:5dc369cc8778 | 42 | const TextLCD::LCDType TextLCD::LCD16x2(16, 2, makeLcdAddress); |
osmeest | 0:5dc369cc8778 | 43 | const TextLCD::LCDType TextLCD::LCD16x2B(16, 2, makeLcdAddress40); |
osmeest | 0:5dc369cc8778 | 44 | const TextLCD::LCDType TextLCD::LCD20x2(20, 2, makeLcdAddress); |
osmeest | 0:5dc369cc8778 | 45 | const TextLCD::LCDType TextLCD::LCD20x4(20, 4, makeLcdAddress4Rows); |
osmeest | 0:5dc369cc8778 | 46 | |
osmeest | 0:5dc369cc8778 | 47 | TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5, |
osmeest | 0:5dc369cc8778 | 48 | PinName d6, PinName d7, const LCDType &type) : |
osmeest | 0:5dc369cc8778 | 49 | _rs(rs), _e(e), _output(new BusOutput(d4, d5, d6, d7)), |
osmeest | 0:5dc369cc8778 | 50 | _type(type) |
osmeest | 0:5dc369cc8778 | 51 | { |
osmeest | 0:5dc369cc8778 | 52 | init(); |
osmeest | 0:5dc369cc8778 | 53 | } |
osmeest | 0:5dc369cc8778 | 54 | |
osmeest | 0:5dc369cc8778 | 55 | TextLCD::TextLCD(PinName rs, PinName e, PortName dport, std::size_t dlsb, |
osmeest | 0:5dc369cc8778 | 56 | const LCDType &type) : |
osmeest | 0:5dc369cc8778 | 57 | _rs(rs), _e(e), _output(new PortOutput(dport, dlsb)), |
osmeest | 0:5dc369cc8778 | 58 | _type(type) |
osmeest | 0:5dc369cc8778 | 59 | { |
osmeest | 0:5dc369cc8778 | 60 | init(); |
osmeest | 0:5dc369cc8778 | 61 | } |
osmeest | 0:5dc369cc8778 | 62 | |
osmeest | 0:5dc369cc8778 | 63 | void TextLCD::init() { |
osmeest | 0:5dc369cc8778 | 64 | _e = 1; |
osmeest | 0:5dc369cc8778 | 65 | _rs = 0; // command mode |
osmeest | 0:5dc369cc8778 | 66 | |
osmeest | 0:5dc369cc8778 | 67 | wait(0.015); // Wait 15ms to ensure powered up |
osmeest | 0:5dc369cc8778 | 68 | |
osmeest | 0:5dc369cc8778 | 69 | // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) |
osmeest | 0:5dc369cc8778 | 70 | for (int i=0; i<3; i++) { |
osmeest | 0:5dc369cc8778 | 71 | writeByte(0x3); |
osmeest | 0:5dc369cc8778 | 72 | wait(0.00164); // this command takes 1.64ms, so wait for it |
osmeest | 0:5dc369cc8778 | 73 | } |
osmeest | 0:5dc369cc8778 | 74 | writeByte(0x2); // 4-bit mode |
osmeest | 0:5dc369cc8778 | 75 | wait(0.000040f); // most instructions take 40us |
osmeest | 0:5dc369cc8778 | 76 | |
osmeest | 0:5dc369cc8778 | 77 | writeCommand(0x28); // Function set 001 BW N F - - |
osmeest | 0:5dc369cc8778 | 78 | writeCommand(0x0C); |
osmeest | 0:5dc369cc8778 | 79 | writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes |
osmeest | 0:5dc369cc8778 | 80 | cls(); |
osmeest | 0:5dc369cc8778 | 81 | } |
osmeest | 0:5dc369cc8778 | 82 | |
osmeest | 0:5dc369cc8778 | 83 | void TextLCD::cls() { |
osmeest | 0:5dc369cc8778 | 84 | writeCommand(0x01); // cls, and set cursor to 0 |
osmeest | 0:5dc369cc8778 | 85 | wait(0.00164f); // This command takes 1.64 ms |
osmeest | 0:5dc369cc8778 | 86 | locate(0, 0); |
osmeest | 0:5dc369cc8778 | 87 | } |
osmeest | 0:5dc369cc8778 | 88 | |
osmeest | 0:5dc369cc8778 | 89 | void TextLCD::locate(int column, int row) { |
osmeest | 0:5dc369cc8778 | 90 | _column = column; |
osmeest | 0:5dc369cc8778 | 91 | _row = row; |
osmeest | 0:5dc369cc8778 | 92 | |
osmeest | 0:5dc369cc8778 | 93 | int a = address(column, row); |
osmeest | 0:5dc369cc8778 | 94 | writeCommand(0x80 | a); |
osmeest | 0:5dc369cc8778 | 95 | } |
osmeest | 0:5dc369cc8778 | 96 | |
osmeest | 0:5dc369cc8778 | 97 | int TextLCD::_putc(int value) { |
osmeest | 0:5dc369cc8778 | 98 | int column = _column; |
osmeest | 0:5dc369cc8778 | 99 | int row = _row; |
osmeest | 0:5dc369cc8778 | 100 | if (value == '\n') { |
osmeest | 0:5dc369cc8778 | 101 | column = 0; |
osmeest | 0:5dc369cc8778 | 102 | row = _row+1; |
osmeest | 0:5dc369cc8778 | 103 | } else { |
osmeest | 0:5dc369cc8778 | 104 | writeData(value); |
osmeest | 0:5dc369cc8778 | 105 | column++; |
osmeest | 0:5dc369cc8778 | 106 | if (column < columns()) { |
osmeest | 0:5dc369cc8778 | 107 | return value; |
osmeest | 0:5dc369cc8778 | 108 | } |
osmeest | 0:5dc369cc8778 | 109 | |
osmeest | 0:5dc369cc8778 | 110 | column = 0; |
osmeest | 0:5dc369cc8778 | 111 | row++; |
osmeest | 0:5dc369cc8778 | 112 | } |
osmeest | 0:5dc369cc8778 | 113 | if (row >= rows()) { |
osmeest | 0:5dc369cc8778 | 114 | row = 0; |
osmeest | 0:5dc369cc8778 | 115 | } |
osmeest | 0:5dc369cc8778 | 116 | locate(column, row); |
osmeest | 0:5dc369cc8778 | 117 | return value; |
osmeest | 0:5dc369cc8778 | 118 | } |
osmeest | 0:5dc369cc8778 | 119 | |
osmeest | 0:5dc369cc8778 | 120 | int TextLCD::_getc() { |
osmeest | 0:5dc369cc8778 | 121 | return -1; |
osmeest | 0:5dc369cc8778 | 122 | } |
osmeest | 0:5dc369cc8778 | 123 | |
osmeest | 0:5dc369cc8778 | 124 | void TextLCD::writeByte(int value) { |
osmeest | 0:5dc369cc8778 | 125 | _output->writeNibble(value >> 4); |
osmeest | 0:5dc369cc8778 | 126 | wait(0.000040f); // most instructions take 40us |
osmeest | 0:5dc369cc8778 | 127 | _e = 0; |
osmeest | 0:5dc369cc8778 | 128 | wait(0.000040f); |
osmeest | 0:5dc369cc8778 | 129 | _e = 1; |
osmeest | 0:5dc369cc8778 | 130 | _output->writeNibble(value); |
osmeest | 0:5dc369cc8778 | 131 | wait(0.000040f); |
osmeest | 0:5dc369cc8778 | 132 | _e = 0; |
osmeest | 0:5dc369cc8778 | 133 | wait(0.000040f); // most instructions take 40us |
osmeest | 0:5dc369cc8778 | 134 | _e = 1; |
osmeest | 0:5dc369cc8778 | 135 | } |
osmeest | 0:5dc369cc8778 | 136 | |
osmeest | 0:5dc369cc8778 | 137 | void TextLCD::writeCommand(int command) { |
osmeest | 0:5dc369cc8778 | 138 | _rs = 0; |
osmeest | 0:5dc369cc8778 | 139 | writeByte(command); |
osmeest | 0:5dc369cc8778 | 140 | } |
osmeest | 0:5dc369cc8778 | 141 | |
osmeest | 0:5dc369cc8778 | 142 | void TextLCD::writeData(int data) { |
osmeest | 0:5dc369cc8778 | 143 | _rs = 1; |
osmeest | 0:5dc369cc8778 | 144 | writeByte(data); |
osmeest | 0:5dc369cc8778 | 145 | } |
osmeest | 0:5dc369cc8778 | 146 | |
osmeest | 0:5dc369cc8778 | 147 | void TextLCD::setDisplayControl(DisplayControl d, CursorDisplayControl c, CursorStyle b) |
osmeest | 0:5dc369cc8778 | 148 | { |
osmeest | 0:5dc369cc8778 | 149 | writeCommand(0x08 | d | c | b); |
osmeest | 0:5dc369cc8778 | 150 | } |
osmeest | 0:5dc369cc8778 | 151 | |
osmeest | 0:5dc369cc8778 | 152 | void TextLCD::writeToCGMem(unsigned addr, const char *buffer, std::size_t size) |
osmeest | 0:5dc369cc8778 | 153 | { |
osmeest | 0:5dc369cc8778 | 154 | writeCommand(0x40 | (addr & 0x3f)); |
osmeest | 0:5dc369cc8778 | 155 | while (size > 0) { |
osmeest | 0:5dc369cc8778 | 156 | writeData(*buffer); |
osmeest | 0:5dc369cc8778 | 157 | size--; |
osmeest | 0:5dc369cc8778 | 158 | buffer++; |
osmeest | 0:5dc369cc8778 | 159 | } |
osmeest | 0:5dc369cc8778 | 160 | |
osmeest | 0:5dc369cc8778 | 161 | locate(_column, _row); |
osmeest | 0:5dc369cc8778 | 162 | } |
osmeest | 0:5dc369cc8778 | 163 | |
osmeest | 0:5dc369cc8778 | 164 | void TextLCD::BusOutput::writeNibble(int value) { |
osmeest | 0:5dc369cc8778 | 165 | _bus = value; |
osmeest | 0:5dc369cc8778 | 166 | } |
osmeest | 0:5dc369cc8778 | 167 | |
osmeest | 0:5dc369cc8778 | 168 | void TextLCD::PortOutput::writeNibble(int value) { |
osmeest | 0:5dc369cc8778 | 169 | _port = value << _shift; |
osmeest | 0:5dc369cc8778 | 170 | } |
osmeest | 0:5dc369cc8778 | 171 | |
osmeest | 0:5dc369cc8778 | 172 | } // ext_text_lcd namespace |