Driver for a Text LCD using a PCF8574 over I2C

Dependencies:   mbed

Fork of I2CTextLCD by Romilly Cocking

Committer:
syafiq8530
Date:
Thu Jan 14 09:48:50 2016 +0000
Revision:
1:eac18254e072
Parent:
0:6beb6f498640
lcd12c

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"
syafiq8530 1:eac18254e072 53 //nclude "PCF8574.h"
romilly 0:6beb6f498640 54
syafiq8530 1:eac18254e072 55 TextLCD::TextLCD(PinName sda, PinName scl, int i2cAddress, LCDType type) : _i2c(sda, scl), _type(type) {
romilly 0:6beb6f498640 56 _i2cAddress = i2cAddress;
romilly 0:6beb6f498640 57 wait(0.015); // 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++) {
romilly 0:6beb6f498640 61 writeByte(0x3, false);
romilly 0:6beb6f498640 62 wait(0.00164); // this command takes 1.64ms, so wait for it
romilly 0:6beb6f498640 63 }
romilly 0:6beb6f498640 64 writeByte(0x2, false); // 4-bit mode
romilly 0:6beb6f498640 65 wait(0.000040f); // most instructions take 40us
romilly 0:6beb6f498640 66
romilly 0:6beb6f498640 67 writeCommand(0x28); // Function set 001 BW N F - -
romilly 0:6beb6f498640 68 writeCommand(0x0C);
romilly 0:6beb6f498640 69 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
romilly 0:6beb6f498640 70 cls();
romilly 0:6beb6f498640 71 }
romilly 0:6beb6f498640 72
romilly 0:6beb6f498640 73 void TextLCD::character(int column, int row, int c) {
romilly 0:6beb6f498640 74 int a = address(column, row);
romilly 0:6beb6f498640 75 writeCommand(a);
romilly 0:6beb6f498640 76 writeData(c);
romilly 0:6beb6f498640 77 }
romilly 0:6beb6f498640 78
romilly 0:6beb6f498640 79 void TextLCD::cls() {
romilly 0:6beb6f498640 80 writeCommand(0x01); // cls, and set cursor to 0
romilly 0:6beb6f498640 81 wait(0.00164f); // This command takes 1.64 ms
romilly 0:6beb6f498640 82 locate(0, 0);
romilly 0:6beb6f498640 83 }
romilly 0:6beb6f498640 84
romilly 0:6beb6f498640 85 void TextLCD::locate(int column, int row) {
romilly 0:6beb6f498640 86 _column = column;
romilly 0:6beb6f498640 87 _row = row;
romilly 0:6beb6f498640 88 }
romilly 0:6beb6f498640 89
romilly 0:6beb6f498640 90 int TextLCD::_putc(int value) {
romilly 0:6beb6f498640 91 if (value == '\n') {
romilly 0:6beb6f498640 92 _column = 0;
romilly 0:6beb6f498640 93 _row++;
romilly 0:6beb6f498640 94 if (_row >= rows()) {
romilly 0:6beb6f498640 95 _row = 0;
romilly 0:6beb6f498640 96 }
romilly 0:6beb6f498640 97 } else {
romilly 0:6beb6f498640 98 character(_column, _row, value);
romilly 0:6beb6f498640 99 _column++;
romilly 0:6beb6f498640 100 if (_column >= columns()) {
romilly 0:6beb6f498640 101 _column = 0;
romilly 0:6beb6f498640 102 _row++;
romilly 0:6beb6f498640 103 if (_row >= rows()) {
romilly 0:6beb6f498640 104 _row = 0;
romilly 0:6beb6f498640 105 }
romilly 0:6beb6f498640 106 }
romilly 0:6beb6f498640 107 }
romilly 0:6beb6f498640 108 return value;
romilly 0:6beb6f498640 109 }
romilly 0:6beb6f498640 110
romilly 0:6beb6f498640 111 int TextLCD::_getc() {
romilly 0:6beb6f498640 112 return -1;
romilly 0:6beb6f498640 113 }
romilly 0:6beb6f498640 114
romilly 0:6beb6f498640 115 void TextLCD::writeI2CByte(int data) {
romilly 0:6beb6f498640 116 // equivalent to writeI2CByte
romilly 0:6beb6f498640 117 char * cmd = new char[1];
romilly 0:6beb6f498640 118 cmd[0] = data;
romilly 0:6beb6f498640 119 _i2c.write(_i2cAddress, cmd, 1);
romilly 0:6beb6f498640 120 }
romilly 0:6beb6f498640 121
romilly 0:6beb6f498640 122 void TextLCD::writeNibble(int data, bool rs) {
syafiq8530 1:eac18254e072 123 data = data << 4;
syafiq8530 1:eac18254e072 124 data |= BL_ON; // E on
romilly 0:6beb6f498640 125 if (rs) {
romilly 0:6beb6f498640 126 data = data | RS_ON; // set rs bit
romilly 0:6beb6f498640 127 }
romilly 0:6beb6f498640 128 data |= E_ON; // E on
romilly 0:6beb6f498640 129 writeI2CByte(data);
romilly 0:6beb6f498640 130 data ^= E_ON; // E off
romilly 0:6beb6f498640 131 wait_us(1);
romilly 0:6beb6f498640 132 writeI2CByte(data);
romilly 0:6beb6f498640 133 wait_us(1000);
romilly 0:6beb6f498640 134 }
romilly 0:6beb6f498640 135
romilly 0:6beb6f498640 136 void TextLCD::writeByte(int data, bool rs) {
romilly 0:6beb6f498640 137 writeNibble(data >> 4, rs);
romilly 0:6beb6f498640 138 writeNibble(data & 0x0F, rs);
romilly 0:6beb6f498640 139 }
romilly 0:6beb6f498640 140
romilly 0:6beb6f498640 141 void TextLCD::writeCommand(int command) {
romilly 0:6beb6f498640 142 // equivalent to ard commandWrite
romilly 0:6beb6f498640 143 writeByte(command, false);
romilly 0:6beb6f498640 144 }
romilly 0:6beb6f498640 145
romilly 0:6beb6f498640 146 void TextLCD::writeData(int data) {
romilly 0:6beb6f498640 147
romilly 0:6beb6f498640 148 writeByte(data, true);
romilly 0:6beb6f498640 149 }
romilly 0:6beb6f498640 150
romilly 0:6beb6f498640 151 int TextLCD::address(int column, int row) {
romilly 0:6beb6f498640 152 switch (_type) {
romilly 0:6beb6f498640 153 case LCD20x4:
romilly 0:6beb6f498640 154 switch (row) {
romilly 0:6beb6f498640 155 case 0:
romilly 0:6beb6f498640 156 return 0x80 + column;
romilly 0:6beb6f498640 157 case 1:
romilly 0:6beb6f498640 158 return 0xc0 + column;
romilly 0:6beb6f498640 159 case 2:
romilly 0:6beb6f498640 160 return 0x94 + column;
romilly 0:6beb6f498640 161 case 3:
romilly 0:6beb6f498640 162 return 0xd4 + column;
romilly 0:6beb6f498640 163 }
romilly 0:6beb6f498640 164 case LCD16x2B:
romilly 0:6beb6f498640 165 return 0x80 + (row * 40) + column;
romilly 0:6beb6f498640 166 case LCD16x2:
romilly 0:6beb6f498640 167 case LCD20x2:
romilly 0:6beb6f498640 168 default:
romilly 0:6beb6f498640 169 return 0x80 + (row * 0x40) + column;
romilly 0:6beb6f498640 170 }
romilly 0:6beb6f498640 171 }
romilly 0:6beb6f498640 172
romilly 0:6beb6f498640 173 int TextLCD::columns() {
romilly 0:6beb6f498640 174 switch (_type) {
romilly 0:6beb6f498640 175 case LCD20x4:
romilly 0:6beb6f498640 176 case LCD20x2:
romilly 0:6beb6f498640 177 return 20;
romilly 0:6beb6f498640 178 case LCD16x2:
romilly 0:6beb6f498640 179 case LCD16x2B:
romilly 0:6beb6f498640 180 default:
romilly 0:6beb6f498640 181 return 16;
romilly 0:6beb6f498640 182 }
romilly 0:6beb6f498640 183 }
romilly 0:6beb6f498640 184
romilly 0:6beb6f498640 185 int TextLCD::rows() {
romilly 0:6beb6f498640 186 switch (_type) {
romilly 0:6beb6f498640 187 case LCD20x4:
romilly 0:6beb6f498640 188 return 4;
romilly 0:6beb6f498640 189 case LCD16x2:
romilly 0:6beb6f498640 190 case LCD16x2B:
romilly 0:6beb6f498640 191 case LCD20x2:
romilly 0:6beb6f498640 192 default:
romilly 0:6beb6f498640 193 return 2;
romilly 0:6beb6f498640 194 }
romilly 0:6beb6f498640 195 }