Final Report

Dependencies:   mbed

Fork of ecu_reader by Sukkin Pang

Committer:
LAvtec818
Date:
Mon Oct 27 02:46:55 2014 +0000
Revision:
6:dae9630af6e3
report

Who changed what in which revision?

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