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