LCD driver

Dependents:   host_mouse lcd_keypad_shield TestDeviceIot

Committer:
duchonic
Date:
Fri Jul 26 03:30:12 2019 +0000
Revision:
1:19be52262a2d
Parent:
0:ec079a141883
added lcd shield

Who changed what in which revision?

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