Convenience routines for an I"C connected LCD display. Handy things like taking cursor to home, positioning cursor, clearing display, writing strings etc
jtlcd.cpp@4:ce867009531a, 2012-11-11 (annotated)
- Committer:
- jont
- Date:
- Sun Nov 11 09:43:33 2012 +0000
- Revision:
- 4:ce867009531a
- Parent:
- 3:5744bf6006e1
- Child:
- 6:31bcd2c72da9
nowt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jont | 0:8f724a47a820 | 1 | #include "mbed.h" |
jont | 0:8f724a47a820 | 2 | #include "jtlcd.h" |
jont | 2:5b220477045b | 3 | #include "stdarg.h" |
jont | 2:5b220477045b | 4 | #include "stdio.h" |
jont | 2:5b220477045b | 5 | #define LCDCONTROL 0x00 |
jont | 2:5b220477045b | 6 | #define LCDDATA 0x40 |
jont | 2:5b220477045b | 7 | |
jont | 2:5b220477045b | 8 | #define LCDBOTTOMSTART 0x40 /* start address of bottom line */ |
jont | 2:5b220477045b | 9 | #define LCDTOPSTART 0x00 /* start address of top line */ |
jont | 0:8f724a47a820 | 10 | |
jont | 0:8f724a47a820 | 11 | I2C i2c(p28, p27); // sda, scl |
jont | 2:5b220477045b | 12 | |
jont | 2:5b220477045b | 13 | |
jont | 2:5b220477045b | 14 | /*==============================================================*/ |
jont | 2:5b220477045b | 15 | /* LcdGetAddress */ |
jont | 2:5b220477045b | 16 | /*==============================================================*/ |
jont | 2:5b220477045b | 17 | /* |
jont | 2:5b220477045b | 18 | int LcdGetAddress(void) |
jont | 2:5b220477045b | 19 | { |
jont | 2:5b220477045b | 20 | int it; // current address position |
jont | 2:5b220477045b | 21 | it =read_E_port( LCDCONTROL); |
jont | 2:5b220477045b | 22 | return(it); |
jont | 2:5b220477045b | 23 | |
jont | 2:5b220477045b | 24 | } |
jont | 2:5b220477045b | 25 | */ |
jont | 2:5b220477045b | 26 | /*==============================================================*/ |
jont | 2:5b220477045b | 27 | /* LcdSetAddress */ |
jont | 2:5b220477045b | 28 | /*==============================================================*/ |
jont | 2:5b220477045b | 29 | |
jont | 2:5b220477045b | 30 | void LcdSetAddress(int address) |
jont | 2:5b220477045b | 31 | { |
jont | 2:5b220477045b | 32 | // LcdReady(); |
jont | 2:5b220477045b | 33 | address |= 0x80; /* write cg/dd ram address */ |
jont | 2:5b220477045b | 34 | write_E_port(LCDCONTROL, address); |
jont | 2:5b220477045b | 35 | } |
jont | 2:5b220477045b | 36 | |
jont | 2:5b220477045b | 37 | |
jont | 2:5b220477045b | 38 | /*==============================================================*/ |
jont | 2:5b220477045b | 39 | /* LcdClear */ |
jont | 2:5b220477045b | 40 | /*==============================================================*/ |
jont | 2:5b220477045b | 41 | void LcdClear(void) |
jont | 2:5b220477045b | 42 | { |
jont | 2:5b220477045b | 43 | // LcdReady(); |
jont | 2:5b220477045b | 44 | write_E_port(LCDCONTROL, 0x01); |
jont | 2:5b220477045b | 45 | } |
jont | 2:5b220477045b | 46 | /*==============================================================*/ |
jont | 2:5b220477045b | 47 | /* LcdHomeTop */ |
jont | 2:5b220477045b | 48 | /*==============================================================*/ |
jont | 2:5b220477045b | 49 | void LcdHomeTop(void) |
jont | 2:5b220477045b | 50 | { |
jont | 2:5b220477045b | 51 | // LcdReady(); |
jont | 2:5b220477045b | 52 | write_E_port(LCDCONTROL, 0x02); |
jont | 2:5b220477045b | 53 | } |
jont | 2:5b220477045b | 54 | /*==============================================================*/ |
jont | 2:5b220477045b | 55 | /* LcdHomeBottom */ |
jont | 2:5b220477045b | 56 | /*==============================================================*/ |
jont | 2:5b220477045b | 57 | void LcdHomeBottom(void) |
jont | 2:5b220477045b | 58 | { |
jont | 2:5b220477045b | 59 | //LcdReady(); |
jont | 2:5b220477045b | 60 | write_E_port(LCDCONTROL, 0xc0); |
jont | 2:5b220477045b | 61 | } |
jont | 2:5b220477045b | 62 | |
jont | 2:5b220477045b | 63 | /*==============================================================*/ |
jont | 2:5b220477045b | 64 | /* LcdCursorLeft */ |
jont | 2:5b220477045b | 65 | /*==============================================================*/ |
jont | 2:5b220477045b | 66 | void LcdCursorLeft(void) |
jont | 2:5b220477045b | 67 | { |
jont | 2:5b220477045b | 68 | //LcdReady(); |
jont | 2:5b220477045b | 69 | write_E_port(LCDCONTROL, 0x10); |
jont | 2:5b220477045b | 70 | } |
jont | 2:5b220477045b | 71 | |
jont | 2:5b220477045b | 72 | /*==============================================================*/ |
jont | 2:5b220477045b | 73 | /* LcdCursorRight */ |
jont | 2:5b220477045b | 74 | /*==============================================================*/ |
jont | 2:5b220477045b | 75 | void LcdCursorRight(void) |
jont | 2:5b220477045b | 76 | { |
jont | 2:5b220477045b | 77 | //LcdReady(); |
jont | 2:5b220477045b | 78 | write_E_port(LCDCONTROL, 0x14); |
jont | 2:5b220477045b | 79 | } |
jont | 2:5b220477045b | 80 | |
jont | 2:5b220477045b | 81 | /*==============================================================*/ |
jont | 2:5b220477045b | 82 | /* LcdSpace */ |
jont | 2:5b220477045b | 83 | /*==============================================================*/ |
jont | 2:5b220477045b | 84 | void LcdSpace(void) |
jont | 2:5b220477045b | 85 | { |
jont | 2:5b220477045b | 86 | // LcdReady(); |
jont | 2:5b220477045b | 87 | write_E_port(LCDDATA, 0x20); |
jont | 2:5b220477045b | 88 | } |
jont | 2:5b220477045b | 89 | |
jont | 2:5b220477045b | 90 | /*==============================================================*/ |
jont | 2:5b220477045b | 91 | /* LcdNSpace */ |
jont | 2:5b220477045b | 92 | /*==============================================================*/ |
jont | 2:5b220477045b | 93 | void LcdNSpace(int number) |
jont | 2:5b220477045b | 94 | |
jont | 2:5b220477045b | 95 | { |
jont | 2:5b220477045b | 96 | int count; |
jont | 2:5b220477045b | 97 | for (count = 0; count < number; count++) |
jont | 2:5b220477045b | 98 | { |
jont | 2:5b220477045b | 99 | LcdSpace(); |
jont | 2:5b220477045b | 100 | } |
jont | 2:5b220477045b | 101 | } |
jont | 2:5b220477045b | 102 | |
jont | 2:5b220477045b | 103 | /*==============================================================*/ |
jont | 2:5b220477045b | 104 | /* LcdCursorNRight */ |
jont | 2:5b220477045b | 105 | /*==============================================================*/ |
jont | 2:5b220477045b | 106 | void LcdCursorNRight(int number) |
jont | 2:5b220477045b | 107 | { |
jont | 2:5b220477045b | 108 | for (int n=0; n< number; n++) |
jont | 2:5b220477045b | 109 | { |
jont | 2:5b220477045b | 110 | LcdCursorRight(); |
jont | 2:5b220477045b | 111 | } |
jont | 2:5b220477045b | 112 | } |
jont | 2:5b220477045b | 113 | |
jont | 2:5b220477045b | 114 | /*==============================================================*/ |
jont | 2:5b220477045b | 115 | /* LcdNLeft */ |
jont | 2:5b220477045b | 116 | /*==============================================================*/ |
jont | 2:5b220477045b | 117 | void LcdCursorNLeft(int number) |
jont | 2:5b220477045b | 118 | |
jont | 2:5b220477045b | 119 | { |
jont | 2:5b220477045b | 120 | for (int n=0; n< number; n++) |
jont | 2:5b220477045b | 121 | { |
jont | 2:5b220477045b | 122 | LcdCursorLeft(); |
jont | 2:5b220477045b | 123 | } |
jont | 2:5b220477045b | 124 | } |
jont | 2:5b220477045b | 125 | |
jont | 2:5b220477045b | 126 | |
jont | 2:5b220477045b | 127 | /*==============================================================*/ |
jont | 2:5b220477045b | 128 | /* LcdClearTop */ |
jont | 2:5b220477045b | 129 | /*==============================================================*/ |
jont | 2:5b220477045b | 130 | void LcdClearTop(void) |
jont | 2:5b220477045b | 131 | { |
jont | 2:5b220477045b | 132 | LcdHomeTop(); |
jont | 2:5b220477045b | 133 | LcdNSpace(0x27); |
jont | 2:5b220477045b | 134 | LcdHomeTop(); |
jont | 2:5b220477045b | 135 | |
jont | 2:5b220477045b | 136 | } |
jont | 2:5b220477045b | 137 | |
jont | 2:5b220477045b | 138 | /*==============================================================*/ |
jont | 2:5b220477045b | 139 | /* LcdClearBottom */ |
jont | 2:5b220477045b | 140 | /*==============================================================*/ |
jont | 2:5b220477045b | 141 | void LcdClearBottom(void) |
jont | 2:5b220477045b | 142 | { |
jont | 2:5b220477045b | 143 | LcdHomeBottom(); |
jont | 2:5b220477045b | 144 | LcdNSpace(0x27); |
jont | 2:5b220477045b | 145 | LcdHomeBottom(); |
jont | 2:5b220477045b | 146 | |
jont | 2:5b220477045b | 147 | } |
jont | 2:5b220477045b | 148 | |
jont | 2:5b220477045b | 149 | /*==============================================================*/ |
jont | 2:5b220477045b | 150 | /* LcdPositionBottom */ |
jont | 2:5b220477045b | 151 | /*==============================================================*/ |
jont | 2:5b220477045b | 152 | void LcdPositionBottom(int pos) |
jont | 2:5b220477045b | 153 | |
jont | 2:5b220477045b | 154 | { |
jont | 2:5b220477045b | 155 | LcdSetAddress(LCDBOTTOMSTART + pos); |
jont | 2:5b220477045b | 156 | |
jont | 2:5b220477045b | 157 | } |
jont | 2:5b220477045b | 158 | |
jont | 2:5b220477045b | 159 | /*==============================================================*/ |
jont | 2:5b220477045b | 160 | /* LcdPositionTop */ |
jont | 2:5b220477045b | 161 | /*==============================================================*/ |
jont | 2:5b220477045b | 162 | void LcdPositionTop(int pos) |
jont | 2:5b220477045b | 163 | |
jont | 2:5b220477045b | 164 | { |
jont | 2:5b220477045b | 165 | LcdSetAddress(LCDTOPSTART + pos); |
jont | 2:5b220477045b | 166 | } |
jont | 2:5b220477045b | 167 | |
jont | 2:5b220477045b | 168 | |
jont | 2:5b220477045b | 169 | /*==============================================================*/ |
jont | 2:5b220477045b | 170 | /* LcdCursorFlash */ |
jont | 2:5b220477045b | 171 | /*==============================================================*/ |
jont | 2:5b220477045b | 172 | void LcdCursorFlash(void) |
jont | 2:5b220477045b | 173 | { |
jont | 2:5b220477045b | 174 | //LcdReady(); |
jont | 2:5b220477045b | 175 | write_E_port(LCDCONTROL, 0x0d); |
jont | 2:5b220477045b | 176 | } |
jont | 2:5b220477045b | 177 | |
jont | 2:5b220477045b | 178 | /*==============================================================*/ |
jont | 2:5b220477045b | 179 | /* LcdCursorOff */ |
jont | 2:5b220477045b | 180 | /*==============================================================*/ |
jont | 2:5b220477045b | 181 | void LcdCursorOff(void) |
jont | 2:5b220477045b | 182 | { |
jont | 2:5b220477045b | 183 | // LcdReady(); |
jont | 2:5b220477045b | 184 | write_E_port(LCDCONTROL, 0x0c); |
jont | 2:5b220477045b | 185 | } |
jont | 2:5b220477045b | 186 | |
jont | 2:5b220477045b | 187 | |
jont | 2:5b220477045b | 188 | /*==============================================================*/ |
jont | 2:5b220477045b | 189 | /* LcdCursorNorm */ |
jont | 2:5b220477045b | 190 | /*==============================================================*/ |
jont | 2:5b220477045b | 191 | void LcdCursorNorm(void) |
jont | 2:5b220477045b | 192 | { |
jont | 2:5b220477045b | 193 | // LcdReady(); |
jont | 2:5b220477045b | 194 | write_E_port(LCDCONTROL, 0x0e); |
jont | 2:5b220477045b | 195 | } |
jont | 2:5b220477045b | 196 | |
jont | 2:5b220477045b | 197 | |
jont | 2:5b220477045b | 198 | |
jont | 2:5b220477045b | 199 | |
jont | 2:5b220477045b | 200 | |
jont | 2:5b220477045b | 201 | /* |
jont | 2:5b220477045b | 202 | |
jont | 2:5b220477045b | 203 | void read_E_port(unsigned char command) |
jont | 2:5b220477045b | 204 | { |
jont | 2:5b220477045b | 205 | i2c.start(); |
jont | 2:5b220477045b | 206 | i2c.write(Slave); |
jont | 2:5b220477045b | 207 | i2c.write(command); |
jont | 2:5b220477045b | 208 | i2c.stop(); |
jont | 2:5b220477045b | 209 | wait(0.2); |
jont | 2:5b220477045b | 210 | } |
jont | 2:5b220477045b | 211 | */ |
jont | 2:5b220477045b | 212 | |
jont | 2:5b220477045b | 213 | void write_E_port(unsigned char command,unsigned char data) |
jont | 2:5b220477045b | 214 | { |
jont | 2:5b220477045b | 215 | i2c.start(); |
jont | 2:5b220477045b | 216 | i2c.write(Slave); |
jont | 2:5b220477045b | 217 | i2c.write(command); |
jont | 2:5b220477045b | 218 | i2c.write(data); |
jont | 2:5b220477045b | 219 | i2c.stop(); |
jont | 2:5b220477045b | 220 | wait(0.01); |
jont | 2:5b220477045b | 221 | } |
jont | 2:5b220477045b | 222 | |
jont | 2:5b220477045b | 223 | void LcdInit() |
jont | 0:8f724a47a820 | 224 | { |
jont | 0:8f724a47a820 | 225 | |
jont | 0:8f724a47a820 | 226 | i2c.start(); |
jont | 0:8f724a47a820 | 227 | i2c.write(Slave); |
jont | 0:8f724a47a820 | 228 | i2c.write(0x00); |
jont | 0:8f724a47a820 | 229 | i2c.write(0x38); |
jont | 0:8f724a47a820 | 230 | wait(0.01); |
jont | 0:8f724a47a820 | 231 | i2c.write(0x39); //i assume now we juyst need data? |
jont | 0:8f724a47a820 | 232 | wait(0.01); |
jont | 0:8f724a47a820 | 233 | i2c.write(0x14); |
jont | 0:8f724a47a820 | 234 | i2c.write(0x74); |
jont | 0:8f724a47a820 | 235 | i2c.write(0x54); |
jont | 0:8f724a47a820 | 236 | i2c.write(0x6f); |
jont | 0:8f724a47a820 | 237 | i2c.write(0x0c); |
jont | 0:8f724a47a820 | 238 | i2c.write(0x01); |
jont | 0:8f724a47a820 | 239 | i2c.write(0x06); |
jont | 0:8f724a47a820 | 240 | wait(0.01); |
jont | 0:8f724a47a820 | 241 | i2c.stop(); |
jont | 0:8f724a47a820 | 242 | |
jont | 0:8f724a47a820 | 243 | |
jont | 0:8f724a47a820 | 244 | } |
jont | 2:5b220477045b | 245 | /* |
jont | 0:8f724a47a820 | 246 | void lcdclear() |
jont | 0:8f724a47a820 | 247 | { |
jont | 0:8f724a47a820 | 248 | i2c.start(); |
jont | 0:8f724a47a820 | 249 | i2c.write(Slave); |
jont | 0:8f724a47a820 | 250 | i2c.write(0x00); |
jont | 0:8f724a47a820 | 251 | i2c.write(0x01); |
jont | 0:8f724a47a820 | 252 | i2c.stop(); |
jont | 0:8f724a47a820 | 253 | wait(0.2); |
jont | 0:8f724a47a820 | 254 | } |
jont | 2:5b220477045b | 255 | */ |
jont | 0:8f724a47a820 | 256 | void test() |
jont | 0:8f724a47a820 | 257 | { |
jont | 0:8f724a47a820 | 258 | |
jont | 0:8f724a47a820 | 259 | i2c.start(); |
jont | 0:8f724a47a820 | 260 | i2c.write(Slave); |
jont | 0:8f724a47a820 | 261 | i2c.stop(); |
jont | 0:8f724a47a820 | 262 | wait(0.2); |
jont | 0:8f724a47a820 | 263 | } |
jont | 0:8f724a47a820 | 264 | |
jont | 0:8f724a47a820 | 265 | |
jont | 2:5b220477045b | 266 | void LcdWriteText(char *text) |
jont | 0:8f724a47a820 | 267 | { |
jont | 1:2ded47079af1 | 268 | int n;//,d; |
jont | 1:2ded47079af1 | 269 | //d=0x00; |
jont | 0:8f724a47a820 | 270 | int length = strlen(text); |
jont | 0:8f724a47a820 | 271 | i2c.start(); |
jont | 0:8f724a47a820 | 272 | i2c.write(Slave); //Slave=0x78 |
jont | 2:5b220477045b | 273 | i2c.write(LCDDATA);//Datasend=0x40 |
jont | 0:8f724a47a820 | 274 | for(n=0;n<length;n++){ |
jont | 0:8f724a47a820 | 275 | i2c.write(*text); |
jont | 0:8f724a47a820 | 276 | ++text; |
jont | 0:8f724a47a820 | 277 | } |
jont | 0:8f724a47a820 | 278 | i2c.stop(); |
jont | 2:5b220477045b | 279 | } |
jont | 2:5b220477045b | 280 | |
jont | 2:5b220477045b | 281 | /*==============================================================*/ |
jont | 3:5744bf6006e1 | 282 | /* Writes to LCD and fills rest of line with space */ |
jont | 3:5744bf6006e1 | 283 | /*==============================================================*/ |
jont | 3:5744bf6006e1 | 284 | void LcdWriteTextAndFill(char *text, int numb) |
jont | 3:5744bf6006e1 | 285 | { |
jont | 3:5744bf6006e1 | 286 | |
jont | 3:5744bf6006e1 | 287 | int length = strlen(text); |
jont | 3:5744bf6006e1 | 288 | int diff = numb -length; |
jont | 3:5744bf6006e1 | 289 | LcdWriteText(text); |
jont | 3:5744bf6006e1 | 290 | if (diff >0) |
jont | 3:5744bf6006e1 | 291 | { |
jont | 3:5744bf6006e1 | 292 | for (int n=0; n <= diff; n++) |
jont | 3:5744bf6006e1 | 293 | LcdWriteText(" "); |
jont | 3:5744bf6006e1 | 294 | |
jont | 3:5744bf6006e1 | 295 | } |
jont | 3:5744bf6006e1 | 296 | } |
jont | 3:5744bf6006e1 | 297 | |
jont | 3:5744bf6006e1 | 298 | /*==============================================================*/ |
jont | 4:ce867009531a | 299 | /* Writes to LCD and fills rest of line with space based on lcd maxlength */ |
jont | 4:ce867009531a | 300 | /*==============================================================*/ |
jont | 4:ce867009531a | 301 | void LcdWriteTextLine(char *text) |
jont | 4:ce867009531a | 302 | { |
jont | 4:ce867009531a | 303 | |
jont | 4:ce867009531a | 304 | int length = strlen(text); |
jont | 4:ce867009531a | 305 | int diff = LCD_LINE_SIZE -length; |
jont | 4:ce867009531a | 306 | LcdWriteText(text); |
jont | 4:ce867009531a | 307 | if (diff >0) |
jont | 4:ce867009531a | 308 | { |
jont | 4:ce867009531a | 309 | for (int n=0; n <= diff; n++) |
jont | 4:ce867009531a | 310 | LcdWriteText(" "); |
jont | 4:ce867009531a | 311 | |
jont | 4:ce867009531a | 312 | } |
jont | 4:ce867009531a | 313 | } |
jont | 4:ce867009531a | 314 | /*==============================================================*/ |
jont | 2:5b220477045b | 315 | /* attemp at lcdlcdprintf */ |
jont | 2:5b220477045b | 316 | /*==============================================================*/ |
jont | 2:5b220477045b | 317 | |
jont | 2:5b220477045b | 318 | static void lcdputchar(char c, void *ptr) |
jont | 2:5b220477045b | 319 | { |
jont | 2:5b220477045b | 320 | //LcdReady(); /* wait for lcd ready */ |
jont | 2:5b220477045b | 321 | write_E_port(LCDDATA, c); /* send data to lcd */ |
jont | 2:5b220477045b | 322 | } |
jont | 2:5b220477045b | 323 | /* |
jont | 2:5b220477045b | 324 | int lcdprintf(const char *format, ...) |
jont | 2:5b220477045b | 325 | { |
jont | 2:5b220477045b | 326 | va_list ap; |
jont | 2:5b220477045b | 327 | int nr_of_chars; |
jont | 2:5b220477045b | 328 | va_start (ap, format); //variable arg begin |
jont | 2:5b220477045b | 329 | nr_of_chars = _formatted_write (format, lcdputchar, (void *) 0, ap); |
jont | 2:5b220477045b | 330 | va_end(ap); //variable arg end |
jont | 2:5b220477045b | 331 | return(nr_of_chars); |
jont | 2:5b220477045b | 332 | } */ |
jont | 2:5b220477045b | 333 |