added lcd.start()

Fork of TextLCD by Simon Ford

Committer:
ritarosakai
Date:
Mon Jan 08 07:21:35 2018 +0000
Revision:
9:496c520855a1
Parent:
7:44f34c09bd37
Child:
10:cec91d426d65
Added lcd.start()

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
simon 7:44f34c09bd37 26 TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5,
simon 7:44f34c09bd37 27 PinName d6, PinName d7, LCDType type) : _rs(rs),
ritarosakai 9:496c520855a1 28 _e(e), _d(d4, d5, d6, d7),
ritarosakai 9:496c520855a1 29 _type(type)
ritarosakai 9:496c520855a1 30 {
ritarosakai 9:496c520855a1 31 }
simon 1:ac48b187213c 32
ritarosakai 9:496c520855a1 33 void TextLCD::start()
ritarosakai 9:496c520855a1 34 {
simon 1:ac48b187213c 35 _e = 1;
simon 1:ac48b187213c 36 _rs = 0; // command mode
simon 1:ac48b187213c 37
simon 1:ac48b187213c 38 wait(0.015); // Wait 15ms 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);
simon 1:ac48b187213c 43 wait(0.00164); // this command takes 1.64ms, so wait for it
simon 1:ac48b187213c 44 }
simon 1:ac48b187213c 45 writeByte(0x2); // 4-bit mode
simon 1:ac48b187213c 46 wait(0.000040f); // 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
ritarosakai 9:496c520855a1 54 void TextLCD::character(int column, int row, int c)
ritarosakai 9:496c520855a1 55 {
simon 1:ac48b187213c 56 int a = address(column, row);
simon 1:ac48b187213c 57 writeCommand(a);
simon 1:ac48b187213c 58 writeData(c);
simon 1:ac48b187213c 59 }
simon 1:ac48b187213c 60
ritarosakai 9:496c520855a1 61 void TextLCD::cls()
ritarosakai 9:496c520855a1 62 {
ritarosakai 9:496c520855a1 63 _e = 1;
ritarosakai 9:496c520855a1 64 _rs = 0; // command mode
ritarosakai 9:496c520855a1 65
ritarosakai 9:496c520855a1 66 wait(0.015); // Wait 15ms to ensure powered up
ritarosakai 9:496c520855a1 67
ritarosakai 9:496c520855a1 68 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
ritarosakai 9:496c520855a1 69 for (int i=0; i<3; i++) {
ritarosakai 9:496c520855a1 70 writeByte(0x3);
ritarosakai 9:496c520855a1 71 wait(0.00164); // this command takes 1.64ms, so wait for it
ritarosakai 9:496c520855a1 72 }
ritarosakai 9:496c520855a1 73 writeByte(0x2); // 4-bit mode
ritarosakai 9:496c520855a1 74 wait(0.000040f); // most instructions take 40us
ritarosakai 9:496c520855a1 75
ritarosakai 9:496c520855a1 76 writeCommand(0x28); // Function set 001 BW N F - -
ritarosakai 9:496c520855a1 77 writeCommand(0x0C);
ritarosakai 9:496c520855a1 78 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 79 writeCommand(0x01); // cls, and set cursor to 0
simon 1:ac48b187213c 80 wait(0.00164f); // This command takes 1.64 ms
simon 1:ac48b187213c 81 locate(0, 0);
simon 1:ac48b187213c 82 }
simon 1:ac48b187213c 83
ritarosakai 9:496c520855a1 84 void TextLCD::locate(int column, int row)
ritarosakai 9:496c520855a1 85 {
simon 1:ac48b187213c 86 _column = column;
simon 1:ac48b187213c 87 _row = row;
simon 1:ac48b187213c 88 }
simon 1:ac48b187213c 89
ritarosakai 9:496c520855a1 90 int TextLCD::_putc(int value)
ritarosakai 9:496c520855a1 91 {
simon 1:ac48b187213c 92 if (value == '\n') {
simon 1:ac48b187213c 93 _column = 0;
simon 1:ac48b187213c 94 _row++;
simon 1:ac48b187213c 95 if (_row >= rows()) {
simon 1:ac48b187213c 96 _row = 0;
simon 1:ac48b187213c 97 }
simon 1:ac48b187213c 98 } else {
simon 1:ac48b187213c 99 character(_column, _row, value);
simon 1:ac48b187213c 100 _column++;
simon 1:ac48b187213c 101 if (_column >= columns()) {
simon 1:ac48b187213c 102 _column = 0;
simon 1:ac48b187213c 103 _row++;
simon 1:ac48b187213c 104 if (_row >= rows()) {
simon 1:ac48b187213c 105 _row = 0;
simon 1:ac48b187213c 106 }
simon 1:ac48b187213c 107 }
simon 1:ac48b187213c 108 }
simon 1:ac48b187213c 109 return value;
simon 1:ac48b187213c 110 }
simon 1:ac48b187213c 111
ritarosakai 9:496c520855a1 112 int TextLCD::_getc()
ritarosakai 9:496c520855a1 113 {
simon 1:ac48b187213c 114 return -1;
simon 1:ac48b187213c 115 }
simon 1:ac48b187213c 116
ritarosakai 9:496c520855a1 117 void TextLCD::writeByte(int value)
ritarosakai 9:496c520855a1 118 {
simon 1:ac48b187213c 119 _d = value >> 4;
simon 1:ac48b187213c 120 wait(0.000040f); // most instructions take 40us
simon 1:ac48b187213c 121 _e = 0;
simon 1:ac48b187213c 122 wait(0.000040f);
simon 1:ac48b187213c 123 _e = 1;
simon 1:ac48b187213c 124 _d = value >> 0;
simon 1:ac48b187213c 125 wait(0.000040f);
simon 1:ac48b187213c 126 _e = 0;
simon 1:ac48b187213c 127 wait(0.000040f); // most instructions take 40us
simon 1:ac48b187213c 128 _e = 1;
simon 1:ac48b187213c 129 }
simon 1:ac48b187213c 130
ritarosakai 9:496c520855a1 131 void TextLCD::writeCommand(int command)
ritarosakai 9:496c520855a1 132 {
simon 1:ac48b187213c 133 _rs = 0;
simon 1:ac48b187213c 134 writeByte(command);
simon 1:ac48b187213c 135 }
simon 1:ac48b187213c 136
ritarosakai 9:496c520855a1 137 void TextLCD::writeData(int data)
ritarosakai 9:496c520855a1 138 {
simon 1:ac48b187213c 139 _rs = 1;
simon 1:ac48b187213c 140 writeByte(data);
simon 1:ac48b187213c 141 }
simon 1:ac48b187213c 142
ritarosakai 9:496c520855a1 143 int TextLCD::address(int column, int row)
ritarosakai 9:496c520855a1 144 {
simon 1:ac48b187213c 145 switch (_type) {
simon 1:ac48b187213c 146 case LCD20x4:
simon 1:ac48b187213c 147 switch (row) {
simon 1:ac48b187213c 148 case 0:
simon 1:ac48b187213c 149 return 0x80 + column;
simon 1:ac48b187213c 150 case 1:
simon 1:ac48b187213c 151 return 0xc0 + column;
simon 1:ac48b187213c 152 case 2:
simon 1:ac48b187213c 153 return 0x94 + column;
simon 1:ac48b187213c 154 case 3:
simon 1:ac48b187213c 155 return 0xd4 + column;
simon 1:ac48b187213c 156 }
simon 1:ac48b187213c 157 case LCD16x2B:
simon 4:bf5b706f8d32 158 return 0x80 + (row * 40) + column;
simon 1:ac48b187213c 159 case LCD16x2:
simon 1:ac48b187213c 160 case LCD20x2:
simon 1:ac48b187213c 161 default:
simon 4:bf5b706f8d32 162 return 0x80 + (row * 0x40) + column;
simon 1:ac48b187213c 163 }
simon 1:ac48b187213c 164 }
simon 1:ac48b187213c 165
ritarosakai 9:496c520855a1 166 int TextLCD::columns()
ritarosakai 9:496c520855a1 167 {
simon 1:ac48b187213c 168 switch (_type) {
simon 1:ac48b187213c 169 case LCD20x4:
simon 1:ac48b187213c 170 case LCD20x2:
simon 1:ac48b187213c 171 return 20;
simon 1:ac48b187213c 172 case LCD16x2:
simon 1:ac48b187213c 173 case LCD16x2B:
simon 1:ac48b187213c 174 default:
simon 1:ac48b187213c 175 return 16;
simon 1:ac48b187213c 176 }
simon 1:ac48b187213c 177 }
simon 1:ac48b187213c 178
ritarosakai 9:496c520855a1 179 int TextLCD::rows()
ritarosakai 9:496c520855a1 180 {
simon 1:ac48b187213c 181 switch (_type) {
simon 1:ac48b187213c 182 case LCD20x4:
simon 1:ac48b187213c 183 return 4;
simon 1:ac48b187213c 184 case LCD16x2:
simon 1:ac48b187213c 185 case LCD16x2B:
simon 1:ac48b187213c 186 case LCD20x2:
simon 1:ac48b187213c 187 default:
simon 1:ac48b187213c 188 return 2;
simon 1:ac48b187213c 189 }
simon 1:ac48b187213c 190 }