Lab Checkoff

Dependencies:   SDFileSystem TextLCD mbed-rtos mbed wave_player FATFileSystem

Committer:
doubster
Date:
Wed Nov 13 20:00:28 2013 +0000
Revision:
0:67dbd54e60d4
Lab Checkoff

Who changed what in which revision?

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