Alpha numeric LCD Library with support for 40x2 display

Dependencies:   mbed

Committer:
tonydbeck
Date:
Sat Jun 12 01:17:02 2010 +0000
Revision:
0:7e92a1d71370

        

Who changed what in which revision?

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