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