Simon Ford
/
displays
TextLCD.cpp@0:cc002f2fad97, 2009-09-15 (annotated)
- Committer:
- simon
- Date:
- Tue Sep 15 10:02:04 2009 +0000
- Revision:
- 0:cc002f2fad97
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:cc002f2fad97 | 1 | /* mbed TextLCD Library |
simon | 0:cc002f2fad97 | 2 | * Copyright (c) 2007-2009 sford |
simon | 0:cc002f2fad97 | 3 | * Released under the MIT License: http://mbed.org/license/mit |
simon | 0:cc002f2fad97 | 4 | */ |
simon | 0:cc002f2fad97 | 5 | |
simon | 0:cc002f2fad97 | 6 | #include "TextLCD.h" |
simon | 0:cc002f2fad97 | 7 | |
simon | 0:cc002f2fad97 | 8 | #include "mbed.h" |
simon | 0:cc002f2fad97 | 9 | |
simon | 0:cc002f2fad97 | 10 | /* |
simon | 0:cc002f2fad97 | 11 | * useful info found at http://www.a-netz.de/lcd.en.php |
simon | 0:cc002f2fad97 | 12 | * |
simon | 0:cc002f2fad97 | 13 | * Initialisation |
simon | 0:cc002f2fad97 | 14 | * ============== |
simon | 0:cc002f2fad97 | 15 | * |
simon | 0:cc002f2fad97 | 16 | * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state |
simon | 0:cc002f2fad97 | 17 | * |
simon | 0:cc002f2fad97 | 18 | * - wait approximately 15 ms so the display is ready to execute commands |
simon | 0:cc002f2fad97 | 19 | * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now). |
simon | 0:cc002f2fad97 | 20 | * - 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. |
simon | 0:cc002f2fad97 | 21 | * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now. |
simon | 0:cc002f2fad97 | 22 | * - Execute the "clear display" command |
simon | 0:cc002f2fad97 | 23 | * |
simon | 0:cc002f2fad97 | 24 | * Timing |
simon | 0:cc002f2fad97 | 25 | * ====== |
simon | 0:cc002f2fad97 | 26 | * |
simon | 0:cc002f2fad97 | 27 | * Nearly all commands transmitted to the display need 40us for execution. |
simon | 0:cc002f2fad97 | 28 | * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position" |
simon | 0:cc002f2fad97 | 29 | * These commands need 1.64ms for execution. These timings are valid for all displays working with an |
simon | 0:cc002f2fad97 | 30 | * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you |
simon | 0:cc002f2fad97 | 31 | * can use the busy flag to test if the display is ready to accept the next command. |
simon | 0:cc002f2fad97 | 32 | * |
simon | 0:cc002f2fad97 | 33 | * _e is kept high apart from calling clock |
simon | 0:cc002f2fad97 | 34 | * _rw is kept 0 (write) apart from actions that uyse it differently |
simon | 0:cc002f2fad97 | 35 | * _rs is set by the data/command writes |
simon | 0:cc002f2fad97 | 36 | */ |
simon | 0:cc002f2fad97 | 37 | |
simon | 0:cc002f2fad97 | 38 | TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, |
simon | 0:cc002f2fad97 | 39 | PinName d2, PinName d3) : _rw(rw), _rs(rs), |
simon | 0:cc002f2fad97 | 40 | _e(e), _d(d0, d1, d2, d3) { |
simon | 0:cc002f2fad97 | 41 | |
simon | 0:cc002f2fad97 | 42 | _rw = 0; |
simon | 0:cc002f2fad97 | 43 | _e = 1; |
simon | 0:cc002f2fad97 | 44 | _rs = 0; // command mode |
simon | 0:cc002f2fad97 | 45 | |
simon | 0:cc002f2fad97 | 46 | // Should theoretically wait 15ms, but most things will be powered up pre-reset |
simon | 0:cc002f2fad97 | 47 | // so i'll disable that for the minute. If implemented, could wait 15ms post reset |
simon | 0:cc002f2fad97 | 48 | // instead |
simon | 0:cc002f2fad97 | 49 | // wait(0.015); |
simon | 0:cc002f2fad97 | 50 | |
simon | 0:cc002f2fad97 | 51 | // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) |
simon | 0:cc002f2fad97 | 52 | for(int i=0; i<3; i++) { |
simon | 0:cc002f2fad97 | 53 | writeByte(0x3); |
simon | 0:cc002f2fad97 | 54 | wait(0.00164); // this command takes 1.64ms, so wait for it |
simon | 0:cc002f2fad97 | 55 | } |
simon | 0:cc002f2fad97 | 56 | writeByte(0x2); // 4-bit mode |
simon | 0:cc002f2fad97 | 57 | |
simon | 0:cc002f2fad97 | 58 | writeCommand(0x28); // Function set 001 BW N F - - |
simon | 0:cc002f2fad97 | 59 | writeCommand(0x0C); |
simon | 0:cc002f2fad97 | 60 | writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes |
simon | 0:cc002f2fad97 | 61 | cls(); |
simon | 0:cc002f2fad97 | 62 | } |
simon | 0:cc002f2fad97 | 63 | |
simon | 0:cc002f2fad97 | 64 | void TextLCD::character(int column, int row, int c) { |
simon | 0:cc002f2fad97 | 65 | int address = 0x80 + (row * 40) + column; // memory starts at 0x80, and is 40 chars long per row |
simon | 0:cc002f2fad97 | 66 | writeCommand(address); |
simon | 0:cc002f2fad97 | 67 | writeData(c); |
simon | 0:cc002f2fad97 | 68 | } |
simon | 0:cc002f2fad97 | 69 | |
simon | 0:cc002f2fad97 | 70 | int TextLCD::columns() { |
simon | 0:cc002f2fad97 | 71 | return 16; |
simon | 0:cc002f2fad97 | 72 | } |
simon | 0:cc002f2fad97 | 73 | |
simon | 0:cc002f2fad97 | 74 | int TextLCD::rows() { |
simon | 0:cc002f2fad97 | 75 | return 2; |
simon | 0:cc002f2fad97 | 76 | } |
simon | 0:cc002f2fad97 | 77 | |
simon | 0:cc002f2fad97 | 78 | void TextLCD::writeByte(int value) { |
simon | 0:cc002f2fad97 | 79 | _d = value >> 4; |
simon | 0:cc002f2fad97 | 80 | wait(0.000040f); // most instructions take 40us |
simon | 0:cc002f2fad97 | 81 | _e = 0; |
simon | 0:cc002f2fad97 | 82 | wait(0.000040f); |
simon | 0:cc002f2fad97 | 83 | _e = 1; |
simon | 0:cc002f2fad97 | 84 | _d = value >> 0; |
simon | 0:cc002f2fad97 | 85 | wait(0.000040f); |
simon | 0:cc002f2fad97 | 86 | _e = 0; |
simon | 0:cc002f2fad97 | 87 | wait(0.000040f); // most instructions take 40us |
simon | 0:cc002f2fad97 | 88 | _e = 1; |
simon | 0:cc002f2fad97 | 89 | } |
simon | 0:cc002f2fad97 | 90 | |
simon | 0:cc002f2fad97 | 91 | void TextLCD::writeCommand(int command) { |
simon | 0:cc002f2fad97 | 92 | _rs = 0; |
simon | 0:cc002f2fad97 | 93 | writeByte(command); |
simon | 0:cc002f2fad97 | 94 | } |
simon | 0:cc002f2fad97 | 95 | |
simon | 0:cc002f2fad97 | 96 | void TextLCD::writeData(int data) { |
simon | 0:cc002f2fad97 | 97 | _rs = 1; |
simon | 0:cc002f2fad97 | 98 | writeByte(data); |
simon | 0:cc002f2fad97 | 99 | } |