Optimization (wait(float) changed to wait_us(int)) of original library TextLCD.

Dependents:   EXAMPLE_Nucleo_InternalTempSensor EXAMPLE_Nucleo_mbed_RTOS_test_code TEST_Dist_lib

Fork of TextLCD by Simon Ford

Committer:
dzoni
Date:
Sat Dec 12 20:14:52 2015 +0000
Revision:
9:43c7f34925ac
Parent:
7:44f34c09bd37
1. Optimization: change from wait(float) to wait_us(int)

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