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