web server load-drive and reading sensors based on LPC1768

Dependencies:   DHT22 EthernetNetIf HTTPServer mbed

Committer:
douglasofranco
Date:
Mon Dec 07 14:06:24 2015 +0000
Revision:
0:3125f39d20b4
Web server load-drive and reading sensors based on LPC1768

Who changed what in which revision?

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