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