Dependencies: ChaNFSSD mbed BMP085 SHT2x
TextLCD_20X4.cpp@0:6089ae824f06, 2011-12-26 (annotated)
- Committer:
- tosihisa
- Date:
- Mon Dec 26 15:58:32 2011 +0000
- Revision:
- 0:6089ae824f06
1st Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tosihisa | 0:6089ae824f06 | 1 | /* mbed TextLCD Library |
tosihisa | 0:6089ae824f06 | 2 | * Copyright (c) 2007-2009 sford |
tosihisa | 0:6089ae824f06 | 3 | * Released under the MIT License: http://mbed.org/license/mit |
tosihisa | 0:6089ae824f06 | 4 | * |
tosihisa | 0:6089ae824f06 | 5 | * TODO: Needs serious rework/neatening up! |
tosihisa | 0:6089ae824f06 | 6 | */ |
tosihisa | 0:6089ae824f06 | 7 | |
tosihisa | 0:6089ae824f06 | 8 | /* |
tosihisa | 0:6089ae824f06 | 9 | * 2010/05/14 modified for 20X4 LCD by ym1784 |
tosihisa | 0:6089ae824f06 | 10 | */ |
tosihisa | 0:6089ae824f06 | 11 | |
tosihisa | 0:6089ae824f06 | 12 | #include "TextLCD_20X4.h" |
tosihisa | 0:6089ae824f06 | 13 | |
tosihisa | 0:6089ae824f06 | 14 | #include "mbed.h" |
tosihisa | 0:6089ae824f06 | 15 | #include "error.h" |
tosihisa | 0:6089ae824f06 | 16 | |
tosihisa | 0:6089ae824f06 | 17 | using namespace mbed; |
tosihisa | 0:6089ae824f06 | 18 | |
tosihisa | 0:6089ae824f06 | 19 | /* |
tosihisa | 0:6089ae824f06 | 20 | * useful info found at http://www.a-netz.de/lcd.en.php |
tosihisa | 0:6089ae824f06 | 21 | * |
tosihisa | 0:6089ae824f06 | 22 | * |
tosihisa | 0:6089ae824f06 | 23 | * Initialization |
tosihisa | 0:6089ae824f06 | 24 | * ============== |
tosihisa | 0:6089ae824f06 | 25 | * |
tosihisa | 0:6089ae824f06 | 26 | * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state |
tosihisa | 0:6089ae824f06 | 27 | * |
tosihisa | 0:6089ae824f06 | 28 | * - wait approximately 15 ms so the display is ready to execute commands |
tosihisa | 0:6089ae824f06 | 29 | * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now). |
tosihisa | 0:6089ae824f06 | 30 | * - The display is in 8 bit mode, so if you have only connected 4 data pins you should only transmit the higher nibble of each command. |
tosihisa | 0:6089ae824f06 | 31 | * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now. |
tosihisa | 0:6089ae824f06 | 32 | * - Execute the "clear display" command |
tosihisa | 0:6089ae824f06 | 33 | * |
tosihisa | 0:6089ae824f06 | 34 | * Timing |
tosihisa | 0:6089ae824f06 | 35 | * ====== |
tosihisa | 0:6089ae824f06 | 36 | * |
tosihisa | 0:6089ae824f06 | 37 | * Nearly all commands transmitted to the display need 40us for execution. |
tosihisa | 0:6089ae824f06 | 38 | * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position" |
tosihisa | 0:6089ae824f06 | 39 | * These commands need 1.64ms for execution. These timings are valid for all displays working with an |
tosihisa | 0:6089ae824f06 | 40 | * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you |
tosihisa | 0:6089ae824f06 | 41 | * can use the busy flag to test if the display is ready to accept the next command. |
tosihisa | 0:6089ae824f06 | 42 | * |
tosihisa | 0:6089ae824f06 | 43 | * _e is kept high apart from calling clock |
tosihisa | 0:6089ae824f06 | 44 | * _rw is kept 0 (write) apart from actions that uyse it differently |
tosihisa | 0:6089ae824f06 | 45 | * -> on this program, the _rw is not used ... ym1784 |
tosihisa | 0:6089ae824f06 | 46 | * _rs is set by the data/command writes |
tosihisa | 0:6089ae824f06 | 47 | */ |
tosihisa | 0:6089ae824f06 | 48 | |
tosihisa | 0:6089ae824f06 | 49 | TextLCD_20X4::TextLCD_20X4(PinName rs, PinName e, PinName d0, PinName d1, |
tosihisa | 0:6089ae824f06 | 50 | PinName d2, PinName d3, int columns, int rows) : _rs(rs), _e(e), _d(d0, d1, d2, d3), |
tosihisa | 0:6089ae824f06 | 51 | _columns(columns), _rows(rows) { |
tosihisa | 0:6089ae824f06 | 52 | |
tosihisa | 0:6089ae824f06 | 53 | _rows = 4; |
tosihisa | 0:6089ae824f06 | 54 | _columns = 20; |
tosihisa | 0:6089ae824f06 | 55 | // Mon, 27 Apr 2009 23:32:34 +0200 |
tosihisa | 0:6089ae824f06 | 56 | // Kevin Konradt: |
tosihisa | 0:6089ae824f06 | 57 | // When using a LCD with 1 row x 16 characters |
tosihisa | 0:6089ae824f06 | 58 | // instead of 2x16, try changing _columns to 8. |
tosihisa | 0:6089ae824f06 | 59 | // (display seems to split the 16 characters into |
tosihisa | 0:6089ae824f06 | 60 | // 2 virtual rows with 8 characters each.) |
tosihisa | 0:6089ae824f06 | 61 | // |
tosihisa | 0:6089ae824f06 | 62 | // 2010/05/14 ym1784 |
tosihisa | 0:6089ae824f06 | 63 | // This program is only for 4 rows x 20 characters specific |
tosihisa | 0:6089ae824f06 | 64 | |
tosihisa | 0:6089ae824f06 | 65 | // _rw = 0; // on this program, the _rw is not used ... ym1784 |
tosihisa | 0:6089ae824f06 | 66 | _e = 1; |
tosihisa | 0:6089ae824f06 | 67 | _rs = 0; // command mode |
tosihisa | 0:6089ae824f06 | 68 | |
tosihisa | 0:6089ae824f06 | 69 | // Should theoretically wait 15ms, but most things will be powered up pre-reset |
tosihisa | 0:6089ae824f06 | 70 | // so i'll disable that for the minute. If implemented, could wait 15ms post reset |
tosihisa | 0:6089ae824f06 | 71 | // instead |
tosihisa | 0:6089ae824f06 | 72 | wait(0.015); // for safety ... ym1784 |
tosihisa | 0:6089ae824f06 | 73 | |
tosihisa | 0:6089ae824f06 | 74 | // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) |
tosihisa | 0:6089ae824f06 | 75 | for(int i=0; i<3; i++) { |
tosihisa | 0:6089ae824f06 | 76 | writeNibble(0x3); |
tosihisa | 0:6089ae824f06 | 77 | wait(0.00164); // this command takes 1.64ms, so wait for it |
tosihisa | 0:6089ae824f06 | 78 | } |
tosihisa | 0:6089ae824f06 | 79 | writeNibble(0x2); // 4-bit mode |
tosihisa | 0:6089ae824f06 | 80 | |
tosihisa | 0:6089ae824f06 | 81 | writeCommand(0x28); // Function set 001 BW N F - - |
tosihisa | 0:6089ae824f06 | 82 | writeCommand(0x0C); |
tosihisa | 0:6089ae824f06 | 83 | writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes |
tosihisa | 0:6089ae824f06 | 84 | |
tosihisa | 0:6089ae824f06 | 85 | cls(); |
tosihisa | 0:6089ae824f06 | 86 | } |
tosihisa | 0:6089ae824f06 | 87 | |
tosihisa | 0:6089ae824f06 | 88 | int TextLCD_20X4::_putc(int value) { |
tosihisa | 0:6089ae824f06 | 89 | if(value == '\n') { |
tosihisa | 0:6089ae824f06 | 90 | newline(); |
tosihisa | 0:6089ae824f06 | 91 | } else { |
tosihisa | 0:6089ae824f06 | 92 | writeData(value); |
tosihisa | 0:6089ae824f06 | 93 | } |
tosihisa | 0:6089ae824f06 | 94 | return value; |
tosihisa | 0:6089ae824f06 | 95 | } |
tosihisa | 0:6089ae824f06 | 96 | |
tosihisa | 0:6089ae824f06 | 97 | int TextLCD_20X4::_getc() { |
tosihisa | 0:6089ae824f06 | 98 | return 0; |
tosihisa | 0:6089ae824f06 | 99 | } |
tosihisa | 0:6089ae824f06 | 100 | |
tosihisa | 0:6089ae824f06 | 101 | void TextLCD_20X4::newline() { |
tosihisa | 0:6089ae824f06 | 102 | _column = 0; |
tosihisa | 0:6089ae824f06 | 103 | _row++; |
tosihisa | 0:6089ae824f06 | 104 | if(_row >= _rows) { |
tosihisa | 0:6089ae824f06 | 105 | _row = 0; |
tosihisa | 0:6089ae824f06 | 106 | } |
tosihisa | 0:6089ae824f06 | 107 | locate(_column, _row); |
tosihisa | 0:6089ae824f06 | 108 | } |
tosihisa | 0:6089ae824f06 | 109 | |
tosihisa | 0:6089ae824f06 | 110 | void TextLCD_20X4::locate(int column, int row) { |
tosihisa | 0:6089ae824f06 | 111 | if(column < 0 || column >= _columns || row < 0 || row >= _rows) { |
tosihisa | 0:6089ae824f06 | 112 | error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows); |
tosihisa | 0:6089ae824f06 | 113 | return; |
tosihisa | 0:6089ae824f06 | 114 | } |
tosihisa | 0:6089ae824f06 | 115 | _row = row; |
tosihisa | 0:6089ae824f06 | 116 | _column = column; |
tosihisa | 0:6089ae824f06 | 117 | // modified for 20X4 LCD |
tosihisa | 0:6089ae824f06 | 118 | switch (_row) { |
tosihisa | 0:6089ae824f06 | 119 | case (0) : address = 0x80 + _column; |
tosihisa | 0:6089ae824f06 | 120 | break; |
tosihisa | 0:6089ae824f06 | 121 | case (1) : address = 0xc0 + _column; |
tosihisa | 0:6089ae824f06 | 122 | break; |
tosihisa | 0:6089ae824f06 | 123 | case (2) : address = 0x94 + _column; |
tosihisa | 0:6089ae824f06 | 124 | break; |
tosihisa | 0:6089ae824f06 | 125 | case (3) : address = 0xd4 + _column; |
tosihisa | 0:6089ae824f06 | 126 | break; |
tosihisa | 0:6089ae824f06 | 127 | } // switch |
tosihisa | 0:6089ae824f06 | 128 | writeCommand(address); |
tosihisa | 0:6089ae824f06 | 129 | } |
tosihisa | 0:6089ae824f06 | 130 | |
tosihisa | 0:6089ae824f06 | 131 | void TextLCD_20X4::cls() { |
tosihisa | 0:6089ae824f06 | 132 | writeCommand(0x01); // Clear Display |
tosihisa | 0:6089ae824f06 | 133 | wait(0.00164f); // This command takes 1.64 ms |
tosihisa | 0:6089ae824f06 | 134 | // locate(0, 0); // We don't have to do this here |
tosihisa | 0:6089ae824f06 | 135 | } |
tosihisa | 0:6089ae824f06 | 136 | |
tosihisa | 0:6089ae824f06 | 137 | void TextLCD_20X4::reset() { |
tosihisa | 0:6089ae824f06 | 138 | cls(); |
tosihisa | 0:6089ae824f06 | 139 | } |
tosihisa | 0:6089ae824f06 | 140 | |
tosihisa | 0:6089ae824f06 | 141 | void TextLCD_20X4::clock() { |
tosihisa | 0:6089ae824f06 | 142 | wait(0.000040f); |
tosihisa | 0:6089ae824f06 | 143 | _e = 0; |
tosihisa | 0:6089ae824f06 | 144 | wait(0.000040f); // most instructions take 40us |
tosihisa | 0:6089ae824f06 | 145 | _e = 1; |
tosihisa | 0:6089ae824f06 | 146 | } |
tosihisa | 0:6089ae824f06 | 147 | |
tosihisa | 0:6089ae824f06 | 148 | void TextLCD_20X4::writeNibble(int value) { |
tosihisa | 0:6089ae824f06 | 149 | _d = value; |
tosihisa | 0:6089ae824f06 | 150 | clock(); |
tosihisa | 0:6089ae824f06 | 151 | } |
tosihisa | 0:6089ae824f06 | 152 | |
tosihisa | 0:6089ae824f06 | 153 | void TextLCD_20X4::writeByte(int value) { |
tosihisa | 0:6089ae824f06 | 154 | writeNibble(value >> 4); |
tosihisa | 0:6089ae824f06 | 155 | writeNibble(value >> 0); |
tosihisa | 0:6089ae824f06 | 156 | } |
tosihisa | 0:6089ae824f06 | 157 | |
tosihisa | 0:6089ae824f06 | 158 | void TextLCD_20X4::writeCommand(int command) { |
tosihisa | 0:6089ae824f06 | 159 | _rs = 0; |
tosihisa | 0:6089ae824f06 | 160 | writeByte(command); |
tosihisa | 0:6089ae824f06 | 161 | } |
tosihisa | 0:6089ae824f06 | 162 | |
tosihisa | 0:6089ae824f06 | 163 | void TextLCD_20X4::writeData(int data) { |
tosihisa | 0:6089ae824f06 | 164 | _rs = 1; |
tosihisa | 0:6089ae824f06 | 165 | writeByte(data); |
tosihisa | 0:6089ae824f06 | 166 | _column++; |
tosihisa | 0:6089ae824f06 | 167 | if(_column >= _columns) { |
tosihisa | 0:6089ae824f06 | 168 | newline(); |
tosihisa | 0:6089ae824f06 | 169 | } |
tosihisa | 0:6089ae824f06 | 170 | } |