Committer:
esmiwa
Date:
Sun Jun 17 01:15:35 2012 +0000
Revision:
0:5bf7e3564c3b

        

Who changed what in which revision?

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