![](/media/cache/profiles/a7bf3f5462cc82062e41b3a2262e1a21.50x50_q85.jpg)
Yet another I2C Text based LCD display program
lc_display.h@2:91dec3d7903a, 2014-01-19 (annotated)
- Committer:
- pwheels
- Date:
- Sun Jan 19 14:23:58 2014 +0000
- Revision:
- 2:91dec3d7903a
- Parent:
- 1:d8767e2b8e7e
updated string to display command
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pwheels | 0:551f3dbd553c | 1 | /* |
pwheels | 0:551f3dbd553c | 2 | * Copyright (c) 2011 Paul van der Wielen, Pro-Serv |
pwheels | 0:551f3dbd553c | 3 | * |
pwheels | 0:551f3dbd553c | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
pwheels | 0:551f3dbd553c | 5 | * of this software and associated documentation files (the "Software"), to use |
pwheels | 0:551f3dbd553c | 6 | * and implement the software for none commercial reason and usage only and |
pwheels | 0:551f3dbd553c | 7 | * subject to the following conditions: |
pwheels | 0:551f3dbd553c | 8 | * |
pwheels | 0:551f3dbd553c | 9 | * The above copyright notice and this permission notice shall be included in |
pwheels | 0:551f3dbd553c | 10 | * all copies or substantial portions of the Software. |
pwheels | 0:551f3dbd553c | 11 | * |
pwheels | 0:551f3dbd553c | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
pwheels | 0:551f3dbd553c | 13 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
pwheels | 0:551f3dbd553c | 14 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
pwheels | 0:551f3dbd553c | 15 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
pwheels | 0:551f3dbd553c | 16 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
pwheels | 0:551f3dbd553c | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
pwheels | 0:551f3dbd553c | 18 | * THE SOFTWARE. |
pwheels | 0:551f3dbd553c | 19 | * |
pwheels | 0:551f3dbd553c | 20 | * Usage and assumptions: |
pwheels | 0:551f3dbd553c | 21 | * coding as to interface an I2C LC Display modle available from: |
pwheels | 0:551f3dbd553c | 22 | * Coptonix GmbH |
pwheels | 0:551f3dbd553c | 23 | * Berlin 24.06.2008by @ less than Euro 13.- |
pwheels | 0:551f3dbd553c | 24 | * which will interface to various HD44780 based units, 8Bit-Interface (16x2 or 20x4) |
pwheels | 0:551f3dbd553c | 25 | * |
pwheels | 0:551f3dbd553c | 26 | * Used device = LPC1768 |
pwheels | 0:551f3dbd553c | 27 | */ |
pwheels | 0:551f3dbd553c | 28 | #include "mbed.h" |
pwheels | 0:551f3dbd553c | 29 | |
pwheels | 0:551f3dbd553c | 30 | #define I2C_ADDR 0xfe |
pwheels | 0:551f3dbd553c | 31 | #define I2C_SPEED 400000 |
pwheels | 0:551f3dbd553c | 32 | |
pwheels | 0:551f3dbd553c | 33 | #define ClrDisplay 0x61 |
pwheels | 0:551f3dbd553c | 34 | #define ReturnHome 0x62 |
pwheels | 0:551f3dbd553c | 35 | #define SetCursor 0x63 |
pwheels | 0:551f3dbd553c | 36 | #define CharToLCD 0x64 |
pwheels | 0:551f3dbd553c | 37 | #define CMDToLCD 0x65 |
pwheels | 0:551f3dbd553c | 38 | #define Delete 0x66 |
pwheels | 0:551f3dbd553c | 39 | #define CopyToClipBoard 0x67 |
pwheels | 0:551f3dbd553c | 40 | #define Paste 0x68 |
pwheels | 0:551f3dbd553c | 41 | #define GetCursorAdr 0x69 |
pwheels | 0:551f3dbd553c | 42 | #define GetCharAtCur 0x6a |
pwheels | 0:551f3dbd553c | 43 | #define ReadRam 0x6b |
pwheels | 0:551f3dbd553c | 44 | #define DisplayON_OFF 0x6c |
pwheels | 0:551f3dbd553c | 45 | #define Shift 0x6d |
pwheels | 0:551f3dbd553c | 46 | #define SetEntryMode 0x6e |
pwheels | 0:551f3dbd553c | 47 | #define SetFunction 0x6f |
pwheels | 0:551f3dbd553c | 48 | #define SetCGRAMAdr 0x70 |
pwheels | 0:551f3dbd553c | 49 | #define SetGGRAMAdr 0x71 |
pwheels | 0:551f3dbd553c | 50 | #define SaveLCDIni 0x73 |
pwheels | 0:551f3dbd553c | 51 | #define WriteUserDefChar 0x74 |
pwheels | 0:551f3dbd553c | 52 | #define SaveUserDefChr 0x75 |
pwheels | 0:551f3dbd553c | 53 | #define WriteString 0x76 |
pwheels | 0:551f3dbd553c | 54 | #define LCDType 0x77 |
pwheels | 0:551f3dbd553c | 55 | |
pwheels | 1:d8767e2b8e7e | 56 | I2C i2c(p28, p27); |
pwheels | 0:551f3dbd553c | 57 | |
pwheels | 0:551f3dbd553c | 58 | void _InitLCD(); |
pwheels | 0:551f3dbd553c | 59 | int _ReadLCD(int Cmd); |
pwheels | 0:551f3dbd553c | 60 | void _WriteLCD(int Cmd, int par1, int par2, int par3); |
pwheels | 0:551f3dbd553c | 61 | void _WriteLCD(int Cmd, int par1, int par2); |
pwheels | 0:551f3dbd553c | 62 | void _WriteLCD(int Cmd, int par1); |
pwheels | 0:551f3dbd553c | 63 | void _WriteLCD(int Cmd); |
pwheels | 0:551f3dbd553c | 64 | void _WriteLCD(int Cmd, char *par1, int par2); |
pwheels | 0:551f3dbd553c | 65 | |
pwheels | 0:551f3dbd553c | 66 | void _InitLCD() { |
pwheels | 0:551f3dbd553c | 67 | i2c.frequency(I2C_SPEED); |
pwheels | 0:551f3dbd553c | 68 | } |
pwheels | 0:551f3dbd553c | 69 | |
pwheels | 0:551f3dbd553c | 70 | int _ReadLCD(int Cmd) { |
pwheels | 0:551f3dbd553c | 71 | |
pwheels | 0:551f3dbd553c | 72 | int j = 50; |
pwheels | 0:551f3dbd553c | 73 | |
pwheels | 0:551f3dbd553c | 74 | char data[2]; |
pwheels | 0:551f3dbd553c | 75 | i2c.start(); |
pwheels | 0:551f3dbd553c | 76 | i2c.write(I2C_ADDR); |
pwheels | 0:551f3dbd553c | 77 | i2c.write(Cmd & 0xff); |
pwheels | 0:551f3dbd553c | 78 | i2c.read(I2C_ADDR, data, 2); |
pwheels | 0:551f3dbd553c | 79 | i2c.stop(); |
pwheels | 0:551f3dbd553c | 80 | wait_us(j); |
pwheels | 0:551f3dbd553c | 81 | return data[1]; |
pwheels | 0:551f3dbd553c | 82 | } |
pwheels | 0:551f3dbd553c | 83 | |
pwheels | 0:551f3dbd553c | 84 | void _WriteLCD(int Cmd, int par1, int par2, int par3) { |
pwheels | 0:551f3dbd553c | 85 | |
pwheels | 0:551f3dbd553c | 86 | int j = 50; |
pwheels | 0:551f3dbd553c | 87 | |
pwheels | 0:551f3dbd553c | 88 | i2c.start(); |
pwheels | 0:551f3dbd553c | 89 | i2c.write(I2C_ADDR); |
pwheels | 0:551f3dbd553c | 90 | i2c.write(Cmd & 0xff); // Set Command |
pwheels | 0:551f3dbd553c | 91 | switch (Cmd) { |
pwheels | 0:551f3dbd553c | 92 | case DisplayON_OFF : |
pwheels | 0:551f3dbd553c | 93 | i2c.write(0x08 + ((par1 & 0x01) << 2) + ((par2 & 0x01) << 1) + (par3 & 0x01)); // 0x00/0x01 for display, cursor and blinking |
pwheels | 0:551f3dbd553c | 94 | break; |
pwheels | 0:551f3dbd553c | 95 | case SetFunction : |
pwheels | 0:551f3dbd553c | 96 | i2c.write(0x20 + ((par1 & 0x01) << 4) + ((par2 & 0x01) << 3) + ((par3 & 0x01) << 2)); // 0x00/0x01 for interface, lines and dots |
pwheels | 0:551f3dbd553c | 97 | break; |
pwheels | 0:551f3dbd553c | 98 | case Delete : |
pwheels | 0:551f3dbd553c | 99 | j = 1000; |
pwheels | 0:551f3dbd553c | 100 | case CopyToClipBoard : |
pwheels | 0:551f3dbd553c | 101 | i2c.write(par1); // line 1-4 |
pwheels | 0:551f3dbd553c | 102 | i2c.write(par2); // position 1-20 |
pwheels | 0:551f3dbd553c | 103 | i2c.write(par3); // count 1-20 |
pwheels | 0:551f3dbd553c | 104 | break; |
pwheels | 0:551f3dbd553c | 105 | default : |
pwheels | 0:551f3dbd553c | 106 | break; |
pwheels | 0:551f3dbd553c | 107 | } |
pwheels | 0:551f3dbd553c | 108 | |
pwheels | 0:551f3dbd553c | 109 | i2c.stop(); |
pwheels | 0:551f3dbd553c | 110 | wait_us(j); |
pwheels | 0:551f3dbd553c | 111 | } |
pwheels | 0:551f3dbd553c | 112 | |
pwheels | 0:551f3dbd553c | 113 | void _WriteLCD(int Cmd, int par1, int par2) { |
pwheels | 0:551f3dbd553c | 114 | |
pwheels | 0:551f3dbd553c | 115 | int j = 50; |
pwheels | 0:551f3dbd553c | 116 | |
pwheels | 0:551f3dbd553c | 117 | i2c.start(); |
pwheels | 0:551f3dbd553c | 118 | i2c.write(I2C_ADDR); |
pwheels | 0:551f3dbd553c | 119 | i2c.write(Cmd & 0xff); // Set Command |
pwheels | 0:551f3dbd553c | 120 | switch (Cmd) { |
pwheels | 0:551f3dbd553c | 121 | case Shift : |
pwheels | 0:551f3dbd553c | 122 | i2c.write(0x10 + ((par1 & 0x01) << 3) + ((par2 & 0x01) << 2)); // 0x00/0x01 cursor or display, 0x00/0x01 left or right |
pwheels | 0:551f3dbd553c | 123 | break; |
pwheels | 0:551f3dbd553c | 124 | case SetEntryMode : |
pwheels | 0:551f3dbd553c | 125 | i2c.write(0x04 + ((par1 & 0x01) << 1) + (par2 & 0x01)); // 0x00/0x01 for cursor move (increase / decrease), display (shifted / not shifted) |
pwheels | 0:551f3dbd553c | 126 | break; |
pwheels | 0:551f3dbd553c | 127 | default : |
pwheels | 0:551f3dbd553c | 128 | break; |
pwheels | 0:551f3dbd553c | 129 | } |
pwheels | 0:551f3dbd553c | 130 | |
pwheels | 0:551f3dbd553c | 131 | i2c.stop(); |
pwheels | 0:551f3dbd553c | 132 | wait_us(j); |
pwheels | 0:551f3dbd553c | 133 | } |
pwheels | 0:551f3dbd553c | 134 | |
pwheels | 0:551f3dbd553c | 135 | void _WriteLCD(int Cmd, int par1) { |
pwheels | 0:551f3dbd553c | 136 | |
pwheels | 0:551f3dbd553c | 137 | int j = 50; |
pwheels | 0:551f3dbd553c | 138 | |
pwheels | 0:551f3dbd553c | 139 | i2c.start(); |
pwheels | 0:551f3dbd553c | 140 | i2c.write(I2C_ADDR); |
pwheels | 0:551f3dbd553c | 141 | i2c.write(Cmd & 0xff); // Set Command |
pwheels | 0:551f3dbd553c | 142 | switch (Cmd) { |
pwheels | 0:551f3dbd553c | 143 | case SetCursor : |
pwheels | 0:551f3dbd553c | 144 | j = 500; |
pwheels | 0:551f3dbd553c | 145 | case CharToLCD : |
pwheels | 0:551f3dbd553c | 146 | case CMDToLCD : |
pwheels | 0:551f3dbd553c | 147 | case SetCGRAMAdr : |
pwheels | 0:551f3dbd553c | 148 | case SetGGRAMAdr : |
pwheels | 0:551f3dbd553c | 149 | i2c.write(par1 & 0xff); // Put character at cursor position |
pwheels | 0:551f3dbd553c | 150 | break; |
pwheels | 0:551f3dbd553c | 151 | case SaveUserDefChr : |
pwheels | 0:551f3dbd553c | 152 | i2c.write(par1 & 0x07); // Save user defined character 0-7 |
pwheels | 0:551f3dbd553c | 153 | break; |
pwheels | 0:551f3dbd553c | 154 | case LCDType : |
pwheels | 0:551f3dbd553c | 155 | i2c.write(par1 & 0x01); // set LCD type 0 = 4 x 16, 1 = 2 x 20 |
pwheels | 0:551f3dbd553c | 156 | break; |
pwheels | 0:551f3dbd553c | 157 | default : |
pwheels | 0:551f3dbd553c | 158 | break; |
pwheels | 0:551f3dbd553c | 159 | } |
pwheels | 0:551f3dbd553c | 160 | |
pwheels | 0:551f3dbd553c | 161 | i2c.stop(); |
pwheels | 0:551f3dbd553c | 162 | wait_us(j); |
pwheels | 0:551f3dbd553c | 163 | } |
pwheels | 0:551f3dbd553c | 164 | |
pwheels | 0:551f3dbd553c | 165 | void _WriteLCD(int Cmd) { |
pwheels | 0:551f3dbd553c | 166 | |
pwheels | 0:551f3dbd553c | 167 | int j = 50; |
pwheels | 0:551f3dbd553c | 168 | |
pwheels | 0:551f3dbd553c | 169 | i2c.start(); |
pwheels | 0:551f3dbd553c | 170 | i2c.write(I2C_ADDR); |
pwheels | 0:551f3dbd553c | 171 | i2c.write(Cmd & 0xff); // Set Command |
pwheels | 0:551f3dbd553c | 172 | switch (Cmd) { |
pwheels | 0:551f3dbd553c | 173 | case ClrDisplay : |
pwheels | 0:551f3dbd553c | 174 | j = 2000; |
pwheels | 0:551f3dbd553c | 175 | break; |
pwheels | 0:551f3dbd553c | 176 | default : |
pwheels | 0:551f3dbd553c | 177 | break; |
pwheels | 0:551f3dbd553c | 178 | } |
pwheels | 0:551f3dbd553c | 179 | i2c.stop(); |
pwheels | 0:551f3dbd553c | 180 | wait_us(j); |
pwheels | 0:551f3dbd553c | 181 | } |
pwheels | 0:551f3dbd553c | 182 | |
pwheels | 0:551f3dbd553c | 183 | void _WriteLCD(int Cmd, char *par1, int par2) { |
pwheels | 2:91dec3d7903a | 184 | // par2 != 0 -> use par2 else actual length of string |
pwheels | 0:551f3dbd553c | 185 | int i; |
pwheels | 0:551f3dbd553c | 186 | int j = 1000; |
pwheels | 0:551f3dbd553c | 187 | |
pwheels | 0:551f3dbd553c | 188 | i2c.start(); |
pwheels | 0:551f3dbd553c | 189 | i2c.write(I2C_ADDR); |
pwheels | 0:551f3dbd553c | 190 | i2c.write(Cmd & 0xff); // Set Command |
pwheels | 2:91dec3d7903a | 191 | for (i = 0; i < (par2 != 0) ? par2 : strlen(par1); i++) { |
pwheels | 0:551f3dbd553c | 192 | i2c.write(par1[i]); |
pwheels | 0:551f3dbd553c | 193 | } |
pwheels | 0:551f3dbd553c | 194 | i2c.stop(); |
pwheels | 0:551f3dbd553c | 195 | wait_us(j); |
pwheels | 0:551f3dbd553c | 196 | } |