joseph adamu / Mbed OS CW_final_thr

Fork of CW_copy by Calvin Kalintra

Committer:
joseph_adamu
Date:
Wed Jan 10 09:50:29 2018 +0000
Revision:
1:dc648c5624b9
version for export

Who changed what in which revision?

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