Driver for a Text LCD using a PCF8574 over I2C
Dependencies: mbed
Fork of I2CTextLCD by
i2clcd/TextLCD.cpp@3:846b36e1f4d5, 2016-01-19 (annotated)
- Committer:
- mijimy
- Date:
- Tue Jan 19 07:50:15 2016 +0000
- Revision:
- 3:846b36e1f4d5
- Parent:
- 2:628a00fec43c
i2c pcf8574t lcd 20x4 .. remove crash after certain cycle
Who changed what in which revision?
User | Revision | Line number | New 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" |
mijimy | 2:628a00fec43c | 53 | |
romilly | 0:6beb6f498640 | 54 | |
mijimy | 3:846b36e1f4d5 | 55 | TextLCD::TextLCD(PinName sda, PinName scl, int i2cAddress, LCDType type) : _i2c(sda, scl), _i2cAddress(i2cAddress) , _type(type){ |
mijimy | 3:846b36e1f4d5 | 56 | // _i2cAddress = i2cAddress; |
mijimy | 3:846b36e1f4d5 | 57 | writeByte(E_ON,false); |
romilly | 0:6beb6f498640 | 58 | wait(0.015); // Wait 15ms to ensure powered up |
romilly | 0:6beb6f498640 | 59 | |
romilly | 0:6beb6f498640 | 60 | // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) |
romilly | 0:6beb6f498640 | 61 | for (int i=0; i<3; i++) { |
romilly | 0:6beb6f498640 | 62 | writeByte(0x3, false); |
romilly | 0:6beb6f498640 | 63 | wait(0.00164); // this command takes 1.64ms, so wait for it |
romilly | 0:6beb6f498640 | 64 | } |
romilly | 0:6beb6f498640 | 65 | writeByte(0x2, false); // 4-bit mode |
romilly | 0:6beb6f498640 | 66 | wait(0.000040f); // most instructions take 40us |
romilly | 0:6beb6f498640 | 67 | |
romilly | 0:6beb6f498640 | 68 | writeCommand(0x28); // Function set 001 BW N F - - |
romilly | 0:6beb6f498640 | 69 | writeCommand(0x0C); |
romilly | 0:6beb6f498640 | 70 | 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 | 71 | cls(); |
romilly | 0:6beb6f498640 | 72 | } |
romilly | 0:6beb6f498640 | 73 | |
romilly | 0:6beb6f498640 | 74 | void TextLCD::character(int column, int row, int c) { |
romilly | 0:6beb6f498640 | 75 | int a = address(column, row); |
romilly | 0:6beb6f498640 | 76 | writeCommand(a); |
romilly | 0:6beb6f498640 | 77 | writeData(c); |
romilly | 0:6beb6f498640 | 78 | } |
romilly | 0:6beb6f498640 | 79 | |
romilly | 0:6beb6f498640 | 80 | void TextLCD::cls() { |
romilly | 0:6beb6f498640 | 81 | writeCommand(0x01); // cls, and set cursor to 0 |
romilly | 0:6beb6f498640 | 82 | wait(0.00164f); // This command takes 1.64 ms |
romilly | 0:6beb6f498640 | 83 | locate(0, 0); |
romilly | 0:6beb6f498640 | 84 | } |
romilly | 0:6beb6f498640 | 85 | |
romilly | 0:6beb6f498640 | 86 | void TextLCD::locate(int column, int row) { |
romilly | 0:6beb6f498640 | 87 | _column = column; |
romilly | 0:6beb6f498640 | 88 | _row = row; |
romilly | 0:6beb6f498640 | 89 | } |
romilly | 0:6beb6f498640 | 90 | |
romilly | 0:6beb6f498640 | 91 | int TextLCD::_putc(int value) { |
romilly | 0:6beb6f498640 | 92 | if (value == '\n') { |
romilly | 0:6beb6f498640 | 93 | _column = 0; |
romilly | 0:6beb6f498640 | 94 | _row++; |
romilly | 0:6beb6f498640 | 95 | if (_row >= rows()) { |
romilly | 0:6beb6f498640 | 96 | _row = 0; |
romilly | 0:6beb6f498640 | 97 | } |
romilly | 0:6beb6f498640 | 98 | } else { |
romilly | 0:6beb6f498640 | 99 | character(_column, _row, value); |
romilly | 0:6beb6f498640 | 100 | _column++; |
romilly | 0:6beb6f498640 | 101 | if (_column >= columns()) { |
romilly | 0:6beb6f498640 | 102 | _column = 0; |
romilly | 0:6beb6f498640 | 103 | _row++; |
romilly | 0:6beb6f498640 | 104 | if (_row >= rows()) { |
romilly | 0:6beb6f498640 | 105 | _row = 0; |
romilly | 0:6beb6f498640 | 106 | } |
romilly | 0:6beb6f498640 | 107 | } |
romilly | 0:6beb6f498640 | 108 | } |
romilly | 0:6beb6f498640 | 109 | return value; |
romilly | 0:6beb6f498640 | 110 | } |
romilly | 0:6beb6f498640 | 111 | |
romilly | 0:6beb6f498640 | 112 | int TextLCD::_getc() { |
romilly | 0:6beb6f498640 | 113 | return -1; |
romilly | 0:6beb6f498640 | 114 | } |
romilly | 0:6beb6f498640 | 115 | |
romilly | 0:6beb6f498640 | 116 | void TextLCD::writeI2CByte(int data) { |
romilly | 0:6beb6f498640 | 117 | // equivalent to writeI2CByte |
mijimy | 3:846b36e1f4d5 | 118 | char cmd[1]; |
romilly | 0:6beb6f498640 | 119 | cmd[0] = data; |
romilly | 0:6beb6f498640 | 120 | _i2c.write(_i2cAddress, cmd, 1); |
romilly | 0:6beb6f498640 | 121 | } |
romilly | 0:6beb6f498640 | 122 | |
romilly | 0:6beb6f498640 | 123 | void TextLCD::writeNibble(int data, bool rs) { |
syafiq8530 | 1:eac18254e072 | 124 | data = data << 4; |
syafiq8530 | 1:eac18254e072 | 125 | data |= BL_ON; // E on |
romilly | 0:6beb6f498640 | 126 | if (rs) { |
romilly | 0:6beb6f498640 | 127 | data = data | RS_ON; // set rs bit |
romilly | 0:6beb6f498640 | 128 | } |
romilly | 0:6beb6f498640 | 129 | data |= E_ON; // E on |
romilly | 0:6beb6f498640 | 130 | writeI2CByte(data); |
romilly | 0:6beb6f498640 | 131 | data ^= E_ON; // E off |
mijimy | 3:846b36e1f4d5 | 132 | //wait_us(1); |
mijimy | 3:846b36e1f4d5 | 133 | wait(0.000001f); // most instructions take 40us |
romilly | 0:6beb6f498640 | 134 | writeI2CByte(data); |
mijimy | 3:846b36e1f4d5 | 135 | wait(0.001f); // most instructions take 40us |
mijimy | 3:846b36e1f4d5 | 136 | // wait_us(1000); |
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 | } |