Edutech IoT Team / EPB_TextLCD
Committer:
dwijaybane
Date:
Mon Jan 25 07:31:21 2016 +0000
Revision:
0:c1ed4a9787c7
EPB_ASK25 LCD 4bit code

Who changed what in which revision?

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