LCD library files

Dependents:   ELEC350_Project2

Committer:
Swabey89
Date:
Wed Jan 02 19:45:21 2019 +0000
Revision:
6:a0b47f581db9
Parent:
0:e3b7e3cfb264
Updated to use queues more

Who changed what in which revision?

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