hai_lcd

Committer:
karthickkarthi
Date:
Mon Jan 02 08:15:11 2017 +0000
Revision:
0:a12c4e4fe6b9
hai

Who changed what in which revision?

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