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