Dependents:   Project_Stepper MP3_Player_Touch_Panel_Input_LCD_Display WeatherStation HTTPClientTest ... more

Committer:
benyun
Date:
Wed Aug 03 00:44:54 2011 +0000
Revision:
0:7dd9751172e1

        

Who changed what in which revision?

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