16x1 panels are actually handled as 8x2 panels. To handle them as named I added a new type LCD16x1

Dependencies:   mbed

Committer:
tlunzer
Date:
Tue May 24 04:46:36 2011 +0000
Revision:
0:7781fb254c42
Added Support for 16x1 LCD panels

Who changed what in which revision?

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