Dependents:   eParking

Committer:
satriabtp
Date:
Wed Jun 13 08:51:44 2012 +0000
Revision:
0:dabc223d3e03

        

Who changed what in which revision?

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