library for driving character LCD in 4-bit mode (HD44780, ST7066U & compatible) supports 16x2, 20x2, 20x4 & 40x2 uses R/W pin for timing rather than delays

Dependents:   Ultrasonic

Committer:
wertyfrog
Date:
Wed Jul 11 22:08:08 2012 +0000
Revision:
0:173f8aaea8cc
Child:
1:ca430b27054d
[mbed] converted /AD7414_test/LCD40x2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wertyfrog 0:173f8aaea8cc 1 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
wertyfrog 0:173f8aaea8cc 2 * Copyright (c) 2007-2010, sford, http://mbed.org
wertyfrog 0:173f8aaea8cc 3
wertyfrog 0:173f8aaea8cc 4 *
wertyfrog 0:173f8aaea8cc 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
wertyfrog 0:173f8aaea8cc 6 * of this software and associated documentation files (the "Software"), to deal
wertyfrog 0:173f8aaea8cc 7 * in the Software without restriction, including without limitation the rights
wertyfrog 0:173f8aaea8cc 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wertyfrog 0:173f8aaea8cc 9 * copies of the Software, and to permit persons to whom the Software is
wertyfrog 0:173f8aaea8cc 10 * furnished to do so, subject to the following conditions:
wertyfrog 0:173f8aaea8cc 11 *
wertyfrog 0:173f8aaea8cc 12 * The above copyright notice and this permission notice shall be included in
wertyfrog 0:173f8aaea8cc 13 * all copies or substantial portions of the Software.
wertyfrog 0:173f8aaea8cc 14 *
wertyfrog 0:173f8aaea8cc 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wertyfrog 0:173f8aaea8cc 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wertyfrog 0:173f8aaea8cc 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wertyfrog 0:173f8aaea8cc 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wertyfrog 0:173f8aaea8cc 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wertyfrog 0:173f8aaea8cc 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wertyfrog 0:173f8aaea8cc 21 * THE SOFTWARE.
wertyfrog 0:173f8aaea8cc 22 */
wertyfrog 0:173f8aaea8cc 23
wertyfrog 0:173f8aaea8cc 24 #ifndef MBED_LCD40x2_H
wertyfrog 0:173f8aaea8cc 25 #define MBED_LCD40x2_H
wertyfrog 0:173f8aaea8cc 26
wertyfrog 0:173f8aaea8cc 27 #include "mbed.h"
wertyfrog 0:173f8aaea8cc 28
wertyfrog 0:173f8aaea8cc 29 /** A TextLCD interface for driving 4-bit HD44780-based LCDs
wertyfrog 0:173f8aaea8cc 30 *
wertyfrog 0:173f8aaea8cc 31 * Currently supports 16x2, 20x2, 40x2 and 20x4 panels
wertyfrog 0:173f8aaea8cc 32 *
wertyfrog 0:173f8aaea8cc 33 * @code
wertyfrog 0:173f8aaea8cc 34 * #include "mbed.h"
wertyfrog 0:173f8aaea8cc 35 * #include "TextLCD.h"
wertyfrog 0:173f8aaea8cc 36 *
wertyfrog 0:173f8aaea8cc 37 * TextLCD lcd(p10, p12, ,p13, p15, p16, p29, p30); // rs, rw, e, d0-d3
wertyfrog 0:173f8aaea8cc 38 *
wertyfrog 0:173f8aaea8cc 39 * int main() {
wertyfrog 0:173f8aaea8cc 40 * lcd.printf("Hello mbed\n");
wertyfrog 0:173f8aaea8cc 41 *
wertyfrog 0:173f8aaea8cc 42 * //define user chars
wertyfrog 0:173f8aaea8cc 43 * int pattern[8];
wertyfrog 0:173f8aaea8cc 44 * int pattern1[8];
wertyfrog 0:173f8aaea8cc 45 * pattern[0] = 1; // *
wertyfrog 0:173f8aaea8cc 46 * pattern[1] = 3; // **
wertyfrog 0:173f8aaea8cc 47 * pattern[2] = 5; // * *
wertyfrog 0:173f8aaea8cc 48 * pattern[3] = 9; // * *
wertyfrog 0:173f8aaea8cc 49 * pattern[4] = 0x11; // * *
wertyfrog 0:173f8aaea8cc 50 * pattern[5] = 0x19; // ** *
wertyfrog 0:173f8aaea8cc 51 * pattern[6] = 0x1d; // *** *
wertyfrog 0:173f8aaea8cc 52 * pattern[7] = 0x1f; // *****
wertyfrog 0:173f8aaea8cc 53 *
wertyfrog 0:173f8aaea8cc 54 * pattern1[0] = 0x10; // *
wertyfrog 0:173f8aaea8cc 55 * pattern1[1] = 0x18; // **
wertyfrog 0:173f8aaea8cc 56 * pattern1[2] = 0x14; // * *
wertyfrog 0:173f8aaea8cc 57 * pattern1[3] = 0x12; // * *
wertyfrog 0:173f8aaea8cc 58 * pattern1[4] = 0x11; // * *
wertyfrog 0:173f8aaea8cc 59 * pattern1[5] = 0x13; // * **
wertyfrog 0:173f8aaea8cc 60 * pattern1[6] = 0x17; // * ***
wertyfrog 0:173f8aaea8cc 61 * pattern1[7] = 0x1f; // *****
wertyfrog 0:173f8aaea8cc 62 *
wertyfrog 0:173f8aaea8cc 63 * lcd.writeCGRAM(0, pattern);
wertyfrog 0:173f8aaea8cc 64 * lcd.writeCGRAM(1, pattern1);
wertyfrog 0:173f8aaea8cc 65 *
wertyfrog 0:173f8aaea8cc 66 * lcd.locate(15,0);
wertyfrog 0:173f8aaea8cc 67 * lcd.putc(0); // user pattern 0
wertyfrog 0:173f8aaea8cc 68 * lcd.putc(1); // user pattern 1
wertyfrog 0:173f8aaea8cc 69 * }
wertyfrog 0:173f8aaea8cc 70 * @endcode
wertyfrog 0:173f8aaea8cc 71 */
wertyfrog 0:173f8aaea8cc 72 class TextLCD : public Stream {
wertyfrog 0:173f8aaea8cc 73 public:
wertyfrog 0:173f8aaea8cc 74
wertyfrog 0:173f8aaea8cc 75 /** LCD panel format */
wertyfrog 0:173f8aaea8cc 76 enum LCDType {
wertyfrog 0:173f8aaea8cc 77 LCD16x2 /**< 16x2 LCD panel (default) */
wertyfrog 0:173f8aaea8cc 78 , LCD16x2B /**< 16x2 LCD panel alternate addressing */
wertyfrog 0:173f8aaea8cc 79 , LCD20x2 /**< 20x2 LCD panel */
wertyfrog 0:173f8aaea8cc 80 , LCD20x4 /**< 20x4 LCD panel */
wertyfrog 0:173f8aaea8cc 81 , LCD40x2 /**< 40x2 LCD panel */
wertyfrog 0:173f8aaea8cc 82 };
wertyfrog 0:173f8aaea8cc 83
wertyfrog 0:173f8aaea8cc 84 /** Create a TextLCD interface
wertyfrog 0:173f8aaea8cc 85 *
wertyfrog 0:173f8aaea8cc 86 * @param rs Instruction/data control line
wertyfrog 0:173f8aaea8cc 87 * @param e Enable line (clock)
wertyfrog 0:173f8aaea8cc 88 * @param rw read / write
wertyfrog 0:173f8aaea8cc 89 * @param d0-d3 Data lines
wertyfrog 0:173f8aaea8cc 90 * @param type Sets the panel size/addressing mode (default = LCD16x2)
wertyfrog 0:173f8aaea8cc 91 */
wertyfrog 0:173f8aaea8cc 92 TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, LCDType type = LCD16x2);
wertyfrog 0:173f8aaea8cc 93
wertyfrog 0:173f8aaea8cc 94 #if DOXYGEN_ONLY
wertyfrog 0:173f8aaea8cc 95 /** Write a character to the LCD
wertyfrog 0:173f8aaea8cc 96 *
wertyfrog 0:173f8aaea8cc 97 * @param c The character to write to the display
wertyfrog 0:173f8aaea8cc 98 */
wertyfrog 0:173f8aaea8cc 99 int putc(int c);
wertyfrog 0:173f8aaea8cc 100
wertyfrog 0:173f8aaea8cc 101 /** Write a formated string to the LCD
wertyfrog 0:173f8aaea8cc 102 *
wertyfrog 0:173f8aaea8cc 103 * @param format A printf-style format string, followed by the
wertyfrog 0:173f8aaea8cc 104 * variables to use in formating the string.
wertyfrog 0:173f8aaea8cc 105 */
wertyfrog 0:173f8aaea8cc 106 int printf(const char* format, ...);
wertyfrog 0:173f8aaea8cc 107 #endif
wertyfrog 0:173f8aaea8cc 108
wertyfrog 0:173f8aaea8cc 109 /** Locate to a screen column and row
wertyfrog 0:173f8aaea8cc 110 *
wertyfrog 0:173f8aaea8cc 111 * @param column The horizontal position from the left, indexed from 0
wertyfrog 0:173f8aaea8cc 112 * @param row The vertical position from the top, indexed from 0
wertyfrog 0:173f8aaea8cc 113 */
wertyfrog 0:173f8aaea8cc 114 void locate(int column, int row);
wertyfrog 0:173f8aaea8cc 115
wertyfrog 0:173f8aaea8cc 116 /** Clear the screen and locate to 0,0 */
wertyfrog 0:173f8aaea8cc 117 void cls();
wertyfrog 0:173f8aaea8cc 118
wertyfrog 0:173f8aaea8cc 119 int rows();
wertyfrog 0:173f8aaea8cc 120 int columns();
wertyfrog 0:173f8aaea8cc 121
wertyfrog 0:173f8aaea8cc 122 /** write a user defined char
wertyfrog 0:173f8aaea8cc 123 *
wertyfrog 0:173f8aaea8cc 124 * @param address The user defined char (0-7)
wertyfrog 0:173f8aaea8cc 125 * @param pattern[8] bit pattern 5*8 of char
wertyfrog 0:173f8aaea8cc 126 */
wertyfrog 0:173f8aaea8cc 127 void writeCGRAM(int address, int pattern[8]);
wertyfrog 0:173f8aaea8cc 128
wertyfrog 0:173f8aaea8cc 129 /** Get the char at the current position
wertyfrog 0:173f8aaea8cc 130 *
wertyfrog 0:173f8aaea8cc 131 * int getc()
wertyfrog 0:173f8aaea8cc 132 */
wertyfrog 0:173f8aaea8cc 133
wertyfrog 0:173f8aaea8cc 134 protected:
wertyfrog 0:173f8aaea8cc 135
wertyfrog 0:173f8aaea8cc 136 // Stream implementation functions
wertyfrog 0:173f8aaea8cc 137 virtual int _putc(int value);
wertyfrog 0:173f8aaea8cc 138 virtual int _getc();
wertyfrog 0:173f8aaea8cc 139
wertyfrog 0:173f8aaea8cc 140 int address(int column, int row);
wertyfrog 0:173f8aaea8cc 141 void character(int column, int row, int c);
wertyfrog 0:173f8aaea8cc 142 void writeByte(int value);
wertyfrog 0:173f8aaea8cc 143 void writeCommand(int command);
wertyfrog 0:173f8aaea8cc 144 void writeData(int data);
wertyfrog 0:173f8aaea8cc 145 int readData();
wertyfrog 0:173f8aaea8cc 146 void waitBusy();
wertyfrog 0:173f8aaea8cc 147 DigitalOut _rs, _rw, _e;
wertyfrog 0:173f8aaea8cc 148 BusInOut _d;
wertyfrog 0:173f8aaea8cc 149 LCDType _type;
wertyfrog 0:173f8aaea8cc 150
wertyfrog 0:173f8aaea8cc 151 int _column;
wertyfrog 0:173f8aaea8cc 152 int _row;
wertyfrog 0:173f8aaea8cc 153 };
wertyfrog 0:173f8aaea8cc 154
wertyfrog 0:173f8aaea8cc 155 #endif