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