Wireless temperature transmitter with ER400TRS transceiver and MCP9700 temperature sensor.

Dependencies:   mbed

Committer:
lnadal
Date:
Wed Aug 31 04:08:11 2011 +0000
Revision:
0:407b38152be7

        

Who changed what in which revision?

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