TextLCD library for controlling various LCD panels based on the HD44780 4-bit interface. Updated for Mbed OS 6.

Committer:
JLarkin
Date:
Sun Jan 10 20:28:36 2021 +0000
Revision:
9:d3875959a370
Parent:
7:44f34c09bd37
Updated for Mbed OS 6: short wait to wait_us and long wait to sleep_for

Who changed what in which revision?

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