Driver for a Text LCD using a PCF8574 over I2C It works with standard I2C Module with different pinout
Fork of I2CTextLCD by
TextLCD.h@1:3561ca8c9754, 2018-04-06 (annotated)
- Committer:
- inggrig
- Date:
- Fri Apr 06 09:33:48 2018 +0000
- Revision:
- 1:3561ca8c9754
- Parent:
- 0:6beb6f498640
Library for I2C Lcd 16x2 with china 1? I2C module
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
romilly | 0:6beb6f498640 | 1 | /* mbed TextLCD Library, for a 4-bit LCD based on HD44780 |
romilly | 0:6beb6f498640 | 2 | * Copyright (c) 2007-2010, sford |
romilly | 0:6beb6f498640 | 3 | * |
romilly | 0:6beb6f498640 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
romilly | 0:6beb6f498640 | 5 | * of this software and associated documentation files (the "Software"), to deal |
romilly | 0:6beb6f498640 | 6 | * in the Software without restriction, including without limitation the rights |
romilly | 0:6beb6f498640 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
romilly | 0:6beb6f498640 | 8 | * copies of the Software, and to permit persons to whom the Software is |
romilly | 0:6beb6f498640 | 9 | * furnished to do so, subject to the following conditions: |
romilly | 0:6beb6f498640 | 10 | * |
romilly | 0:6beb6f498640 | 11 | * The above copyright notice and this permission notice shall be included in |
romilly | 0:6beb6f498640 | 12 | * all copies or substantial portions of the Software. |
romilly | 0:6beb6f498640 | 13 | * |
romilly | 0:6beb6f498640 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
romilly | 0:6beb6f498640 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
romilly | 0:6beb6f498640 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
romilly | 0:6beb6f498640 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
romilly | 0:6beb6f498640 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
romilly | 0:6beb6f498640 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
romilly | 0:6beb6f498640 | 20 | * THE SOFTWARE. |
romilly | 0:6beb6f498640 | 21 | */ |
romilly | 0:6beb6f498640 | 22 | |
romilly | 0:6beb6f498640 | 23 | #ifndef MBED_TEXTLCD_H |
romilly | 0:6beb6f498640 | 24 | #define MBED_TEXTLCD_H |
romilly | 0:6beb6f498640 | 25 | |
romilly | 0:6beb6f498640 | 26 | #include "mbed.h" |
romilly | 0:6beb6f498640 | 27 | |
inggrig | 1:3561ca8c9754 | 28 | #define E_ON 0x04 |
inggrig | 1:3561ca8c9754 | 29 | #define RS_ON 0x01 |
romilly | 0:6beb6f498640 | 30 | |
romilly | 0:6beb6f498640 | 31 | /** A TextLCD interface for driving 4-bit HD44780-based LCDs |
romilly | 0:6beb6f498640 | 32 | * |
romilly | 0:6beb6f498640 | 33 | * Currently supports 16x2, 20x2 and 20x4 panels |
romilly | 0:6beb6f498640 | 34 | * |
romilly | 0:6beb6f498640 | 35 | * @code |
romilly | 0:6beb6f498640 | 36 | * #include "mbed.h" |
romilly | 0:6beb6f498640 | 37 | * #include "TextLCD.h" |
romilly | 0:6beb6f498640 | 38 | * |
romilly | 0:6beb6f498640 | 39 | * TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d0-d3 |
romilly | 0:6beb6f498640 | 40 | * |
romilly | 0:6beb6f498640 | 41 | * int main() { |
romilly | 0:6beb6f498640 | 42 | * lcd.printf("Hello World!\n"); |
romilly | 0:6beb6f498640 | 43 | * } |
romilly | 0:6beb6f498640 | 44 | * @endcode |
romilly | 0:6beb6f498640 | 45 | */ |
romilly | 0:6beb6f498640 | 46 | class TextLCD : public Stream { |
romilly | 0:6beb6f498640 | 47 | public: |
romilly | 0:6beb6f498640 | 48 | |
romilly | 0:6beb6f498640 | 49 | /** LCD panel format */ |
romilly | 0:6beb6f498640 | 50 | enum LCDType { |
romilly | 0:6beb6f498640 | 51 | LCD16x2 /**< 16x2 LCD panel (default) */ |
romilly | 0:6beb6f498640 | 52 | , LCD16x2B /**< 16x2 LCD panel alternate addressing */ |
romilly | 0:6beb6f498640 | 53 | , LCD20x2 /**< 20x2 LCD panel */ |
romilly | 0:6beb6f498640 | 54 | , LCD20x4 /**< 20x4 LCD panel */ |
romilly | 0:6beb6f498640 | 55 | }; |
romilly | 0:6beb6f498640 | 56 | |
romilly | 0:6beb6f498640 | 57 | /** Create a TextLCD interface |
romilly | 0:6beb6f498640 | 58 | * |
romilly | 0:6beb6f498640 | 59 | * @param rs Instruction/data control line |
romilly | 0:6beb6f498640 | 60 | * @param e Enable line (clock) |
romilly | 0:6beb6f498640 | 61 | * @param d0-d3 Data lines |
romilly | 0:6beb6f498640 | 62 | * @param type Sets the panel size/addressing mode (default = LCD16x2) |
romilly | 0:6beb6f498640 | 63 | */ |
romilly | 0:6beb6f498640 | 64 | TextLCD(PinName sda, PinName scl, int i2cAddress = 0x40, LCDType type = LCD16x2); |
romilly | 0:6beb6f498640 | 65 | |
romilly | 0:6beb6f498640 | 66 | #if DOXYGEN_ONLY |
romilly | 0:6beb6f498640 | 67 | /** Write a character to the LCD |
romilly | 0:6beb6f498640 | 68 | * |
romilly | 0:6beb6f498640 | 69 | * @param c The character to write to the display |
romilly | 0:6beb6f498640 | 70 | */ |
romilly | 0:6beb6f498640 | 71 | int putc(int c); |
romilly | 0:6beb6f498640 | 72 | |
romilly | 0:6beb6f498640 | 73 | /** Write a formated string to the LCD |
romilly | 0:6beb6f498640 | 74 | * |
romilly | 0:6beb6f498640 | 75 | * @param format A printf-style format string, followed by the |
romilly | 0:6beb6f498640 | 76 | * variables to use in formating the string. |
romilly | 0:6beb6f498640 | 77 | */ |
romilly | 0:6beb6f498640 | 78 | int printf(const char* format, ...); |
romilly | 0:6beb6f498640 | 79 | #endif |
romilly | 0:6beb6f498640 | 80 | |
romilly | 0:6beb6f498640 | 81 | /** Locate to a screen column and row |
romilly | 0:6beb6f498640 | 82 | * |
romilly | 0:6beb6f498640 | 83 | * @param column The horizontal position from the left, indexed from 0 |
romilly | 0:6beb6f498640 | 84 | * @param row The vertical position from the top, indexed from 0 |
romilly | 0:6beb6f498640 | 85 | */ |
romilly | 0:6beb6f498640 | 86 | void locate(int column, int row); |
romilly | 0:6beb6f498640 | 87 | |
romilly | 0:6beb6f498640 | 88 | /** Clear the screen and locate to 0,0 */ |
romilly | 0:6beb6f498640 | 89 | void cls(); |
romilly | 0:6beb6f498640 | 90 | |
romilly | 0:6beb6f498640 | 91 | int rows(); |
romilly | 0:6beb6f498640 | 92 | int columns(); |
romilly | 0:6beb6f498640 | 93 | |
romilly | 0:6beb6f498640 | 94 | protected: |
romilly | 0:6beb6f498640 | 95 | |
romilly | 0:6beb6f498640 | 96 | // Stream implementation functions |
romilly | 0:6beb6f498640 | 97 | virtual int _putc(int value); |
romilly | 0:6beb6f498640 | 98 | virtual int _getc(); |
romilly | 0:6beb6f498640 | 99 | |
romilly | 0:6beb6f498640 | 100 | int address(int column, int row); |
romilly | 0:6beb6f498640 | 101 | void character(int column, int row, int c); |
romilly | 0:6beb6f498640 | 102 | void writeByte(int value, bool rs); |
romilly | 0:6beb6f498640 | 103 | void writeCommand(int command); |
romilly | 0:6beb6f498640 | 104 | void writeData(int data); |
romilly | 0:6beb6f498640 | 105 | void writeNibble(int value, bool rs); |
romilly | 0:6beb6f498640 | 106 | void writeI2CByte(int data); |
romilly | 0:6beb6f498640 | 107 | |
romilly | 0:6beb6f498640 | 108 | LCDType _type; |
romilly | 0:6beb6f498640 | 109 | int _rs; |
romilly | 0:6beb6f498640 | 110 | I2C _i2c; |
romilly | 0:6beb6f498640 | 111 | int _i2cAddress; |
romilly | 0:6beb6f498640 | 112 | |
romilly | 0:6beb6f498640 | 113 | int _column; |
romilly | 0:6beb6f498640 | 114 | int _row; |
romilly | 0:6beb6f498640 | 115 | }; |
romilly | 0:6beb6f498640 | 116 | |
romilly | 0:6beb6f498640 | 117 | #endif |