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