Driver for a Text LCD using a PCF8574 over I2C It works with standard I2C Module with different pinout

Dependencies:   mbed

Fork of I2CTextLCD by Romilly Cocking

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?

UserRevisionLine numberNew contents of line
romilly 0:6beb6f498640 1 /* mbed I2CTextLCD Library, for a 4-bit LCD driven by I2C and a PCF8574
romilly 0:6beb6f498640 2 * Copyright (c) 2007-2010, sford, rcocking
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 /* This is a hack of Simon Ford's direct-driven TextLCD code.
romilly 0:6beb6f498640 24 * It should be refactored to extract a common superclass
romilly 0:6beb6f498640 25 * but my C++ isn't yet good enough :)
romilly 0:6beb6f498640 26 *
romilly 0:6beb6f498640 27 * The code assumes the following connections between the PCF8574
romilly 0:6beb6f498640 28 * and the LCD
romilly 0:6beb6f498640 29 *
romilly 0:6beb6f498640 30 * nc - D0
romilly 0:6beb6f498640 31 * nc - D1
romilly 0:6beb6f498640 32 * nc - D2
romilly 0:6beb6f498640 33 * nc - D3
romilly 0:6beb6f498640 34 * P0 - D4
romilly 0:6beb6f498640 35 * P1 - D5
romilly 0:6beb6f498640 36 * P2 - D6
romilly 0:6beb6f498640 37 * P3 - D7
romilly 0:6beb6f498640 38 * P4 - E
romilly 0:6beb6f498640 39 * P5 - nc
romilly 0:6beb6f498640 40 * P6 - nc
romilly 0:6beb6f498640 41 * P7 - RS
romilly 0:6beb6f498640 42 * gnd - R/W
romilly 0:6beb6f498640 43 *
romilly 0:6beb6f498640 44 * D0-3 of the LCD are not connected because we work in 4-bit mode
romilly 0:6beb6f498640 45 * R/W is hardwired to gound, as we only ever write to the LCD
romilly 0:6beb6f498640 46 * A0-2 on the PCF8574 can be set in any combination; you will need to modify
romilly 0:6beb6f498640 47 * the I2C address in the I2CTextLCD constructor.
romilly 0:6beb6f498640 48 * Remember that the mbed uses 8-bit addresses, which should be
romilly 0:6beb6f498640 49 * in the range 0x40-0x4E for the PCF8574
romilly 0:6beb6f498640 50 */
romilly 0:6beb6f498640 51 #include "TextLCD.h"
romilly 0:6beb6f498640 52 #include "mbed.h"
romilly 0:6beb6f498640 53
romilly 0:6beb6f498640 54 TextLCD::TextLCD(PinName sda, PinName scl, int i2cAddress, LCDType type) : _i2c(sda, scl),
romilly 0:6beb6f498640 55 _type(type) {
romilly 0:6beb6f498640 56 _i2cAddress = i2cAddress;
inggrig 1:3561ca8c9754 57 wait(0.020); // Wait 15ms to ensure powered up
romilly 0:6beb6f498640 58
romilly 0:6beb6f498640 59 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
romilly 0:6beb6f498640 60 for (int i=0; i<3; i++) {
inggrig 1:3561ca8c9754 61 writeByte(0x03, false);
inggrig 1:3561ca8c9754 62 wait(0.005); // this command takes 1.64ms, so wait for it
romilly 0:6beb6f498640 63 }
romilly 0:6beb6f498640 64 writeByte(0x2, false); // 4-bit mode
inggrig 1:3561ca8c9754 65 wait(0.000080f); // most instructions take 40us
romilly 0:6beb6f498640 66
romilly 0:6beb6f498640 67 writeCommand(0x28); // Function set 001 BW N F - -
inggrig 1:3561ca8c9754 68 wait(0.000080f);
romilly 0:6beb6f498640 69 writeCommand(0x0C);
inggrig 1:3561ca8c9754 70 wait(0.000080f);
romilly 0:6beb6f498640 71 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
inggrig 1:3561ca8c9754 72 wait(0.000080f);
romilly 0:6beb6f498640 73 cls();
romilly 0:6beb6f498640 74 }
romilly 0:6beb6f498640 75
romilly 0:6beb6f498640 76 void TextLCD::character(int column, int row, int c) {
romilly 0:6beb6f498640 77 int a = address(column, row);
romilly 0:6beb6f498640 78 writeCommand(a);
romilly 0:6beb6f498640 79 writeData(c);
romilly 0:6beb6f498640 80 }
romilly 0:6beb6f498640 81
romilly 0:6beb6f498640 82 void TextLCD::cls() {
romilly 0:6beb6f498640 83 writeCommand(0x01); // cls, and set cursor to 0
romilly 0:6beb6f498640 84 wait(0.00164f); // This command takes 1.64 ms
romilly 0:6beb6f498640 85 locate(0, 0);
romilly 0:6beb6f498640 86 }
romilly 0:6beb6f498640 87
romilly 0:6beb6f498640 88 void TextLCD::locate(int column, int row) {
romilly 0:6beb6f498640 89 _column = column;
romilly 0:6beb6f498640 90 _row = row;
romilly 0:6beb6f498640 91 }
romilly 0:6beb6f498640 92
romilly 0:6beb6f498640 93 int TextLCD::_putc(int value) {
romilly 0:6beb6f498640 94 if (value == '\n') {
romilly 0:6beb6f498640 95 _column = 0;
romilly 0:6beb6f498640 96 _row++;
romilly 0:6beb6f498640 97 if (_row >= rows()) {
romilly 0:6beb6f498640 98 _row = 0;
romilly 0:6beb6f498640 99 }
romilly 0:6beb6f498640 100 } else {
romilly 0:6beb6f498640 101 character(_column, _row, value);
romilly 0:6beb6f498640 102 _column++;
romilly 0:6beb6f498640 103 if (_column >= columns()) {
romilly 0:6beb6f498640 104 _column = 0;
romilly 0:6beb6f498640 105 _row++;
romilly 0:6beb6f498640 106 if (_row >= rows()) {
romilly 0:6beb6f498640 107 _row = 0;
romilly 0:6beb6f498640 108 }
romilly 0:6beb6f498640 109 }
romilly 0:6beb6f498640 110 }
romilly 0:6beb6f498640 111 return value;
romilly 0:6beb6f498640 112 }
romilly 0:6beb6f498640 113
romilly 0:6beb6f498640 114 int TextLCD::_getc() {
romilly 0:6beb6f498640 115 return -1;
romilly 0:6beb6f498640 116 }
romilly 0:6beb6f498640 117
romilly 0:6beb6f498640 118 void TextLCD::writeI2CByte(int data) {
romilly 0:6beb6f498640 119 // equivalent to writeI2CByte
romilly 0:6beb6f498640 120 char * cmd = new char[1];
romilly 0:6beb6f498640 121 cmd[0] = data;
romilly 0:6beb6f498640 122 _i2c.write(_i2cAddress, cmd, 1);
romilly 0:6beb6f498640 123 }
romilly 0:6beb6f498640 124
romilly 0:6beb6f498640 125 void TextLCD::writeNibble(int data, bool rs) {
inggrig 1:3561ca8c9754 126 data=data<<4;
inggrig 1:3561ca8c9754 127 data=data | 0x08; //backlight on
romilly 0:6beb6f498640 128 if (rs) {
romilly 0:6beb6f498640 129 data = data | RS_ON; // set rs bit
romilly 0:6beb6f498640 130 }
romilly 0:6beb6f498640 131 data |= E_ON; // E on
romilly 0:6beb6f498640 132 writeI2CByte(data);
romilly 0:6beb6f498640 133 data ^= E_ON; // E off
inggrig 1:3561ca8c9754 134 wait_us(100);
romilly 0:6beb6f498640 135 writeI2CByte(data);
inggrig 1:3561ca8c9754 136 wait_us(100);
romilly 0:6beb6f498640 137 }
romilly 0:6beb6f498640 138
romilly 0:6beb6f498640 139 void TextLCD::writeByte(int data, bool rs) {
romilly 0:6beb6f498640 140 writeNibble(data >> 4, rs);
romilly 0:6beb6f498640 141 writeNibble(data & 0x0F, rs);
romilly 0:6beb6f498640 142 }
romilly 0:6beb6f498640 143
romilly 0:6beb6f498640 144 void TextLCD::writeCommand(int command) {
romilly 0:6beb6f498640 145 // equivalent to ard commandWrite
romilly 0:6beb6f498640 146 writeByte(command, false);
romilly 0:6beb6f498640 147 }
romilly 0:6beb6f498640 148
romilly 0:6beb6f498640 149 void TextLCD::writeData(int data) {
romilly 0:6beb6f498640 150
romilly 0:6beb6f498640 151 writeByte(data, true);
romilly 0:6beb6f498640 152 }
romilly 0:6beb6f498640 153
romilly 0:6beb6f498640 154 int TextLCD::address(int column, int row) {
romilly 0:6beb6f498640 155 switch (_type) {
romilly 0:6beb6f498640 156 case LCD20x4:
romilly 0:6beb6f498640 157 switch (row) {
romilly 0:6beb6f498640 158 case 0:
romilly 0:6beb6f498640 159 return 0x80 + column;
romilly 0:6beb6f498640 160 case 1:
romilly 0:6beb6f498640 161 return 0xc0 + column;
romilly 0:6beb6f498640 162 case 2:
romilly 0:6beb6f498640 163 return 0x94 + column;
romilly 0:6beb6f498640 164 case 3:
romilly 0:6beb6f498640 165 return 0xd4 + column;
romilly 0:6beb6f498640 166 }
romilly 0:6beb6f498640 167 case LCD16x2B:
romilly 0:6beb6f498640 168 return 0x80 + (row * 40) + column;
romilly 0:6beb6f498640 169 case LCD16x2:
romilly 0:6beb6f498640 170 case LCD20x2:
romilly 0:6beb6f498640 171 default:
romilly 0:6beb6f498640 172 return 0x80 + (row * 0x40) + column;
romilly 0:6beb6f498640 173 }
romilly 0:6beb6f498640 174 }
romilly 0:6beb6f498640 175
romilly 0:6beb6f498640 176 int TextLCD::columns() {
romilly 0:6beb6f498640 177 switch (_type) {
romilly 0:6beb6f498640 178 case LCD20x4:
romilly 0:6beb6f498640 179 case LCD20x2:
romilly 0:6beb6f498640 180 return 20;
romilly 0:6beb6f498640 181 case LCD16x2:
romilly 0:6beb6f498640 182 case LCD16x2B:
romilly 0:6beb6f498640 183 default:
romilly 0:6beb6f498640 184 return 16;
romilly 0:6beb6f498640 185 }
romilly 0:6beb6f498640 186 }
romilly 0:6beb6f498640 187
romilly 0:6beb6f498640 188 int TextLCD::rows() {
romilly 0:6beb6f498640 189 switch (_type) {
romilly 0:6beb6f498640 190 case LCD20x4:
romilly 0:6beb6f498640 191 return 4;
romilly 0:6beb6f498640 192 case LCD16x2:
romilly 0:6beb6f498640 193 case LCD16x2B:
romilly 0:6beb6f498640 194 case LCD20x2:
romilly 0:6beb6f498640 195 default:
romilly 0:6beb6f498640 196 return 2;
romilly 0:6beb6f498640 197 }
romilly 0:6beb6f498640 198 }