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:21:07 2012 +0000
Revision:
1:ca430b27054d
Parent:
0:173f8aaea8cc
1st public

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