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