TextLCD library for controlling various LCD panels based on the HD44780 4-bit interface

Dependents:   DisplayDriver

Fork of TextLCD by Simon Ford

Committer:
atomicLogic
Date:
Mon May 15 18:29:27 2017 +0000
Revision:
10:5f557ed7ca4e
Parent:
9:8ba443e7fdca
Bla

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 */
simon 1:ac48b187213c 22
simon 1:ac48b187213c 23 #include "TextLCD.h"
simon 1:ac48b187213c 24 #include "mbed.h"
simon 1:ac48b187213c 25
simon 7:44f34c09bd37 26 TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5,
simon 7:44f34c09bd37 27 PinName d6, PinName d7, LCDType type) : _rs(rs),
atomicLogic 9:8ba443e7fdca 28 _e(e), _d(d4, d5, d6, d7),
atomicLogic 9:8ba443e7fdca 29 _type(type)
atomicLogic 9:8ba443e7fdca 30 {
simon 1:ac48b187213c 31
simon 1:ac48b187213c 32 _e = 1;
simon 1:ac48b187213c 33 _rs = 0; // command mode
simon 1:ac48b187213c 34
simon 1:ac48b187213c 35 wait(0.015); // Wait 15ms to ensure powered up
simon 1:ac48b187213c 36
simon 1:ac48b187213c 37 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
simon 1:ac48b187213c 38 for (int i=0; i<3; i++) {
simon 1:ac48b187213c 39 writeByte(0x3);
simon 1:ac48b187213c 40 wait(0.00164); // this command takes 1.64ms, so wait for it
simon 1:ac48b187213c 41 }
simon 1:ac48b187213c 42 writeByte(0x2); // 4-bit mode
simon 1:ac48b187213c 43 wait(0.000040f); // most instructions take 40us
simon 1:ac48b187213c 44
simon 1:ac48b187213c 45 writeCommand(0x28); // Function set 001 BW N F - -
simon 1:ac48b187213c 46 writeCommand(0x0C);
simon 1:ac48b187213c 47 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 48 cls();
simon 1:ac48b187213c 49 }
simon 1:ac48b187213c 50
atomicLogic 9:8ba443e7fdca 51 void TextLCD::character(int column, int row, int c)
atomicLogic 9:8ba443e7fdca 52 {
simon 1:ac48b187213c 53 int a = address(column, row);
simon 1:ac48b187213c 54 writeCommand(a);
simon 1:ac48b187213c 55 writeData(c);
simon 1:ac48b187213c 56 }
simon 1:ac48b187213c 57
atomicLogic 9:8ba443e7fdca 58 void TextLCD::cls()
atomicLogic 9:8ba443e7fdca 59 {
simon 1:ac48b187213c 60 writeCommand(0x01); // cls, and set cursor to 0
simon 1:ac48b187213c 61 wait(0.00164f); // This command takes 1.64 ms
simon 1:ac48b187213c 62 locate(0, 0);
simon 1:ac48b187213c 63 }
simon 1:ac48b187213c 64
atomicLogic 9:8ba443e7fdca 65 void TextLCD::locate(int column, int row)
atomicLogic 9:8ba443e7fdca 66 {
simon 1:ac48b187213c 67 _column = column;
simon 1:ac48b187213c 68 _row = row;
simon 1:ac48b187213c 69 }
simon 1:ac48b187213c 70
atomicLogic 9:8ba443e7fdca 71 void TextLCD::defineChar(int index, char *data)
atomicLogic 9:8ba443e7fdca 72 {
atomicLogic 9:8ba443e7fdca 73 if (index<0||index>7)
atomicLogic 9:8ba443e7fdca 74 return;
atomicLogic 9:8ba443e7fdca 75 writeCommand(0x40+index*8);
atomicLogic 9:8ba443e7fdca 76 for (int i=0; i<8; i++)
atomicLogic 9:8ba443e7fdca 77 writeData(*data++);
atomicLogic 9:8ba443e7fdca 78 }
atomicLogic 9:8ba443e7fdca 79
atomicLogic 9:8ba443e7fdca 80 int TextLCD::_putc(int value)
atomicLogic 9:8ba443e7fdca 81 {
simon 1:ac48b187213c 82 if (value == '\n') {
simon 1:ac48b187213c 83 _column = 0;
simon 1:ac48b187213c 84 _row++;
simon 1:ac48b187213c 85 if (_row >= rows()) {
simon 1:ac48b187213c 86 _row = 0;
simon 1:ac48b187213c 87 }
simon 1:ac48b187213c 88 } else {
simon 1:ac48b187213c 89 character(_column, _row, value);
simon 1:ac48b187213c 90 _column++;
simon 1:ac48b187213c 91 if (_column >= columns()) {
simon 1:ac48b187213c 92 _column = 0;
simon 1:ac48b187213c 93 _row++;
simon 1:ac48b187213c 94 if (_row >= rows()) {
simon 1:ac48b187213c 95 _row = 0;
simon 1:ac48b187213c 96 }
simon 1:ac48b187213c 97 }
simon 1:ac48b187213c 98 }
simon 1:ac48b187213c 99 return value;
simon 1:ac48b187213c 100 }
simon 1:ac48b187213c 101
atomicLogic 9:8ba443e7fdca 102 int TextLCD::_getc()
atomicLogic 9:8ba443e7fdca 103 {
simon 1:ac48b187213c 104 return -1;
simon 1:ac48b187213c 105 }
simon 1:ac48b187213c 106
atomicLogic 9:8ba443e7fdca 107 void TextLCD::writeByte(int value)
atomicLogic 9:8ba443e7fdca 108 {
simon 1:ac48b187213c 109 _d = value >> 4;
simon 1:ac48b187213c 110 wait(0.000040f); // most instructions take 40us
simon 1:ac48b187213c 111 _e = 0;
simon 1:ac48b187213c 112 wait(0.000040f);
simon 1:ac48b187213c 113 _e = 1;
simon 1:ac48b187213c 114 _d = value >> 0;
simon 1:ac48b187213c 115 wait(0.000040f);
simon 1:ac48b187213c 116 _e = 0;
simon 1:ac48b187213c 117 wait(0.000040f); // most instructions take 40us
simon 1:ac48b187213c 118 _e = 1;
simon 1:ac48b187213c 119 }
simon 1:ac48b187213c 120
atomicLogic 9:8ba443e7fdca 121 void TextLCD::writeCommand(int command)
atomicLogic 9:8ba443e7fdca 122 {
simon 1:ac48b187213c 123 _rs = 0;
simon 1:ac48b187213c 124 writeByte(command);
simon 1:ac48b187213c 125 }
simon 1:ac48b187213c 126
atomicLogic 9:8ba443e7fdca 127 void TextLCD::writeData(int data)
atomicLogic 9:8ba443e7fdca 128 {
simon 1:ac48b187213c 129 _rs = 1;
simon 1:ac48b187213c 130 writeByte(data);
simon 1:ac48b187213c 131 }
simon 1:ac48b187213c 132
atomicLogic 9:8ba443e7fdca 133 int TextLCD::address(int column, int row)
atomicLogic 9:8ba443e7fdca 134 {
simon 1:ac48b187213c 135 switch (_type) {
simon 1:ac48b187213c 136 case LCD20x4:
simon 1:ac48b187213c 137 switch (row) {
simon 1:ac48b187213c 138 case 0:
simon 1:ac48b187213c 139 return 0x80 + column;
simon 1:ac48b187213c 140 case 1:
simon 1:ac48b187213c 141 return 0xc0 + column;
simon 1:ac48b187213c 142 case 2:
simon 1:ac48b187213c 143 return 0x94 + column;
simon 1:ac48b187213c 144 case 3:
simon 1:ac48b187213c 145 return 0xd4 + column;
simon 1:ac48b187213c 146 }
simon 1:ac48b187213c 147 case LCD16x2B:
simon 4:bf5b706f8d32 148 return 0x80 + (row * 40) + column;
simon 1:ac48b187213c 149 case LCD16x2:
simon 1:ac48b187213c 150 case LCD20x2:
simon 1:ac48b187213c 151 default:
simon 4:bf5b706f8d32 152 return 0x80 + (row * 0x40) + column;
simon 1:ac48b187213c 153 }
simon 1:ac48b187213c 154 }
simon 1:ac48b187213c 155
atomicLogic 9:8ba443e7fdca 156 int TextLCD::columns()
atomicLogic 9:8ba443e7fdca 157 {
simon 1:ac48b187213c 158 switch (_type) {
simon 1:ac48b187213c 159 case LCD20x4:
simon 1:ac48b187213c 160 case LCD20x2:
simon 1:ac48b187213c 161 return 20;
simon 1:ac48b187213c 162 case LCD16x2:
simon 1:ac48b187213c 163 case LCD16x2B:
simon 1:ac48b187213c 164 default:
simon 1:ac48b187213c 165 return 16;
simon 1:ac48b187213c 166 }
simon 1:ac48b187213c 167 }
simon 1:ac48b187213c 168
atomicLogic 9:8ba443e7fdca 169 int TextLCD::rows()
atomicLogic 9:8ba443e7fdca 170 {
simon 1:ac48b187213c 171 switch (_type) {
simon 1:ac48b187213c 172 case LCD20x4:
simon 1:ac48b187213c 173 return 4;
simon 1:ac48b187213c 174 case LCD16x2:
simon 1:ac48b187213c 175 case LCD16x2B:
simon 1:ac48b187213c 176 case LCD20x2:
simon 1:ac48b187213c 177 default:
simon 1:ac48b187213c 178 return 2;
simon 1:ac48b187213c 179 }
atomicLogic 9:8ba443e7fdca 180 }