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