Slight modifications to original for different LCD and mbed pin outs

Dependencies:   mbed

Fork of GT_Tuner by Andrew Durand

Committer:
mptapton
Date:
Fri Dec 16 10:15:01 2016 +0000
Revision:
2:0b7bf57470c4
Original code modified for a different LCD and mbed pin out. Additional output to LCD for real time input signal value

Who changed what in which revision?

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