can't push chnages :(

Fork of FBRDash by Michael Allan

Committer:
intrinseca
Date:
Thu Nov 03 20:38:12 2011 +0000
Revision:
0:1f422ed56e0f
Test Program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
intrinseca 0:1f422ed56e0f 1 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
intrinseca 0:1f422ed56e0f 2 * Copyright (c) 2007-2010, sford, http://mbed.org
intrinseca 0:1f422ed56e0f 3 *
intrinseca 0:1f422ed56e0f 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
intrinseca 0:1f422ed56e0f 5 * of this software and associated documentation files (the "Software"), to deal
intrinseca 0:1f422ed56e0f 6 * in the Software without restriction, including without limitation the rights
intrinseca 0:1f422ed56e0f 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
intrinseca 0:1f422ed56e0f 8 * copies of the Software, and to permit persons to whom the Software is
intrinseca 0:1f422ed56e0f 9 * furnished to do so, subject to the following conditions:
intrinseca 0:1f422ed56e0f 10 *
intrinseca 0:1f422ed56e0f 11 * The above copyright notice and this permission notice shall be included in
intrinseca 0:1f422ed56e0f 12 * all copies or substantial portions of the Software.
intrinseca 0:1f422ed56e0f 13 *
intrinseca 0:1f422ed56e0f 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
intrinseca 0:1f422ed56e0f 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
intrinseca 0:1f422ed56e0f 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
intrinseca 0:1f422ed56e0f 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
intrinseca 0:1f422ed56e0f 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
intrinseca 0:1f422ed56e0f 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
intrinseca 0:1f422ed56e0f 20 * THE SOFTWARE.
intrinseca 0:1f422ed56e0f 21 */
intrinseca 0:1f422ed56e0f 22
intrinseca 0:1f422ed56e0f 23 #include "TextLCD.h"
intrinseca 0:1f422ed56e0f 24 #include "mbed.h"
intrinseca 0:1f422ed56e0f 25
intrinseca 0:1f422ed56e0f 26 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1,
intrinseca 0:1f422ed56e0f 27 PinName d2, PinName d3, LCDType type) : _rs(rs), _rw(rw),
intrinseca 0:1f422ed56e0f 28 _e(e), _d(d0, d1, d2, d3), _type(type) {
intrinseca 0:1f422ed56e0f 29 _rs = 0; // command mode
intrinseca 0:1f422ed56e0f 30 _rw = 0;
intrinseca 0:1f422ed56e0f 31 _e = 0;
intrinseca 0:1f422ed56e0f 32 _d.output(); // data out
intrinseca 0:1f422ed56e0f 33
intrinseca 0:1f422ed56e0f 34 wait(0.05); // Wait 50ms to ensure powered up
intrinseca 0:1f422ed56e0f 35
intrinseca 0:1f422ed56e0f 36 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
intrinseca 0:1f422ed56e0f 37 for (int i=0; i<3; i++) {
intrinseca 0:1f422ed56e0f 38 _e = 1;
intrinseca 0:1f422ed56e0f 39 __nop();
intrinseca 0:1f422ed56e0f 40 _d = 0x3;
intrinseca 0:1f422ed56e0f 41 __nop();
intrinseca 0:1f422ed56e0f 42 _e = 0;
intrinseca 0:1f422ed56e0f 43 wait(0.004f); // 4ms
intrinseca 0:1f422ed56e0f 44 }
intrinseca 0:1f422ed56e0f 45 _e = 1;
intrinseca 0:1f422ed56e0f 46 __nop();
intrinseca 0:1f422ed56e0f 47 _d = 0x2; // 4 Bit mode
intrinseca 0:1f422ed56e0f 48 __nop();
intrinseca 0:1f422ed56e0f 49 _e = 0;
intrinseca 0:1f422ed56e0f 50
intrinseca 0:1f422ed56e0f 51 writeCommand(0x28); // Function set 4 Bit, 2Line, 5*7
intrinseca 0:1f422ed56e0f 52 writeCommand(0x08); // Display off
intrinseca 0:1f422ed56e0f 53 writeCommand(0x01); // clear Display
intrinseca 0:1f422ed56e0f 54 writeCommand(0x04); // cursor right, Display is not shifted
intrinseca 0:1f422ed56e0f 55 writeCommand(0x0C); // Display on , Cursor off
intrinseca 0:1f422ed56e0f 56 }
intrinseca 0:1f422ed56e0f 57
intrinseca 0:1f422ed56e0f 58 void TextLCD::character(int column, int row, int c) {
intrinseca 0:1f422ed56e0f 59 int a = address(column, row);
intrinseca 0:1f422ed56e0f 60 writeCommand(a);
intrinseca 0:1f422ed56e0f 61 writeData(c);
intrinseca 0:1f422ed56e0f 62 }
intrinseca 0:1f422ed56e0f 63
intrinseca 0:1f422ed56e0f 64 void TextLCD::cls() {
intrinseca 0:1f422ed56e0f 65 writeCommand(0x01); // cls, and set cursor to 0
intrinseca 0:1f422ed56e0f 66 locate(0, 0);
intrinseca 0:1f422ed56e0f 67 }
intrinseca 0:1f422ed56e0f 68
intrinseca 0:1f422ed56e0f 69 void TextLCD::locate(int column, int row) {
intrinseca 0:1f422ed56e0f 70 _column = column;
intrinseca 0:1f422ed56e0f 71 _row = row;
intrinseca 0:1f422ed56e0f 72 }
intrinseca 0:1f422ed56e0f 73
intrinseca 0:1f422ed56e0f 74
intrinseca 0:1f422ed56e0f 75
intrinseca 0:1f422ed56e0f 76 int TextLCD::_putc(int value) {
intrinseca 0:1f422ed56e0f 77 if (value == '\n') {
intrinseca 0:1f422ed56e0f 78 _column = 0;
intrinseca 0:1f422ed56e0f 79 _row++;
intrinseca 0:1f422ed56e0f 80 if (_row >= rows()) {
intrinseca 0:1f422ed56e0f 81 _row = 0;
intrinseca 0:1f422ed56e0f 82 }
intrinseca 0:1f422ed56e0f 83 } else {
intrinseca 0:1f422ed56e0f 84 character(_column, _row, value);
intrinseca 0:1f422ed56e0f 85 _column++;
intrinseca 0:1f422ed56e0f 86 if (_column >= columns()) {
intrinseca 0:1f422ed56e0f 87 _column = 0;
intrinseca 0:1f422ed56e0f 88 _row++;
intrinseca 0:1f422ed56e0f 89 if (_row >= rows()) {
intrinseca 0:1f422ed56e0f 90 _row = 0;
intrinseca 0:1f422ed56e0f 91 }
intrinseca 0:1f422ed56e0f 92 }
intrinseca 0:1f422ed56e0f 93 }
intrinseca 0:1f422ed56e0f 94 return value;
intrinseca 0:1f422ed56e0f 95 }
intrinseca 0:1f422ed56e0f 96
intrinseca 0:1f422ed56e0f 97 int TextLCD::_getc() {
intrinseca 0:1f422ed56e0f 98 int a = address(_column, _row);
intrinseca 0:1f422ed56e0f 99 writeCommand(a);
intrinseca 0:1f422ed56e0f 100 return (readData());
intrinseca 0:1f422ed56e0f 101 }
intrinseca 0:1f422ed56e0f 102
intrinseca 0:1f422ed56e0f 103 void TextLCD::writeByte(int value) {
intrinseca 0:1f422ed56e0f 104 _e = 1;
intrinseca 0:1f422ed56e0f 105 __nop();
intrinseca 0:1f422ed56e0f 106 _d = value >> 4;
intrinseca 0:1f422ed56e0f 107 __nop();
intrinseca 0:1f422ed56e0f 108 _e = 0;
intrinseca 0:1f422ed56e0f 109 __nop();
intrinseca 0:1f422ed56e0f 110 _e = 1;
intrinseca 0:1f422ed56e0f 111 __nop();
intrinseca 0:1f422ed56e0f 112 _d = value >> 0;
intrinseca 0:1f422ed56e0f 113 __nop();
intrinseca 0:1f422ed56e0f 114 _e = 0;
intrinseca 0:1f422ed56e0f 115 }
intrinseca 0:1f422ed56e0f 116
intrinseca 0:1f422ed56e0f 117 void TextLCD::writeCommand(int command) {
intrinseca 0:1f422ed56e0f 118 waitBusy(); // check if display is ready
intrinseca 0:1f422ed56e0f 119 _rs = 0;
intrinseca 0:1f422ed56e0f 120 writeByte(command);
intrinseca 0:1f422ed56e0f 121 }
intrinseca 0:1f422ed56e0f 122
intrinseca 0:1f422ed56e0f 123 int TextLCD::readData(){
intrinseca 0:1f422ed56e0f 124 int input;
intrinseca 0:1f422ed56e0f 125 waitBusy();
intrinseca 0:1f422ed56e0f 126 _rw = 1;
intrinseca 0:1f422ed56e0f 127 _rs = 1;
intrinseca 0:1f422ed56e0f 128 __nop();
intrinseca 0:1f422ed56e0f 129 _d.input(); // switch Data port to input
intrinseca 0:1f422ed56e0f 130 _e = 1;
intrinseca 0:1f422ed56e0f 131 __nop();
intrinseca 0:1f422ed56e0f 132 input = _d.read() << 4; // high nibble
intrinseca 0:1f422ed56e0f 133 _e = 0;
intrinseca 0:1f422ed56e0f 134 __nop();
intrinseca 0:1f422ed56e0f 135 _e = 1;
intrinseca 0:1f422ed56e0f 136 __nop();
intrinseca 0:1f422ed56e0f 137 input = input | _d.read(); // low nibble
intrinseca 0:1f422ed56e0f 138 _e = 0;
intrinseca 0:1f422ed56e0f 139 return (input);
intrinseca 0:1f422ed56e0f 140 }
intrinseca 0:1f422ed56e0f 141
intrinseca 0:1f422ed56e0f 142 void TextLCD::waitBusy(){
intrinseca 0:1f422ed56e0f 143 int input;
intrinseca 0:1f422ed56e0f 144 _rw = 1;
intrinseca 0:1f422ed56e0f 145 _rs = 0;
intrinseca 0:1f422ed56e0f 146 __nop();
intrinseca 0:1f422ed56e0f 147 _d.input(); // switch Data port to input
intrinseca 0:1f422ed56e0f 148 do{
intrinseca 0:1f422ed56e0f 149 _e = 1;
intrinseca 0:1f422ed56e0f 150 __nop();
intrinseca 0:1f422ed56e0f 151 input = _d.read();
intrinseca 0:1f422ed56e0f 152 _e = 0;
intrinseca 0:1f422ed56e0f 153 __nop();
intrinseca 0:1f422ed56e0f 154 _e = 1;
intrinseca 0:1f422ed56e0f 155 __nop();
intrinseca 0:1f422ed56e0f 156 _e = 0;
intrinseca 0:1f422ed56e0f 157 }while((0x8 & input) == 0x8); // wait until display is ready
intrinseca 0:1f422ed56e0f 158 _rw = 0;
intrinseca 0:1f422ed56e0f 159 _d.output(); // switch port back to output
intrinseca 0:1f422ed56e0f 160 }
intrinseca 0:1f422ed56e0f 161
intrinseca 0:1f422ed56e0f 162 void TextLCD::writeData(int data) {
intrinseca 0:1f422ed56e0f 163 waitBusy();
intrinseca 0:1f422ed56e0f 164 _rs = 1;
intrinseca 0:1f422ed56e0f 165 writeByte(data);
intrinseca 0:1f422ed56e0f 166 }
intrinseca 0:1f422ed56e0f 167
intrinseca 0:1f422ed56e0f 168
intrinseca 0:1f422ed56e0f 169 // set user defined char
intrinseca 0:1f422ed56e0f 170 void TextLCD::writeCGRAM(int address, int pattern[8]){
intrinseca 0:1f422ed56e0f 171 int i;
intrinseca 0:1f422ed56e0f 172 address = address & 0x07; //max 8 char
intrinseca 0:1f422ed56e0f 173 for(i=0;i<8;i++){
intrinseca 0:1f422ed56e0f 174 waitBusy(); // check if display is ready
intrinseca 0:1f422ed56e0f 175 _rs = 0;
intrinseca 0:1f422ed56e0f 176 writeByte(0x40 + address * 8 + i);
intrinseca 0:1f422ed56e0f 177 writeData(pattern[i]);
intrinseca 0:1f422ed56e0f 178 }
intrinseca 0:1f422ed56e0f 179 }
intrinseca 0:1f422ed56e0f 180
intrinseca 0:1f422ed56e0f 181
intrinseca 0:1f422ed56e0f 182 int TextLCD::address(int column, int row) {
intrinseca 0:1f422ed56e0f 183 switch (_type) {
intrinseca 0:1f422ed56e0f 184 case LCD20x4:
intrinseca 0:1f422ed56e0f 185 switch (row) {
intrinseca 0:1f422ed56e0f 186 case 0:
intrinseca 0:1f422ed56e0f 187 return 0x80 + column;
intrinseca 0:1f422ed56e0f 188 case 1:
intrinseca 0:1f422ed56e0f 189 return 0xc0 + column;
intrinseca 0:1f422ed56e0f 190 case 2:
intrinseca 0:1f422ed56e0f 191 return 0x94 + column;
intrinseca 0:1f422ed56e0f 192 case 3:
intrinseca 0:1f422ed56e0f 193 return 0xd4 + column;
intrinseca 0:1f422ed56e0f 194 }
intrinseca 0:1f422ed56e0f 195 case LCD16x2B:
intrinseca 0:1f422ed56e0f 196 return 0x80 + (row * 40) + column;
intrinseca 0:1f422ed56e0f 197 case LCD16x2:
intrinseca 0:1f422ed56e0f 198 case LCD20x2:
intrinseca 0:1f422ed56e0f 199 default:
intrinseca 0:1f422ed56e0f 200 return 0x80 + (row * 0x40) + column;
intrinseca 0:1f422ed56e0f 201 }
intrinseca 0:1f422ed56e0f 202 }
intrinseca 0:1f422ed56e0f 203
intrinseca 0:1f422ed56e0f 204 int TextLCD::columns() {
intrinseca 0:1f422ed56e0f 205 switch (_type) {
intrinseca 0:1f422ed56e0f 206 case LCD20x4:
intrinseca 0:1f422ed56e0f 207 case LCD20x2:
intrinseca 0:1f422ed56e0f 208 return 20;
intrinseca 0:1f422ed56e0f 209 case LCD16x2:
intrinseca 0:1f422ed56e0f 210 case LCD16x2B:
intrinseca 0:1f422ed56e0f 211 default:
intrinseca 0:1f422ed56e0f 212 return 16;
intrinseca 0:1f422ed56e0f 213 }
intrinseca 0:1f422ed56e0f 214 }
intrinseca 0:1f422ed56e0f 215
intrinseca 0:1f422ed56e0f 216 int TextLCD::rows() {
intrinseca 0:1f422ed56e0f 217 switch (_type) {
intrinseca 0:1f422ed56e0f 218 case LCD20x4:
intrinseca 0:1f422ed56e0f 219 return 4;
intrinseca 0:1f422ed56e0f 220 case LCD16x2:
intrinseca 0:1f422ed56e0f 221 case LCD16x2B:
intrinseca 0:1f422ed56e0f 222 case LCD20x2:
intrinseca 0:1f422ed56e0f 223 default:
intrinseca 0:1f422ed56e0f 224 return 2;
intrinseca 0:1f422ed56e0f 225 }
intrinseca 0:1f422ed56e0f 226 }