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