TextLCD library for controlling various LCD panels based on the HD44780 4-bit interface. adapted for winstar oled display by CJS august 2012. Moved some functions into public class to enable custom character generation from user code.
Dependents: CCRMonitor12_sp07_120ver
Fork of TextLCD by
TextOLED.cpp@11:25a700bce989, 2013-01-14 (annotated)
- Committer:
- pegcjs
- Date:
- Mon Jan 14 12:51:35 2013 +0000
- Revision:
- 11:25a700bce989
- Parent:
- 10:8e272a434581
- Child:
- 12:4d8dcfa5af6e
Finalised version with control of custom chars and direct write to any location
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pegcjs | 11:25a700bce989 | 1 | /* mbed TextLCD Library, for a 4-bit LCD based on HD44780 |
pegcjs | 11:25a700bce989 | 2 | * Copyright (c) 2007-2010, sford, http://mbed.org |
pegcjs | 11:25a700bce989 | 3 | * |
pegcjs | 11:25a700bce989 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
pegcjs | 11:25a700bce989 | 5 | * of this software and associated documentation files (the "Software"), to deal |
pegcjs | 11:25a700bce989 | 6 | * in the Software without restriction, including without limitation the rights |
pegcjs | 11:25a700bce989 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
pegcjs | 11:25a700bce989 | 8 | * copies of the Software, and to permit persons to whom the Software is |
pegcjs | 11:25a700bce989 | 9 | * furnished to do so, subject to the following conditions: |
pegcjs | 11:25a700bce989 | 10 | * |
pegcjs | 11:25a700bce989 | 11 | * The above copyright notice and this permission notice shall be included in |
pegcjs | 11:25a700bce989 | 12 | * all copies or substantial portions of the Software. |
pegcjs | 11:25a700bce989 | 13 | * |
pegcjs | 11:25a700bce989 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
pegcjs | 11:25a700bce989 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
pegcjs | 11:25a700bce989 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
pegcjs | 11:25a700bce989 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
pegcjs | 11:25a700bce989 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
pegcjs | 11:25a700bce989 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
pegcjs | 11:25a700bce989 | 20 | * THE SOFTWARE. |
pegcjs | 11:25a700bce989 | 21 | */ |
pegcjs | 11:25a700bce989 | 22 | |
pegcjs | 11:25a700bce989 | 23 | #include "TextOLED.h" |
pegcjs | 11:25a700bce989 | 24 | #include "mbed.h" |
pegcjs | 11:25a700bce989 | 25 | |
pegcjs | 11:25a700bce989 | 26 | TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5, |
pegcjs | 11:25a700bce989 | 27 | PinName d6, PinName d7, LCDType type) : _rs(rs), |
pegcjs | 11:25a700bce989 | 28 | _e(e), _d(d4, d5, d6, d7), |
pegcjs | 11:25a700bce989 | 29 | _type(type) { |
pegcjs | 11:25a700bce989 | 30 | //edited 06082012 to run winstart oled 16x2 display |
pegcjs | 11:25a700bce989 | 31 | _e = 1; |
pegcjs | 11:25a700bce989 | 32 | _rs = 0; // command mode |
pegcjs | 11:25a700bce989 | 33 | |
pegcjs | 11:25a700bce989 | 34 | wait(0.100); // Wait 100ms to ensure powered up - need this long if run of 3.3V from mbed vout |
pegcjs | 11:25a700bce989 | 35 | |
pegcjs | 11:25a700bce989 | 36 | wait(0.015); //wait `15ms for power up |
pegcjs | 11:25a700bce989 | 37 | |
pegcjs | 11:25a700bce989 | 38 | |
pegcjs | 11:25a700bce989 | 39 | // send 4 bit mode commenad twice |
pegcjs | 11:25a700bce989 | 40 | writeByte(0x2); //set 4 bit mode |
pegcjs | 11:25a700bce989 | 41 | wait(0.00164); |
pegcjs | 11:25a700bce989 | 42 | writeByte(0x2); //set 4 bit mode |
pegcjs | 11:25a700bce989 | 43 | wait(0.00164); |
pegcjs | 11:25a700bce989 | 44 | writeByte(0x2a); //4 bit 2 lines display font table 1 |
pegcjs | 11:25a700bce989 | 45 | |
pegcjs | 11:25a700bce989 | 46 | wait(0.00164); |
pegcjs | 11:25a700bce989 | 47 | writeByte(0x01); // clear screen |
pegcjs | 11:25a700bce989 | 48 | writeByte(0x6); // cursor mode |
pegcjs | 11:25a700bce989 | 49 | writeByte(0xc); // Lcd on |
pegcjs | 11:25a700bce989 | 50 | } |
pegcjs | 11:25a700bce989 | 51 | |
pegcjs | 11:25a700bce989 | 52 | void TextLCD::character(int column, int row, int c) { |
pegcjs | 11:25a700bce989 | 53 | int a = address(column, row); |
pegcjs | 11:25a700bce989 | 54 | writeCommand(a); |
pegcjs | 11:25a700bce989 | 55 | writeData(c); |
pegcjs | 11:25a700bce989 | 56 | } |
pegcjs | 11:25a700bce989 | 57 | |
pegcjs | 11:25a700bce989 | 58 | void TextLCD::cls() { |
pegcjs | 11:25a700bce989 | 59 | writeCommand(0x01); // cls, and set cursor to 0 |
pegcjs | 11:25a700bce989 | 60 | wait(0.00164f); // This command takes 1.64 ms |
pegcjs | 11:25a700bce989 | 61 | locate(0, 0); |
pegcjs | 11:25a700bce989 | 62 | } |
pegcjs | 11:25a700bce989 | 63 | |
pegcjs | 11:25a700bce989 | 64 | void TextLCD::locate(int column, int row) { |
pegcjs | 11:25a700bce989 | 65 | _column = column; |
pegcjs | 11:25a700bce989 | 66 | _row = row; |
pegcjs | 11:25a700bce989 | 67 | } |
pegcjs | 11:25a700bce989 | 68 | |
pegcjs | 11:25a700bce989 | 69 | int TextLCD::_putc(int value) { |
pegcjs | 11:25a700bce989 | 70 | if (value == '\n') { |
pegcjs | 11:25a700bce989 | 71 | _column = 0; |
pegcjs | 11:25a700bce989 | 72 | _row++; |
pegcjs | 11:25a700bce989 | 73 | if (_row >= rows()) { |
pegcjs | 11:25a700bce989 | 74 | _row = 0; |
pegcjs | 11:25a700bce989 | 75 | } |
pegcjs | 11:25a700bce989 | 76 | } else { |
pegcjs | 11:25a700bce989 | 77 | character(_column, _row, value); |
pegcjs | 11:25a700bce989 | 78 | _column++; |
pegcjs | 11:25a700bce989 | 79 | if (_column >= columns()) { |
pegcjs | 11:25a700bce989 | 80 | _column = 0; |
pegcjs | 11:25a700bce989 | 81 | _row++; |
pegcjs | 11:25a700bce989 | 82 | if (_row >= rows()) { |
pegcjs | 11:25a700bce989 | 83 | _row = 0; |
pegcjs | 11:25a700bce989 | 84 | } |
pegcjs | 11:25a700bce989 | 85 | } |
pegcjs | 11:25a700bce989 | 86 | } |
pegcjs | 11:25a700bce989 | 87 | return value; |
pegcjs | 11:25a700bce989 | 88 | } |
pegcjs | 11:25a700bce989 | 89 | |
pegcjs | 11:25a700bce989 | 90 | int TextLCD::_getc() { |
pegcjs | 11:25a700bce989 | 91 | return -1; |
pegcjs | 11:25a700bce989 | 92 | } |
pegcjs | 11:25a700bce989 | 93 | |
pegcjs | 11:25a700bce989 | 94 | void TextLCD::writeByte(int value) { |
pegcjs | 11:25a700bce989 | 95 | _d = value >> 4; |
pegcjs | 11:25a700bce989 | 96 | wait(0.000040f); // most instructions take 40us |
pegcjs | 11:25a700bce989 | 97 | _e = 0; |
pegcjs | 11:25a700bce989 | 98 | wait(0.000040f); |
pegcjs | 11:25a700bce989 | 99 | _e = 1; |
pegcjs | 11:25a700bce989 | 100 | _d = value >> 0; |
pegcjs | 11:25a700bce989 | 101 | wait(0.000040f); |
pegcjs | 11:25a700bce989 | 102 | _e = 0; |
pegcjs | 11:25a700bce989 | 103 | wait(0.000040f); // most instructions take 40us |
pegcjs | 11:25a700bce989 | 104 | _e = 1; |
pegcjs | 11:25a700bce989 | 105 | } |
pegcjs | 11:25a700bce989 | 106 | |
pegcjs | 11:25a700bce989 | 107 | void TextLCD::writeCommand(int command) { |
pegcjs | 11:25a700bce989 | 108 | _rs = 0; |
pegcjs | 11:25a700bce989 | 109 | writeByte(command); |
pegcjs | 11:25a700bce989 | 110 | } |
pegcjs | 11:25a700bce989 | 111 | |
pegcjs | 11:25a700bce989 | 112 | void TextLCD::writeData(int data) { |
pegcjs | 11:25a700bce989 | 113 | _rs = 1; |
pegcjs | 11:25a700bce989 | 114 | writeByte(data); |
pegcjs | 11:25a700bce989 | 115 | } |
pegcjs | 11:25a700bce989 | 116 | |
pegcjs | 11:25a700bce989 | 117 | int TextLCD::address(int column, int row) { |
pegcjs | 11:25a700bce989 | 118 | switch (_type) { |
pegcjs | 11:25a700bce989 | 119 | case LCD20x4: |
pegcjs | 11:25a700bce989 | 120 | switch (row) { |
pegcjs | 11:25a700bce989 | 121 | case 0: |
pegcjs | 11:25a700bce989 | 122 | return 0x80 + column; |
pegcjs | 11:25a700bce989 | 123 | case 1: |
pegcjs | 11:25a700bce989 | 124 | return 0xc0 + column; |
pegcjs | 11:25a700bce989 | 125 | case 2: |
pegcjs | 11:25a700bce989 | 126 | return 0x94 + column; |
pegcjs | 11:25a700bce989 | 127 | case 3: |
pegcjs | 11:25a700bce989 | 128 | return 0xd4 + column; |
pegcjs | 11:25a700bce989 | 129 | } |
pegcjs | 11:25a700bce989 | 130 | case LCD16x2B: |
pegcjs | 11:25a700bce989 | 131 | return 0x80 + (row * 40) + column; |
pegcjs | 11:25a700bce989 | 132 | case LCD16x2: |
pegcjs | 11:25a700bce989 | 133 | case LCD20x2: |
pegcjs | 11:25a700bce989 | 134 | default: |
pegcjs | 11:25a700bce989 | 135 | return 0x80 + (row * 0x40) + column; |
pegcjs | 11:25a700bce989 | 136 | } |
pegcjs | 11:25a700bce989 | 137 | } |
pegcjs | 11:25a700bce989 | 138 | |
pegcjs | 11:25a700bce989 | 139 | int TextLCD::columns() { |
pegcjs | 11:25a700bce989 | 140 | switch (_type) { |
pegcjs | 11:25a700bce989 | 141 | case LCD20x4: |
pegcjs | 11:25a700bce989 | 142 | case LCD20x2: |
pegcjs | 11:25a700bce989 | 143 | return 20; |
pegcjs | 11:25a700bce989 | 144 | case LCD16x2: |
pegcjs | 11:25a700bce989 | 145 | case LCD16x2B: |
pegcjs | 11:25a700bce989 | 146 | default: |
pegcjs | 11:25a700bce989 | 147 | return 16; |
pegcjs | 11:25a700bce989 | 148 | } |
pegcjs | 11:25a700bce989 | 149 | } |
pegcjs | 11:25a700bce989 | 150 | |
pegcjs | 11:25a700bce989 | 151 | int TextLCD::rows() { |
pegcjs | 11:25a700bce989 | 152 | switch (_type) { |
pegcjs | 11:25a700bce989 | 153 | case LCD20x4: |
pegcjs | 11:25a700bce989 | 154 | return 4; |
pegcjs | 11:25a700bce989 | 155 | case LCD16x2: |
pegcjs | 11:25a700bce989 | 156 | case LCD16x2B: |
pegcjs | 11:25a700bce989 | 157 | case LCD20x2: |
pegcjs | 11:25a700bce989 | 158 | default: |
pegcjs | 11:25a700bce989 | 159 | return 2; |
pegcjs | 11:25a700bce989 | 160 | } |
pegcjs | 11:25a700bce989 | 161 | } |