TextLCD_piano adapted to LCD screen with R/W

Fork of TextLCD by Simon Ford

Committer:
Aliened
Date:
Mon Jul 02 17:58:56 2018 +0000
Revision:
9:05c9b97e4a91
Parent:
7:44f34c09bd37
Compatibility with R/W pin of Character LCD 16 x 2 character LCD display module:; Manufacturer: Newhaven Display Intl	; Manufacturer part number: NHD-0216HZ-FSW-FBW-33V3C-ND	; Part number: NHD-0216HZ-FSW-FBW-33V3C

Who changed what in which revision?

UserRevisionLine numberNew 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 */
Aliened 9:05c9b97e4a91 22
simon 1:ac48b187213c 23 #include "TextLCD.h"
simon 1:ac48b187213c 24 #include "mbed.h"
Aliened 9:05c9b97e4a91 25
Aliened 9:05c9b97e4a91 26 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1,
Aliened 9:05c9b97e4a91 27 PinName d2, PinName d3, LCDType type) : _rs(rs), _rw(rw),
Aliened 9:05c9b97e4a91 28 _e(e), _d(d0, d1, d2, d3), _type(type) {
simon 1:ac48b187213c 29 _rs = 0; // command mode
Aliened 9:05c9b97e4a91 30 _rw = 0;
Aliened 9:05c9b97e4a91 31 _e = 0;
Aliened 9:05c9b97e4a91 32 _d.output(); // data out
Aliened 9:05c9b97e4a91 33
Aliened 9:05c9b97e4a91 34 wait(0.05); // Wait 50ms to ensure powered up
Aliened 9:05c9b97e4a91 35
simon 1:ac48b187213c 36 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
simon 1:ac48b187213c 37 for (int i=0; i<3; i++) {
Aliened 9:05c9b97e4a91 38 _e = 1;
Aliened 9:05c9b97e4a91 39 wait(0.000001f);
Aliened 9:05c9b97e4a91 40 _d = 0x3;
Aliened 9:05c9b97e4a91 41 wait(0.000001f);
Aliened 9:05c9b97e4a91 42 _e = 0;
Aliened 9:05c9b97e4a91 43 wait(0.004f); // 4ms
Aliened 9:05c9b97e4a91 44 }
Aliened 9:05c9b97e4a91 45 _e = 1;
Aliened 9:05c9b97e4a91 46 wait(0.000001f);
Aliened 9:05c9b97e4a91 47 _d = 0x2; // 4 Bit mode
Aliened 9:05c9b97e4a91 48 wait(0.000001f);
Aliened 9:05c9b97e4a91 49 _e = 0;
Aliened 9:05c9b97e4a91 50
Aliened 9:05c9b97e4a91 51 writeCommand(0x28); // Function set 4 Bit, 2Line, 5*7
Aliened 9:05c9b97e4a91 52 writeCommand(0x08); // Display off
Aliened 9:05c9b97e4a91 53 writeCommand(0x01); // clear Display
Aliened 9:05c9b97e4a91 54 writeCommand(0x04); // cursor right, Display is not shifted
Aliened 9:05c9b97e4a91 55 writeCommand(0x0C); // Display on , Cursor off
simon 1:ac48b187213c 56 }
Aliened 9:05c9b97e4a91 57
simon 1:ac48b187213c 58 void TextLCD::character(int column, int row, int c) {
simon 1:ac48b187213c 59 int a = address(column, row);
simon 1:ac48b187213c 60 writeCommand(a);
simon 1:ac48b187213c 61 writeData(c);
simon 1:ac48b187213c 62 }
Aliened 9:05c9b97e4a91 63
simon 1:ac48b187213c 64 void TextLCD::cls() {
simon 1:ac48b187213c 65 writeCommand(0x01); // cls, and set cursor to 0
simon 1:ac48b187213c 66 locate(0, 0);
simon 1:ac48b187213c 67 }
Aliened 9:05c9b97e4a91 68
simon 1:ac48b187213c 69 void TextLCD::locate(int column, int row) {
simon 1:ac48b187213c 70 _column = column;
simon 1:ac48b187213c 71 _row = row;
simon 1:ac48b187213c 72 }
Aliened 9:05c9b97e4a91 73
Aliened 9:05c9b97e4a91 74
Aliened 9:05c9b97e4a91 75
simon 1:ac48b187213c 76 int TextLCD::_putc(int value) {
simon 1:ac48b187213c 77 if (value == '\n') {
simon 1:ac48b187213c 78 _column = 0;
simon 1:ac48b187213c 79 _row++;
simon 1:ac48b187213c 80 if (_row >= rows()) {
simon 1:ac48b187213c 81 _row = 0;
simon 1:ac48b187213c 82 }
simon 1:ac48b187213c 83 } else {
simon 1:ac48b187213c 84 character(_column, _row, value);
simon 1:ac48b187213c 85 _column++;
simon 1:ac48b187213c 86 if (_column >= columns()) {
simon 1:ac48b187213c 87 _column = 0;
simon 1:ac48b187213c 88 _row++;
simon 1:ac48b187213c 89 if (_row >= rows()) {
simon 1:ac48b187213c 90 _row = 0;
simon 1:ac48b187213c 91 }
simon 1:ac48b187213c 92 }
simon 1:ac48b187213c 93 }
simon 1:ac48b187213c 94 return value;
simon 1:ac48b187213c 95 }
Aliened 9:05c9b97e4a91 96
simon 1:ac48b187213c 97 int TextLCD::_getc() {
Aliened 9:05c9b97e4a91 98 int a = address(_column, _row);
Aliened 9:05c9b97e4a91 99 writeCommand(a);
Aliened 9:05c9b97e4a91 100 return (readData());
simon 1:ac48b187213c 101 }
Aliened 9:05c9b97e4a91 102
simon 1:ac48b187213c 103 void TextLCD::writeByte(int value) {
Aliened 9:05c9b97e4a91 104 _e = 1;
Aliened 9:05c9b97e4a91 105 wait(0.000001f);
simon 1:ac48b187213c 106 _d = value >> 4;
Aliened 9:05c9b97e4a91 107 wait(0.000001f);
simon 1:ac48b187213c 108 _e = 0;
Aliened 9:05c9b97e4a91 109 wait(0.000001f);
simon 1:ac48b187213c 110 _e = 1;
Aliened 9:05c9b97e4a91 111 wait(0.000001f);
simon 1:ac48b187213c 112 _d = value >> 0;
Aliened 9:05c9b97e4a91 113 wait(0.000001f);
simon 1:ac48b187213c 114 _e = 0;
simon 1:ac48b187213c 115 }
Aliened 9:05c9b97e4a91 116
simon 1:ac48b187213c 117 void TextLCD::writeCommand(int command) {
Aliened 9:05c9b97e4a91 118 waitBusy(); // check if display is ready
simon 1:ac48b187213c 119 _rs = 0;
simon 1:ac48b187213c 120 writeByte(command);
simon 1:ac48b187213c 121 }
Aliened 9:05c9b97e4a91 122
Aliened 9:05c9b97e4a91 123 int TextLCD::readData(){
Aliened 9:05c9b97e4a91 124 int input;
Aliened 9:05c9b97e4a91 125 waitBusy();
Aliened 9:05c9b97e4a91 126 _rw = 1;
Aliened 9:05c9b97e4a91 127 _rs = 1;
Aliened 9:05c9b97e4a91 128 wait(0.000001f);
Aliened 9:05c9b97e4a91 129 _d.input(); // switch Data port to input
Aliened 9:05c9b97e4a91 130 _e = 1;
Aliened 9:05c9b97e4a91 131 wait(0.000001f);
Aliened 9:05c9b97e4a91 132 input = _d.read() << 4; // high nibble
Aliened 9:05c9b97e4a91 133 _e = 0;
Aliened 9:05c9b97e4a91 134 wait(0.000001f);
Aliened 9:05c9b97e4a91 135 _e = 1;
Aliened 9:05c9b97e4a91 136 wait(0.000001f);
Aliened 9:05c9b97e4a91 137 input = input | _d.read(); // low nibble
Aliened 9:05c9b97e4a91 138 _e = 0;
Aliened 9:05c9b97e4a91 139 return (input);
Aliened 9:05c9b97e4a91 140 }
Aliened 9:05c9b97e4a91 141
Aliened 9:05c9b97e4a91 142 void TextLCD::waitBusy(){
Aliened 9:05c9b97e4a91 143 int input;
Aliened 9:05c9b97e4a91 144 _rw = 1;
Aliened 9:05c9b97e4a91 145 _rs = 0;
Aliened 9:05c9b97e4a91 146 wait(0.000001f);
Aliened 9:05c9b97e4a91 147 _d.input(); // switch Data port to input
Aliened 9:05c9b97e4a91 148 do{
Aliened 9:05c9b97e4a91 149 _e = 1;
Aliened 9:05c9b97e4a91 150 wait(0.000001f);
Aliened 9:05c9b97e4a91 151 input = _d.read();
Aliened 9:05c9b97e4a91 152 _e = 0;
Aliened 9:05c9b97e4a91 153 wait(0.000001f);
Aliened 9:05c9b97e4a91 154 _e = 1;
Aliened 9:05c9b97e4a91 155 wait(0.000001f);
Aliened 9:05c9b97e4a91 156 _e = 0;
Aliened 9:05c9b97e4a91 157 }while((0x8 & input) == 0x8); // wait until display is ready
Aliened 9:05c9b97e4a91 158 _rw = 0;
Aliened 9:05c9b97e4a91 159 _d.output(); // switch port back to output
Aliened 9:05c9b97e4a91 160 }
Aliened 9:05c9b97e4a91 161
simon 1:ac48b187213c 162 void TextLCD::writeData(int data) {
Aliened 9:05c9b97e4a91 163 waitBusy();
simon 1:ac48b187213c 164 _rs = 1;
simon 1:ac48b187213c 165 writeByte(data);
simon 1:ac48b187213c 166 }
Aliened 9:05c9b97e4a91 167
Aliened 9:05c9b97e4a91 168
Aliened 9:05c9b97e4a91 169 // set user defined char
Aliened 9:05c9b97e4a91 170 void TextLCD::writeCGRAM(int address, int pattern[8]){
Aliened 9:05c9b97e4a91 171 int i;
Aliened 9:05c9b97e4a91 172 address = address & 0x07; //max 8 char
Aliened 9:05c9b97e4a91 173 for(i=0;i<8;i++){
Aliened 9:05c9b97e4a91 174 waitBusy(); // check if display is ready
Aliened 9:05c9b97e4a91 175 _rs = 0;
Aliened 9:05c9b97e4a91 176 writeByte(0x40 + address * 8 + i);
Aliened 9:05c9b97e4a91 177 writeData(pattern[i]);
Aliened 9:05c9b97e4a91 178 }
Aliened 9:05c9b97e4a91 179 }
Aliened 9:05c9b97e4a91 180
Aliened 9:05c9b97e4a91 181
simon 1:ac48b187213c 182 int TextLCD::address(int column, int row) {
simon 1:ac48b187213c 183 switch (_type) {
simon 1:ac48b187213c 184 case LCD20x4:
simon 1:ac48b187213c 185 switch (row) {
simon 1:ac48b187213c 186 case 0:
simon 1:ac48b187213c 187 return 0x80 + column;
simon 1:ac48b187213c 188 case 1:
simon 1:ac48b187213c 189 return 0xc0 + column;
simon 1:ac48b187213c 190 case 2:
simon 1:ac48b187213c 191 return 0x94 + column;
simon 1:ac48b187213c 192 case 3:
simon 1:ac48b187213c 193 return 0xd4 + column;
simon 1:ac48b187213c 194 }
simon 1:ac48b187213c 195 case LCD16x2B:
simon 4:bf5b706f8d32 196 return 0x80 + (row * 40) + column;
simon 1:ac48b187213c 197 case LCD16x2:
simon 1:ac48b187213c 198 case LCD20x2:
simon 1:ac48b187213c 199 default:
simon 4:bf5b706f8d32 200 return 0x80 + (row * 0x40) + column;
simon 1:ac48b187213c 201 }
simon 1:ac48b187213c 202 }
Aliened 9:05c9b97e4a91 203
simon 1:ac48b187213c 204 int TextLCD::columns() {
simon 1:ac48b187213c 205 switch (_type) {
simon 1:ac48b187213c 206 case LCD20x4:
simon 1:ac48b187213c 207 case LCD20x2:
simon 1:ac48b187213c 208 return 20;
simon 1:ac48b187213c 209 case LCD16x2:
simon 1:ac48b187213c 210 case LCD16x2B:
simon 1:ac48b187213c 211 default:
simon 1:ac48b187213c 212 return 16;
simon 1:ac48b187213c 213 }
simon 1:ac48b187213c 214 }
Aliened 9:05c9b97e4a91 215
simon 1:ac48b187213c 216 int TextLCD::rows() {
simon 1:ac48b187213c 217 switch (_type) {
simon 1:ac48b187213c 218 case LCD20x4:
simon 1:ac48b187213c 219 return 4;
simon 1:ac48b187213c 220 case LCD16x2:
simon 1:ac48b187213c 221 case LCD16x2B:
simon 1:ac48b187213c 222 case LCD20x2:
simon 1:ac48b187213c 223 default:
simon 1:ac48b187213c 224 return 2;
simon 1:ac48b187213c 225 }
Aliened 9:05c9b97e4a91 226 }