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