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