testing of combination of LCS and LAN

Dependencies:   mbed

Committer:
damir
Date:
Wed Jan 14 13:29:55 2015 +0000
Revision:
0:a7a6a692162f
Test of LAN

Who changed what in which revision?

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