Based on the NewTextLib, I made a modification for using this lib on the Elmicro Testbed with display GDM2004D

Dependencies:   mbed

Committer:
TP
Date:
Sat Jun 11 20:34:14 2011 +0000
Revision:
0:9c69fa6c1120

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TP 0:9c69fa6c1120 1 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
TP 0:9c69fa6c1120 2 * Copyright (c) 2007-2010, sford, http://mbed.org
TP 0:9c69fa6c1120 3 * Changes by Erik Kerger
TP 0:9c69fa6c1120 4 *
TP 0:9c69fa6c1120 5 *
TP 0:9c69fa6c1120 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
TP 0:9c69fa6c1120 7 * of this software and associated documentation files (the "Software"), to deal
TP 0:9c69fa6c1120 8 * in the Software without restriction, including without limitation the rights
TP 0:9c69fa6c1120 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
TP 0:9c69fa6c1120 10 * copies of the Software, and to permit persons to whom the Software is
TP 0:9c69fa6c1120 11 * furnished to do so, subject to the following conditions:
TP 0:9c69fa6c1120 12 *
TP 0:9c69fa6c1120 13 * The above copyright notice and this permission notice shall be included in
TP 0:9c69fa6c1120 14 * all copies or substantial portions of the Software.
TP 0:9c69fa6c1120 15 *
TP 0:9c69fa6c1120 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
TP 0:9c69fa6c1120 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
TP 0:9c69fa6c1120 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
TP 0:9c69fa6c1120 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
TP 0:9c69fa6c1120 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
TP 0:9c69fa6c1120 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
TP 0:9c69fa6c1120 22 * THE SOFTWARE.
TP 0:9c69fa6c1120 23 */
TP 0:9c69fa6c1120 24
TP 0:9c69fa6c1120 25 #include "NewTextLCD.h"
TP 0:9c69fa6c1120 26 #include "mbed.h"
TP 0:9c69fa6c1120 27
TP 0:9c69fa6c1120 28 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d4, PinName d5,
TP 0:9c69fa6c1120 29 PinName d6, PinName d7, LCDType type) : _rs(rs), _rw(rw), _e(e),
TP 0:9c69fa6c1120 30 _d(d4, d5, d6, d7), _type(type) {
TP 0:9c69fa6c1120 31 _rs = 0; // command mode
TP 0:9c69fa6c1120 32 _rw = 0; // r/w in defined state
TP 0:9c69fa6c1120 33 _e = 0; // 0 --> inactive (most important change to original TextLCD)
TP 0:9c69fa6c1120 34
TP 0:9c69fa6c1120 35 if (_type != LCDuser)
TP 0:9c69fa6c1120 36 setLCDparam(_type); // otherwise rows, colums, comdelay, adresses must be set before
TP 0:9c69fa6c1120 37
TP 0:9c69fa6c1120 38 wait(0.050f); // Wait 50ms to ensure powered up
TP 0:9c69fa6c1120 39
TP 0:9c69fa6c1120 40 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
TP 0:9c69fa6c1120 41 // this sets controler into 8 bit mode, so we have a defined state
TP 0:9c69fa6c1120 42 for (int i=0; i<3; i++) {
TP 0:9c69fa6c1120 43 _e = 1;
TP 0:9c69fa6c1120 44 __nop();
TP 0:9c69fa6c1120 45 __nop();
TP 0:9c69fa6c1120 46 _d = 0x3;
TP 0:9c69fa6c1120 47 __nop(); // data setup time at least 60 ns
TP 0:9c69fa6c1120 48 _e = 0;
TP 0:9c69fa6c1120 49 wait(0.005f); // 5ms
TP 0:9c69fa6c1120 50 }
TP 0:9c69fa6c1120 51 _e = 1;
TP 0:9c69fa6c1120 52 __nop();
TP 0:9c69fa6c1120 53 __nop();
TP 0:9c69fa6c1120 54 _d = 0x2; // 4 Bit mode (after this command, we have to send 2 nibbles)
TP 0:9c69fa6c1120 55 __nop();
TP 0:9c69fa6c1120 56 _e = 0;
TP 0:9c69fa6c1120 57 wait_us(LCDparam.delay);
TP 0:9c69fa6c1120 58
TP 0:9c69fa6c1120 59 writeCommand(0x28); // Function set 4 Bit, 2Line, 5*7
TP 0:9c69fa6c1120 60 writeCommand(0x08); // Display off
TP 0:9c69fa6c1120 61 cls(); // clear display, reset _column and _row
TP 0:9c69fa6c1120 62 writeCommand(0x04); // cursor right, Display is not shifted
TP 0:9c69fa6c1120 63 writeCommand(0x0C); // Display on , Cursor off
TP 0:9c69fa6c1120 64 }
TP 0:9c69fa6c1120 65
TP 0:9c69fa6c1120 66 void TextLCD::character(int column, int row, int c) {
TP 0:9c69fa6c1120 67 int a = 0x80 | (LCDparam.adresses[row & 3] + column);
TP 0:9c69fa6c1120 68 writeCommand(a); // set cursor address
TP 0:9c69fa6c1120 69 writeData(c); // write char
TP 0:9c69fa6c1120 70 }
TP 0:9c69fa6c1120 71
TP 0:9c69fa6c1120 72 void TextLCD::cls() {
TP 0:9c69fa6c1120 73 writeCommand(0x01); // cls, and set cursor to 0
TP 0:9c69fa6c1120 74 locate(0, 0); // set internal position
TP 0:9c69fa6c1120 75 wait_us(45 * LCDparam.delay); // CLS need much time
TP 0:9c69fa6c1120 76 }
TP 0:9c69fa6c1120 77
TP 0:9c69fa6c1120 78 void TextLCD::locate(int column, int row) {
TP 0:9c69fa6c1120 79 _column = column; // set position for next char
TP 0:9c69fa6c1120 80 _row = row; // note: cursor is not set yet
TP 0:9c69fa6c1120 81 }
TP 0:9c69fa6c1120 82
TP 0:9c69fa6c1120 83
TP 0:9c69fa6c1120 84
TP 0:9c69fa6c1120 85 int TextLCD::_putc(int value) {
TP 0:9c69fa6c1120 86 if (value == '\n') {
TP 0:9c69fa6c1120 87 _column = 0;
TP 0:9c69fa6c1120 88 _row++;
TP 0:9c69fa6c1120 89 if (_row >= LCDparam.rows) {
TP 0:9c69fa6c1120 90 _row = 0;
TP 0:9c69fa6c1120 91 }
TP 0:9c69fa6c1120 92 } else {
TP 0:9c69fa6c1120 93 character(_column, _row, value);
TP 0:9c69fa6c1120 94 _column++;
TP 0:9c69fa6c1120 95 if (_column >= LCDparam.columns) {
TP 0:9c69fa6c1120 96 _column = 0;
TP 0:9c69fa6c1120 97 _row++;
TP 0:9c69fa6c1120 98 if (_row >= LCDparam.rows) {
TP 0:9c69fa6c1120 99 _row = 0;
TP 0:9c69fa6c1120 100 }
TP 0:9c69fa6c1120 101 }
TP 0:9c69fa6c1120 102 }
TP 0:9c69fa6c1120 103 return value;
TP 0:9c69fa6c1120 104 }
TP 0:9c69fa6c1120 105
TP 0:9c69fa6c1120 106 // Dummy function - read not supported
TP 0:9c69fa6c1120 107 int TextLCD::_getc() {
TP 0:9c69fa6c1120 108 return -1;
TP 0:9c69fa6c1120 109 }
TP 0:9c69fa6c1120 110
TP 0:9c69fa6c1120 111 void TextLCD::writeByte(int value) {
TP 0:9c69fa6c1120 112 _e = 1;
TP 0:9c69fa6c1120 113 __nop();
TP 0:9c69fa6c1120 114 _d = value >> 4; // higher nibble first
TP 0:9c69fa6c1120 115 __nop();
TP 0:9c69fa6c1120 116 _e = 0;
TP 0:9c69fa6c1120 117 __nop();
TP 0:9c69fa6c1120 118 __nop();
TP 0:9c69fa6c1120 119 _e = 1;
TP 0:9c69fa6c1120 120 __nop();
TP 0:9c69fa6c1120 121 _d = value >> 0; // then lower
TP 0:9c69fa6c1120 122 __nop();
TP 0:9c69fa6c1120 123 _e = 0;
TP 0:9c69fa6c1120 124 }
TP 0:9c69fa6c1120 125
TP 0:9c69fa6c1120 126 void TextLCD::writeCommand(int command) {
TP 0:9c69fa6c1120 127 _rs = 0;
TP 0:9c69fa6c1120 128 writeByte(command);
TP 0:9c69fa6c1120 129 wait_us(LCDparam.delay);
TP 0:9c69fa6c1120 130 }
TP 0:9c69fa6c1120 131
TP 0:9c69fa6c1120 132
TP 0:9c69fa6c1120 133 void TextLCD::writeData(int data) {
TP 0:9c69fa6c1120 134 _rs = 1;
TP 0:9c69fa6c1120 135 writeByte(data);
TP 0:9c69fa6c1120 136 wait_us(LCDparam.delay);
TP 0:9c69fa6c1120 137 }
TP 0:9c69fa6c1120 138
TP 0:9c69fa6c1120 139
TP 0:9c69fa6c1120 140 // set user defined char
TP 0:9c69fa6c1120 141 void TextLCD::writeCGRAM(int address, int pattern[8]){
TP 0:9c69fa6c1120 142 int i;
TP 0:9c69fa6c1120 143 address = address & 7; //max 8 char
TP 0:9c69fa6c1120 144 for(i=0;i<8;i++){
TP 0:9c69fa6c1120 145 writeCommand(0x40 | (address * 8) + i);
TP 0:9c69fa6c1120 146 writeData(pattern[i]);
TP 0:9c69fa6c1120 147 }
TP 0:9c69fa6c1120 148 }
TP 0:9c69fa6c1120 149
TP 0:9c69fa6c1120 150 void TextLCD::setLCDparam(LCDType _type){
TP 0:9c69fa6c1120 151 switch (_type) {
TP 0:9c69fa6c1120 152
TP 0:9c69fa6c1120 153 case LCD16x2:
TP 0:9c69fa6c1120 154 case LCD16x2B:
TP 0:9c69fa6c1120 155 LCDparam.columns = 16;
TP 0:9c69fa6c1120 156 break;
TP 0:9c69fa6c1120 157 case LCD20x2:
TP 0:9c69fa6c1120 158 case LCD20x4:
TP 0:9c69fa6c1120 159 LCDparam.columns = 20;
TP 0:9c69fa6c1120 160 break;
TP 0:9c69fa6c1120 161 case LCD24x2:
TP 0:9c69fa6c1120 162 LCDparam.columns = 24;
TP 0:9c69fa6c1120 163 break;
TP 0:9c69fa6c1120 164 }
TP 0:9c69fa6c1120 165 if (_type == LCD20x4)
TP 0:9c69fa6c1120 166 LCDparam.rows = 4;
TP 0:9c69fa6c1120 167 else
TP 0:9c69fa6c1120 168 LCDparam.rows = 2;
TP 0:9c69fa6c1120 169
TP 0:9c69fa6c1120 170 LCDparam.adresses[0] = 0;
TP 0:9c69fa6c1120 171
TP 0:9c69fa6c1120 172 if (_type == LCD16x2B)
TP 0:9c69fa6c1120 173 LCDparam.adresses[1] = 40;
TP 0:9c69fa6c1120 174 else
TP 0:9c69fa6c1120 175 LCDparam.adresses[1] = 0x40;
TP 0:9c69fa6c1120 176
TP 0:9c69fa6c1120 177 if (_type == LCD20x4) {
TP 0:9c69fa6c1120 178 LCDparam.adresses[2] = 0x14;
TP 0:9c69fa6c1120 179 LCDparam.adresses[3] = 0x54;}
TP 0:9c69fa6c1120 180 else {
TP 0:9c69fa6c1120 181 LCDparam.adresses[2] = 0;
TP 0:9c69fa6c1120 182 LCDparam.adresses[3] = 0;}
TP 0:9c69fa6c1120 183
TP 0:9c69fa6c1120 184 LCDparam.delay = 50; // 50 us delays as default
TP 0:9c69fa6c1120 185 }
TP 0:9c69fa6c1120 186