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