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