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