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