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