平成24年中国四国技術職員研修用

Dependencies:   MSCUsbHost NetServices RPCInterface TextLCD mbed

Committer:
yueee_yt
Date:
Sat Aug 18 07:26:48 2012 +0000
Revision:
0:fd83f3540b1a
NTP????????

Who changed what in which revision?

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