Dependents:   revcounter SDFileSystem_Load TMP102HelloWorld KnightRiderOptimized ... more

Committer:
tlunzer
Date:
Tue May 24 05:13:04 2011 +0000
Revision:
0:a7c08c5305b3
added support for 16x1 panels

Who changed what in which revision?

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