Robot Security System

Dependencies:   Motordriver SHTx mbed

Committer:
dmehta39
Date:
Fri Oct 12 05:21:40 2012 +0000
Revision:
0:b6d5c2bbe0a8
Robot Security System

Who changed what in which revision?

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