Fork tetxldc

Fork of TextLCD_piano by Adeline Galasso

Committer:
aknin001
Date:
Tue Jul 03 02:04:42 2018 +0000
Revision:
10:4276b56b4400
Parent:
9:05c9b97e4a91
piano with 4  octaves and 8 notes, bug display notes

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 */
Aliened 9:05c9b97e4a91 22
simon 1:ac48b187213c 23 #include "TextLCD.h"
simon 1:ac48b187213c 24 #include "mbed.h"
Aliened 9:05c9b97e4a91 25
Aliened 9:05c9b97e4a91 26 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1,
Aliened 9:05c9b97e4a91 27 PinName d2, PinName d3, LCDType type) : _rs(rs), _rw(rw),
Aliened 9:05c9b97e4a91 28 _e(e), _d(d0, d1, d2, d3), _type(type) {
simon 1:ac48b187213c 29 _rs = 0; // command mode
Aliened 9:05c9b97e4a91 30 _rw = 0;
Aliened 9:05c9b97e4a91 31 _e = 0;
Aliened 9:05c9b97e4a91 32 _d.output(); // data out
Aliened 9:05c9b97e4a91 33
Aliened 9:05c9b97e4a91 34 wait(0.05); // Wait 50ms to ensure powered up
Aliened 9:05c9b97e4a91 35
simon 1:ac48b187213c 36 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
simon 1:ac48b187213c 37 for (int i=0; i<3; i++) {
Aliened 9:05c9b97e4a91 38 _e = 1;
Aliened 9:05c9b97e4a91 39 wait(0.000001f);
Aliened 9:05c9b97e4a91 40 _d = 0x3;
Aliened 9:05c9b97e4a91 41 wait(0.000001f);
Aliened 9:05c9b97e4a91 42 _e = 0;
Aliened 9:05c9b97e4a91 43 wait(0.004f); // 4ms
Aliened 9:05c9b97e4a91 44 }
Aliened 9:05c9b97e4a91 45 _e = 1;
Aliened 9:05c9b97e4a91 46 wait(0.000001f);
Aliened 9:05c9b97e4a91 47 _d = 0x2; // 4 Bit mode
Aliened 9:05c9b97e4a91 48 wait(0.000001f);
Aliened 9:05c9b97e4a91 49 _e = 0;
Aliened 9:05c9b97e4a91 50
Aliened 9:05c9b97e4a91 51 writeCommand(0x28); // Function set 4 Bit, 2Line, 5*7
Aliened 9:05c9b97e4a91 52 writeCommand(0x08); // Display off
Aliened 9:05c9b97e4a91 53 writeCommand(0x01); // clear Display
Aliened 9:05c9b97e4a91 54 writeCommand(0x04); // cursor right, Display is not shifted
Aliened 9:05c9b97e4a91 55 writeCommand(0x0C); // Display on , Cursor off
simon 1:ac48b187213c 56 }
Aliened 9:05c9b97e4a91 57
simon 1:ac48b187213c 58 void TextLCD::character(int column, int row, int c) {
simon 1:ac48b187213c 59 int a = address(column, row);
simon 1:ac48b187213c 60 writeCommand(a);
simon 1:ac48b187213c 61 writeData(c);
simon 1:ac48b187213c 62 }
Aliened 9:05c9b97e4a91 63
simon 1:ac48b187213c 64 void TextLCD::cls() {
simon 1:ac48b187213c 65 writeCommand(0x01); // cls, and set cursor to 0
aknin001 10:4276b56b4400 66 wait(0.00164f);
simon 1:ac48b187213c 67 locate(0, 0);
simon 1:ac48b187213c 68 }
Aliened 9:05c9b97e4a91 69
simon 1:ac48b187213c 70 void TextLCD::locate(int column, int row) {
simon 1:ac48b187213c 71 _column = column;
simon 1:ac48b187213c 72 _row = row;
simon 1:ac48b187213c 73 }
Aliened 9:05c9b97e4a91 74
aknin001 10:4276b56b4400 75 /**int TextLCD::putc(int values) {
aknin001 10:4276b56b4400 76 return(_putc(values));
aknin001 10:4276b56b4400 77 }**/
Aliened 9:05c9b97e4a91 78
simon 1:ac48b187213c 79 int TextLCD::_putc(int value) {
simon 1:ac48b187213c 80 if (value == '\n') {
simon 1:ac48b187213c 81 _column = 0;
simon 1:ac48b187213c 82 _row++;
simon 1:ac48b187213c 83 if (_row >= rows()) {
simon 1:ac48b187213c 84 _row = 0;
simon 1:ac48b187213c 85 }
simon 1:ac48b187213c 86 } else {
simon 1:ac48b187213c 87 character(_column, _row, value);
simon 1:ac48b187213c 88 _column++;
simon 1:ac48b187213c 89 if (_column >= columns()) {
simon 1:ac48b187213c 90 _column = 0;
simon 1:ac48b187213c 91 _row++;
simon 1:ac48b187213c 92 if (_row >= rows()) {
simon 1:ac48b187213c 93 _row = 0;
simon 1:ac48b187213c 94 }
simon 1:ac48b187213c 95 }
simon 1:ac48b187213c 96 }
simon 1:ac48b187213c 97 return value;
simon 1:ac48b187213c 98 }
Aliened 9:05c9b97e4a91 99
simon 1:ac48b187213c 100 int TextLCD::_getc() {
Aliened 9:05c9b97e4a91 101 int a = address(_column, _row);
Aliened 9:05c9b97e4a91 102 writeCommand(a);
Aliened 9:05c9b97e4a91 103 return (readData());
simon 1:ac48b187213c 104 }
Aliened 9:05c9b97e4a91 105
simon 1:ac48b187213c 106 void TextLCD::writeByte(int value) {
aknin001 10:4276b56b4400 107 _e = 1;
aknin001 10:4276b56b4400 108 wait(0.000040f);
simon 1:ac48b187213c 109 _d = value >> 4;
aknin001 10:4276b56b4400 110 wait(0.000040f);
simon 1:ac48b187213c 111 _e = 0;
aknin001 10:4276b56b4400 112 wait(0.000040f);
simon 1:ac48b187213c 113 _e = 1;
aknin001 10:4276b56b4400 114 wait(0.000040f);
simon 1:ac48b187213c 115 _d = value >> 0;
aknin001 10:4276b56b4400 116 wait(0.000040f);
simon 1:ac48b187213c 117 _e = 0;
simon 1:ac48b187213c 118 }
Aliened 9:05c9b97e4a91 119
simon 1:ac48b187213c 120 void TextLCD::writeCommand(int command) {
Aliened 9:05c9b97e4a91 121 waitBusy(); // check if display is ready
simon 1:ac48b187213c 122 _rs = 0;
simon 1:ac48b187213c 123 writeByte(command);
simon 1:ac48b187213c 124 }
Aliened 9:05c9b97e4a91 125
Aliened 9:05c9b97e4a91 126 int TextLCD::readData(){
Aliened 9:05c9b97e4a91 127 int input;
Aliened 9:05c9b97e4a91 128 waitBusy();
Aliened 9:05c9b97e4a91 129 _rw = 1;
Aliened 9:05c9b97e4a91 130 _rs = 1;
Aliened 9:05c9b97e4a91 131 wait(0.000001f);
Aliened 9:05c9b97e4a91 132 _d.input(); // switch Data port to input
Aliened 9:05c9b97e4a91 133 _e = 1;
Aliened 9:05c9b97e4a91 134 wait(0.000001f);
Aliened 9:05c9b97e4a91 135 input = _d.read() << 4; // high nibble
Aliened 9:05c9b97e4a91 136 _e = 0;
Aliened 9:05c9b97e4a91 137 wait(0.000001f);
Aliened 9:05c9b97e4a91 138 _e = 1;
Aliened 9:05c9b97e4a91 139 wait(0.000001f);
Aliened 9:05c9b97e4a91 140 input = input | _d.read(); // low nibble
Aliened 9:05c9b97e4a91 141 _e = 0;
Aliened 9:05c9b97e4a91 142 return (input);
Aliened 9:05c9b97e4a91 143 }
Aliened 9:05c9b97e4a91 144
Aliened 9:05c9b97e4a91 145 void TextLCD::waitBusy(){
Aliened 9:05c9b97e4a91 146 int input;
Aliened 9:05c9b97e4a91 147 _rw = 1;
Aliened 9:05c9b97e4a91 148 _rs = 0;
Aliened 9:05c9b97e4a91 149 wait(0.000001f);
Aliened 9:05c9b97e4a91 150 _d.input(); // switch Data port to input
Aliened 9:05c9b97e4a91 151 do{
Aliened 9:05c9b97e4a91 152 _e = 1;
Aliened 9:05c9b97e4a91 153 wait(0.000001f);
Aliened 9:05c9b97e4a91 154 input = _d.read();
Aliened 9:05c9b97e4a91 155 _e = 0;
Aliened 9:05c9b97e4a91 156 wait(0.000001f);
Aliened 9:05c9b97e4a91 157 _e = 1;
Aliened 9:05c9b97e4a91 158 wait(0.000001f);
Aliened 9:05c9b97e4a91 159 _e = 0;
Aliened 9:05c9b97e4a91 160 }while((0x8 & input) == 0x8); // wait until display is ready
Aliened 9:05c9b97e4a91 161 _rw = 0;
Aliened 9:05c9b97e4a91 162 _d.output(); // switch port back to output
Aliened 9:05c9b97e4a91 163 }
Aliened 9:05c9b97e4a91 164
simon 1:ac48b187213c 165 void TextLCD::writeData(int data) {
Aliened 9:05c9b97e4a91 166 waitBusy();
simon 1:ac48b187213c 167 _rs = 1;
simon 1:ac48b187213c 168 writeByte(data);
simon 1:ac48b187213c 169 }
Aliened 9:05c9b97e4a91 170
Aliened 9:05c9b97e4a91 171
Aliened 9:05c9b97e4a91 172 // set user defined char
Aliened 9:05c9b97e4a91 173 void TextLCD::writeCGRAM(int address, int pattern[8]){
Aliened 9:05c9b97e4a91 174 int i;
Aliened 9:05c9b97e4a91 175 address = address & 0x07; //max 8 char
Aliened 9:05c9b97e4a91 176 for(i=0;i<8;i++){
Aliened 9:05c9b97e4a91 177 waitBusy(); // check if display is ready
Aliened 9:05c9b97e4a91 178 _rs = 0;
Aliened 9:05c9b97e4a91 179 writeByte(0x40 + address * 8 + i);
Aliened 9:05c9b97e4a91 180 writeData(pattern[i]);
Aliened 9:05c9b97e4a91 181 }
Aliened 9:05c9b97e4a91 182 }
Aliened 9:05c9b97e4a91 183
Aliened 9:05c9b97e4a91 184
simon 1:ac48b187213c 185 int TextLCD::address(int column, int row) {
simon 1:ac48b187213c 186 switch (_type) {
simon 1:ac48b187213c 187 case LCD20x4:
simon 1:ac48b187213c 188 switch (row) {
simon 1:ac48b187213c 189 case 0:
simon 1:ac48b187213c 190 return 0x80 + column;
simon 1:ac48b187213c 191 case 1:
simon 1:ac48b187213c 192 return 0xc0 + column;
simon 1:ac48b187213c 193 case 2:
simon 1:ac48b187213c 194 return 0x94 + column;
simon 1:ac48b187213c 195 case 3:
simon 1:ac48b187213c 196 return 0xd4 + column;
simon 1:ac48b187213c 197 }
simon 1:ac48b187213c 198 case LCD16x2B:
simon 4:bf5b706f8d32 199 return 0x80 + (row * 40) + column;
simon 1:ac48b187213c 200 case LCD16x2:
simon 1:ac48b187213c 201 case LCD20x2:
simon 1:ac48b187213c 202 default:
simon 4:bf5b706f8d32 203 return 0x80 + (row * 0x40) + column;
simon 1:ac48b187213c 204 }
simon 1:ac48b187213c 205 }
Aliened 9:05c9b97e4a91 206
simon 1:ac48b187213c 207 int TextLCD::columns() {
simon 1:ac48b187213c 208 switch (_type) {
simon 1:ac48b187213c 209 case LCD20x4:
simon 1:ac48b187213c 210 case LCD20x2:
simon 1:ac48b187213c 211 return 20;
simon 1:ac48b187213c 212 case LCD16x2:
simon 1:ac48b187213c 213 case LCD16x2B:
simon 1:ac48b187213c 214 default:
simon 1:ac48b187213c 215 return 16;
simon 1:ac48b187213c 216 }
simon 1:ac48b187213c 217 }
Aliened 9:05c9b97e4a91 218
simon 1:ac48b187213c 219 int TextLCD::rows() {
simon 1:ac48b187213c 220 switch (_type) {
simon 1:ac48b187213c 221 case LCD20x4:
simon 1:ac48b187213c 222 return 4;
simon 1:ac48b187213c 223 case LCD16x2:
simon 1:ac48b187213c 224 case LCD16x2B:
simon 1:ac48b187213c 225 case LCD20x2:
simon 1:ac48b187213c 226 default:
simon 1:ac48b187213c 227 return 2;
simon 1:ac48b187213c 228 }
Aliened 9:05c9b97e4a91 229 }