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