MDX-20用の改造

Committer:
suupen
Date:
Sun Dec 18 11:39:04 2016 +0000
Revision:
9:5af2538e7684
Parent:
7:44f34c09bd37
SC2004CSLB???????1charactor?????(cls())??initialize???????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 1:ac48b187213c 1 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
simon 6:e4cb7ddee0d3 2 * Copyright (c) 2007-2010, sford, http://mbed.org
simon 1:ac48b187213c 3 *
simon 1:ac48b187213c 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 1:ac48b187213c 5 * of this software and associated documentation files (the "Software"), to deal
simon 1:ac48b187213c 6 * in the Software without restriction, including without limitation the rights
simon 1:ac48b187213c 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 1:ac48b187213c 8 * copies of the Software, and to permit persons to whom the Software is
simon 1:ac48b187213c 9 * furnished to do so, subject to the following conditions:
simon 1:ac48b187213c 10 *
simon 1:ac48b187213c 11 * The above copyright notice and this permission notice shall be included in
simon 1:ac48b187213c 12 * all copies or substantial portions of the Software.
simon 1:ac48b187213c 13 *
simon 1:ac48b187213c 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 1:ac48b187213c 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 1:ac48b187213c 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 1:ac48b187213c 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 1:ac48b187213c 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 1:ac48b187213c 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 1:ac48b187213c 20 * THE SOFTWARE.
simon 1:ac48b187213c 21 */
simon 1:ac48b187213c 22
simon 1:ac48b187213c 23 #include "TextLCD.h"
simon 1:ac48b187213c 24 #include "mbed.h"
simon 1:ac48b187213c 25
simon 7:44f34c09bd37 26 TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5,
simon 7:44f34c09bd37 27 PinName d6, PinName d7, LCDType type) : _rs(rs),
simon 7:44f34c09bd37 28 _e(e), _d(d4, d5, d6, d7),
simon 1:ac48b187213c 29 _type(type) {
simon 1:ac48b187213c 30
simon 1:ac48b187213c 31 _e = 1;
simon 1:ac48b187213c 32 _rs = 0; // command mode
simon 1:ac48b187213c 33
suupen 9:5af2538e7684 34 wait(0.040); // Wait 40ms to ensure powered up
simon 1:ac48b187213c 35
simon 1:ac48b187213c 36 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
suupen 9:5af2538e7684 37 for (int i=0; i<4; i++) {
simon 1:ac48b187213c 38 writeByte(0x3);
suupen 9:5af2538e7684 39 wait(0.00410f); // this command takes 4.10ms, so wait for it
simon 1:ac48b187213c 40 }
simon 1:ac48b187213c 41 writeByte(0x2); // 4-bit mode
simon 1:ac48b187213c 42 wait(0.000040f); // most instructions take 40us
simon 1:ac48b187213c 43
simon 1:ac48b187213c 44 writeCommand(0x28); // Function set 001 BW N F - -
suupen 9:5af2538e7684 45
suupen 9:5af2538e7684 46 writeCommand(0x08); // Display OFF
suupen 9:5af2538e7684 47 writeCommand(0x01); // Display ON
suupen 9:5af2538e7684 48
suupen 9:5af2538e7684 49 writeCommand(0x0C); // Display ON/OFF Display On, Cursor OFF, Blink OFF
simon 1:ac48b187213c 50 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
suupen 9:5af2538e7684 51
suupen 9:5af2538e7684 52 // writeCommand(0x14); // Cursor or Display Shift Cursor movement, Shift to the right
suupen 9:5af2538e7684 53
simon 1:ac48b187213c 54 cls();
simon 1:ac48b187213c 55 }
simon 1:ac48b187213c 56
simon 1:ac48b187213c 57 void TextLCD::character(int column, int row, int c) {
simon 1:ac48b187213c 58 int a = address(column, row);
simon 1:ac48b187213c 59 writeCommand(a);
simon 1:ac48b187213c 60 writeData(c);
simon 1:ac48b187213c 61 }
simon 1:ac48b187213c 62
simon 1:ac48b187213c 63 void TextLCD::cls() {
simon 1:ac48b187213c 64 writeCommand(0x01); // cls, and set cursor to 0
simon 1:ac48b187213c 65 wait(0.00164f); // This command takes 1.64 ms
simon 1:ac48b187213c 66 locate(0, 0);
simon 1:ac48b187213c 67 }
simon 1:ac48b187213c 68
suupen 9:5af2538e7684 69 void TextLCD::displayONOFF(int d, int c, int b){
suupen 9:5af2538e7684 70 int a = (0x08 | (d & 0x01) << 2 | (c & 0x01) << 1 | (b & 0x01));
suupen 9:5af2538e7684 71 writeCommand(a); // Instruction Display On/OFF
suupen 9:5af2538e7684 72 wait(0.000040f); // This command takes 40us (necessary >37us)
suupen 9:5af2538e7684 73
suupen 9:5af2538e7684 74 }
suupen 9:5af2538e7684 75
simon 1:ac48b187213c 76 void TextLCD::locate(int column, int row) {
suupen 9:5af2538e7684 77 writeCommand(0x02); // set cursor to 0 // 1charactor ずれずれるので応急対応
suupen 9:5af2538e7684 78 wait(0.00164f); // This command takes 1.64 ms
suupen 9:5af2538e7684 79
simon 1:ac48b187213c 80 _column = column;
simon 1:ac48b187213c 81 _row = row;
simon 1:ac48b187213c 82 }
simon 1:ac48b187213c 83
simon 1:ac48b187213c 84 int TextLCD::_putc(int value) {
simon 1:ac48b187213c 85 if (value == '\n') {
simon 1:ac48b187213c 86 _column = 0;
simon 1:ac48b187213c 87 _row++;
simon 1:ac48b187213c 88 if (_row >= rows()) {
simon 1:ac48b187213c 89 _row = 0;
simon 1:ac48b187213c 90 }
simon 1:ac48b187213c 91 } else {
simon 1:ac48b187213c 92 character(_column, _row, value);
simon 1:ac48b187213c 93 _column++;
simon 1:ac48b187213c 94 if (_column >= columns()) {
simon 1:ac48b187213c 95 _column = 0;
simon 1:ac48b187213c 96 _row++;
simon 1:ac48b187213c 97 if (_row >= rows()) {
simon 1:ac48b187213c 98 _row = 0;
simon 1:ac48b187213c 99 }
simon 1:ac48b187213c 100 }
simon 1:ac48b187213c 101 }
simon 1:ac48b187213c 102 return value;
simon 1:ac48b187213c 103 }
simon 1:ac48b187213c 104
simon 1:ac48b187213c 105 int TextLCD::_getc() {
simon 1:ac48b187213c 106 return -1;
simon 1:ac48b187213c 107 }
simon 1:ac48b187213c 108
simon 1:ac48b187213c 109 void TextLCD::writeByte(int value) {
simon 1:ac48b187213c 110 _d = value >> 4;
simon 1:ac48b187213c 111 wait(0.000040f); // most instructions take 40us
simon 1:ac48b187213c 112 _e = 0;
simon 1:ac48b187213c 113 wait(0.000040f);
simon 1:ac48b187213c 114 _e = 1;
simon 1:ac48b187213c 115 _d = value >> 0;
simon 1:ac48b187213c 116 wait(0.000040f);
simon 1:ac48b187213c 117 _e = 0;
simon 1:ac48b187213c 118 wait(0.000040f); // most instructions take 40us
simon 1:ac48b187213c 119 _e = 1;
simon 1:ac48b187213c 120 }
simon 1:ac48b187213c 121
simon 1:ac48b187213c 122 void TextLCD::writeCommand(int command) {
simon 1:ac48b187213c 123 _rs = 0;
simon 1:ac48b187213c 124 writeByte(command);
suupen 9:5af2538e7684 125 wait(0.000040f); // most instructions take 40us
suupen 9:5af2538e7684 126
simon 1:ac48b187213c 127 }
simon 1:ac48b187213c 128
simon 1:ac48b187213c 129 void TextLCD::writeData(int data) {
simon 1:ac48b187213c 130 _rs = 1;
simon 1:ac48b187213c 131 writeByte(data);
simon 1:ac48b187213c 132 }
simon 1:ac48b187213c 133
simon 1:ac48b187213c 134 int TextLCD::address(int column, int row) {
simon 1:ac48b187213c 135 switch (_type) {
simon 1:ac48b187213c 136 case LCD20x4:
simon 1:ac48b187213c 137 switch (row) {
suupen 9:5af2538e7684 138
simon 1:ac48b187213c 139 case 0:
simon 1:ac48b187213c 140 return 0x80 + column;
simon 1:ac48b187213c 141 case 1:
simon 1:ac48b187213c 142 return 0xc0 + column;
simon 1:ac48b187213c 143 case 2:
simon 1:ac48b187213c 144 return 0x94 + column;
simon 1:ac48b187213c 145 case 3:
simon 1:ac48b187213c 146 return 0xd4 + column;
suupen 9:5af2538e7684 147
simon 1:ac48b187213c 148 }
simon 1:ac48b187213c 149 case LCD16x2B:
simon 4:bf5b706f8d32 150 return 0x80 + (row * 40) + column;
simon 1:ac48b187213c 151 case LCD16x2:
simon 1:ac48b187213c 152 case LCD20x2:
simon 1:ac48b187213c 153 default:
simon 4:bf5b706f8d32 154 return 0x80 + (row * 0x40) + column;
simon 1:ac48b187213c 155 }
simon 1:ac48b187213c 156 }
simon 1:ac48b187213c 157
simon 1:ac48b187213c 158 int TextLCD::columns() {
simon 1:ac48b187213c 159 switch (_type) {
simon 1:ac48b187213c 160 case LCD20x4:
simon 1:ac48b187213c 161 case LCD20x2:
simon 1:ac48b187213c 162 return 20;
simon 1:ac48b187213c 163 case LCD16x2:
simon 1:ac48b187213c 164 case LCD16x2B:
simon 1:ac48b187213c 165 default:
simon 1:ac48b187213c 166 return 16;
simon 1:ac48b187213c 167 }
simon 1:ac48b187213c 168 }
simon 1:ac48b187213c 169
simon 1:ac48b187213c 170 int TextLCD::rows() {
simon 1:ac48b187213c 171 switch (_type) {
simon 1:ac48b187213c 172 case LCD20x4:
simon 1:ac48b187213c 173 return 4;
simon 1:ac48b187213c 174 case LCD16x2:
simon 1:ac48b187213c 175 case LCD16x2B:
simon 1:ac48b187213c 176 case LCD20x2:
simon 1:ac48b187213c 177 default:
simon 1:ac48b187213c 178 return 2;
simon 1:ac48b187213c 179 }
simon 1:ac48b187213c 180 }