private Library copy of TextLCD

Dependents:   eBible ej_RSSfeeder erg y_HM55B ... more

Committer:
davervw
Date:
Sun Feb 27 18:50:36 2011 +0000
Revision:
0:c5318c74f1a9
Revised to support 40x2 and 8x2 screens

Who changed what in which revision?

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