mbed/ARM 活用事例 第2章 キャラクタLCDを極めよう mbedで初めてのマイコン開発 LCDを極める!<4/5> 文字コード表に載っていない文字や記号を登録し、LCDに表示するプログラムです。 自分だけのオリジナル記号を作成し、LCDに表示してみましょう。 http://www.eleki-jack.com/arm/2010/11/mbed-lcd-3.html

Dependencies:   mbed

Committer:
sunifu
Date:
Wed Mar 02 13:38:56 2011 +0000
Revision:
0:e3313a37353c

        

Who changed what in which revision?

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