It is a door opener with mbed and Felica(RFID).

Dependencies:   mbed Servo SDFileSystem

Committer:
ryought
Date:
Tue May 15 07:47:19 2012 +0000
Revision:
6:9fe8caff6142
FINAL

Who changed what in which revision?

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