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