Convenience routines for an I"C connected LCD display. Handy things like taking cursor to home, positioning cursor, clearing display, writing strings etc

Dependents:   gu_squirt_tester

Committer:
jont
Date:
Sat Feb 14 08:55:12 2015 +0000
Revision:
6:31bcd2c72da9
Parent:
4:ce867009531a
Child:
7:5b0b6167b507
First public release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jont 6:31bcd2c72da9 1
jont 6:31bcd2c72da9 2 /* Copyright (c) 2015 jont@ninelocks.com, MIT License
jont 6:31bcd2c72da9 3 *
jont 6:31bcd2c72da9 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jont 6:31bcd2c72da9 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
jont 6:31bcd2c72da9 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
jont 6:31bcd2c72da9 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
jont 6:31bcd2c72da9 8 * furnished to do so, subject to the following conditions:
jont 6:31bcd2c72da9 9 *
jont 6:31bcd2c72da9 10 * The above copyright notice and this permission notice shall be included in all copies or
jont 6:31bcd2c72da9 11 * substantial portions of the Software.
jont 6:31bcd2c72da9 12 *
jont 6:31bcd2c72da9 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jont 6:31bcd2c72da9 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jont 6:31bcd2c72da9 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jont 6:31bcd2c72da9 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jont 6:31bcd2c72da9 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jont 6:31bcd2c72da9 18 */
jont 6:31bcd2c72da9 19
jont 6:31bcd2c72da9 20 /*
jont 6:31bcd2c72da9 21 jont@ninelocks.com
jont 6:31bcd2c72da9 22
jont 6:31bcd2c72da9 23 Code to interface to LCD dot matric display connected via the I2C Bus
jont 6:31bcd2c72da9 24
jont 6:31bcd2c72da9 25 Some of this dates back to an H8 project from the 90's
jont 6:31bcd2c72da9 26
jont 6:31bcd2c72da9 27 Isnt C a truly wonderous thing :-)
jont 6:31bcd2c72da9 28
jont 6:31bcd2c72da9 29 */
jont 0:8f724a47a820 30 #include "mbed.h"
jont 0:8f724a47a820 31 #include "jtlcd.h"
jont 2:5b220477045b 32 #include "stdarg.h"
jont 2:5b220477045b 33 #include "stdio.h"
jont 2:5b220477045b 34 #define LCDCONTROL 0x00
jont 2:5b220477045b 35 #define LCDDATA 0x40
jont 2:5b220477045b 36
jont 2:5b220477045b 37 #define LCDBOTTOMSTART 0x40 /* start address of bottom line */
jont 2:5b220477045b 38 #define LCDTOPSTART 0x00 /* start address of top line */
jont 0:8f724a47a820 39
jont 0:8f724a47a820 40 I2C i2c(p28, p27); // sda, scl
jont 2:5b220477045b 41
jont 2:5b220477045b 42
jont 2:5b220477045b 43 /*==============================================================*/
jont 2:5b220477045b 44 /* LcdGetAddress */
jont 2:5b220477045b 45 /*==============================================================*/
jont 2:5b220477045b 46 /*
jont 2:5b220477045b 47 int LcdGetAddress(void)
jont 2:5b220477045b 48 {
jont 2:5b220477045b 49 int it; // current address position
jont 2:5b220477045b 50 it =read_E_port( LCDCONTROL);
jont 2:5b220477045b 51 return(it);
jont 2:5b220477045b 52
jont 2:5b220477045b 53 }
jont 2:5b220477045b 54 */
jont 2:5b220477045b 55 /*==============================================================*/
jont 2:5b220477045b 56 /* LcdSetAddress */
jont 2:5b220477045b 57 /*==============================================================*/
jont 2:5b220477045b 58
jont 2:5b220477045b 59 void LcdSetAddress(int address)
jont 2:5b220477045b 60 {
jont 2:5b220477045b 61 // LcdReady();
jont 2:5b220477045b 62 address |= 0x80; /* write cg/dd ram address */
jont 2:5b220477045b 63 write_E_port(LCDCONTROL, address);
jont 2:5b220477045b 64 }
jont 2:5b220477045b 65
jont 2:5b220477045b 66
jont 2:5b220477045b 67 /*==============================================================*/
jont 2:5b220477045b 68 /* LcdClear */
jont 2:5b220477045b 69 /*==============================================================*/
jont 2:5b220477045b 70 void LcdClear(void)
jont 2:5b220477045b 71 {
jont 2:5b220477045b 72 // LcdReady();
jont 2:5b220477045b 73 write_E_port(LCDCONTROL, 0x01);
jont 2:5b220477045b 74 }
jont 2:5b220477045b 75 /*==============================================================*/
jont 2:5b220477045b 76 /* LcdHomeTop */
jont 2:5b220477045b 77 /*==============================================================*/
jont 2:5b220477045b 78 void LcdHomeTop(void)
jont 2:5b220477045b 79 {
jont 2:5b220477045b 80 // LcdReady();
jont 2:5b220477045b 81 write_E_port(LCDCONTROL, 0x02);
jont 2:5b220477045b 82 }
jont 2:5b220477045b 83 /*==============================================================*/
jont 2:5b220477045b 84 /* LcdHomeBottom */
jont 2:5b220477045b 85 /*==============================================================*/
jont 2:5b220477045b 86 void LcdHomeBottom(void)
jont 2:5b220477045b 87 {
jont 2:5b220477045b 88 //LcdReady();
jont 2:5b220477045b 89 write_E_port(LCDCONTROL, 0xc0);
jont 2:5b220477045b 90 }
jont 2:5b220477045b 91
jont 2:5b220477045b 92 /*==============================================================*/
jont 2:5b220477045b 93 /* LcdCursorLeft */
jont 2:5b220477045b 94 /*==============================================================*/
jont 2:5b220477045b 95 void LcdCursorLeft(void)
jont 2:5b220477045b 96 {
jont 2:5b220477045b 97 //LcdReady();
jont 2:5b220477045b 98 write_E_port(LCDCONTROL, 0x10);
jont 2:5b220477045b 99 }
jont 2:5b220477045b 100
jont 2:5b220477045b 101 /*==============================================================*/
jont 2:5b220477045b 102 /* LcdCursorRight */
jont 2:5b220477045b 103 /*==============================================================*/
jont 2:5b220477045b 104 void LcdCursorRight(void)
jont 2:5b220477045b 105 {
jont 2:5b220477045b 106 //LcdReady();
jont 2:5b220477045b 107 write_E_port(LCDCONTROL, 0x14);
jont 2:5b220477045b 108 }
jont 2:5b220477045b 109
jont 2:5b220477045b 110 /*==============================================================*/
jont 2:5b220477045b 111 /* LcdSpace */
jont 2:5b220477045b 112 /*==============================================================*/
jont 2:5b220477045b 113 void LcdSpace(void)
jont 2:5b220477045b 114 {
jont 2:5b220477045b 115 // LcdReady();
jont 2:5b220477045b 116 write_E_port(LCDDATA, 0x20);
jont 2:5b220477045b 117 }
jont 2:5b220477045b 118
jont 2:5b220477045b 119 /*==============================================================*/
jont 2:5b220477045b 120 /* LcdNSpace */
jont 2:5b220477045b 121 /*==============================================================*/
jont 2:5b220477045b 122 void LcdNSpace(int number)
jont 2:5b220477045b 123
jont 2:5b220477045b 124 {
jont 2:5b220477045b 125 int count;
jont 2:5b220477045b 126 for (count = 0; count < number; count++)
jont 2:5b220477045b 127 {
jont 2:5b220477045b 128 LcdSpace();
jont 2:5b220477045b 129 }
jont 2:5b220477045b 130 }
jont 2:5b220477045b 131
jont 2:5b220477045b 132 /*==============================================================*/
jont 2:5b220477045b 133 /* LcdCursorNRight */
jont 2:5b220477045b 134 /*==============================================================*/
jont 2:5b220477045b 135 void LcdCursorNRight(int number)
jont 2:5b220477045b 136 {
jont 2:5b220477045b 137 for (int n=0; n< number; n++)
jont 2:5b220477045b 138 {
jont 2:5b220477045b 139 LcdCursorRight();
jont 2:5b220477045b 140 }
jont 2:5b220477045b 141 }
jont 2:5b220477045b 142
jont 2:5b220477045b 143 /*==============================================================*/
jont 2:5b220477045b 144 /* LcdNLeft */
jont 2:5b220477045b 145 /*==============================================================*/
jont 2:5b220477045b 146 void LcdCursorNLeft(int number)
jont 2:5b220477045b 147
jont 2:5b220477045b 148 {
jont 2:5b220477045b 149 for (int n=0; n< number; n++)
jont 2:5b220477045b 150 {
jont 2:5b220477045b 151 LcdCursorLeft();
jont 2:5b220477045b 152 }
jont 2:5b220477045b 153 }
jont 2:5b220477045b 154
jont 2:5b220477045b 155
jont 2:5b220477045b 156 /*==============================================================*/
jont 2:5b220477045b 157 /* LcdClearTop */
jont 2:5b220477045b 158 /*==============================================================*/
jont 2:5b220477045b 159 void LcdClearTop(void)
jont 2:5b220477045b 160 {
jont 2:5b220477045b 161 LcdHomeTop();
jont 2:5b220477045b 162 LcdNSpace(0x27);
jont 2:5b220477045b 163 LcdHomeTop();
jont 2:5b220477045b 164
jont 2:5b220477045b 165 }
jont 2:5b220477045b 166
jont 2:5b220477045b 167 /*==============================================================*/
jont 2:5b220477045b 168 /* LcdClearBottom */
jont 2:5b220477045b 169 /*==============================================================*/
jont 2:5b220477045b 170 void LcdClearBottom(void)
jont 2:5b220477045b 171 {
jont 2:5b220477045b 172 LcdHomeBottom();
jont 2:5b220477045b 173 LcdNSpace(0x27);
jont 2:5b220477045b 174 LcdHomeBottom();
jont 2:5b220477045b 175
jont 2:5b220477045b 176 }
jont 2:5b220477045b 177
jont 2:5b220477045b 178 /*==============================================================*/
jont 2:5b220477045b 179 /* LcdPositionBottom */
jont 2:5b220477045b 180 /*==============================================================*/
jont 2:5b220477045b 181 void LcdPositionBottom(int pos)
jont 2:5b220477045b 182
jont 2:5b220477045b 183 {
jont 2:5b220477045b 184 LcdSetAddress(LCDBOTTOMSTART + pos);
jont 2:5b220477045b 185
jont 2:5b220477045b 186 }
jont 2:5b220477045b 187
jont 2:5b220477045b 188 /*==============================================================*/
jont 2:5b220477045b 189 /* LcdPositionTop */
jont 2:5b220477045b 190 /*==============================================================*/
jont 2:5b220477045b 191 void LcdPositionTop(int pos)
jont 2:5b220477045b 192
jont 2:5b220477045b 193 {
jont 2:5b220477045b 194 LcdSetAddress(LCDTOPSTART + pos);
jont 2:5b220477045b 195 }
jont 2:5b220477045b 196
jont 2:5b220477045b 197
jont 2:5b220477045b 198 /*==============================================================*/
jont 2:5b220477045b 199 /* LcdCursorFlash */
jont 2:5b220477045b 200 /*==============================================================*/
jont 2:5b220477045b 201 void LcdCursorFlash(void)
jont 2:5b220477045b 202 {
jont 2:5b220477045b 203 //LcdReady();
jont 2:5b220477045b 204 write_E_port(LCDCONTROL, 0x0d);
jont 2:5b220477045b 205 }
jont 2:5b220477045b 206
jont 2:5b220477045b 207 /*==============================================================*/
jont 2:5b220477045b 208 /* LcdCursorOff */
jont 2:5b220477045b 209 /*==============================================================*/
jont 2:5b220477045b 210 void LcdCursorOff(void)
jont 2:5b220477045b 211 {
jont 2:5b220477045b 212 // LcdReady();
jont 2:5b220477045b 213 write_E_port(LCDCONTROL, 0x0c);
jont 2:5b220477045b 214 }
jont 2:5b220477045b 215
jont 2:5b220477045b 216
jont 2:5b220477045b 217 /*==============================================================*/
jont 2:5b220477045b 218 /* LcdCursorNorm */
jont 2:5b220477045b 219 /*==============================================================*/
jont 2:5b220477045b 220 void LcdCursorNorm(void)
jont 2:5b220477045b 221 {
jont 2:5b220477045b 222 // LcdReady();
jont 2:5b220477045b 223 write_E_port(LCDCONTROL, 0x0e);
jont 2:5b220477045b 224 }
jont 2:5b220477045b 225
jont 2:5b220477045b 226
jont 2:5b220477045b 227
jont 2:5b220477045b 228
jont 2:5b220477045b 229
jont 2:5b220477045b 230 /*
jont 2:5b220477045b 231
jont 2:5b220477045b 232 void read_E_port(unsigned char command)
jont 2:5b220477045b 233 {
jont 2:5b220477045b 234 i2c.start();
jont 2:5b220477045b 235 i2c.write(Slave);
jont 2:5b220477045b 236 i2c.write(command);
jont 2:5b220477045b 237 i2c.stop();
jont 2:5b220477045b 238 wait(0.2);
jont 2:5b220477045b 239 }
jont 2:5b220477045b 240 */
jont 2:5b220477045b 241
jont 2:5b220477045b 242 void write_E_port(unsigned char command,unsigned char data)
jont 2:5b220477045b 243 {
jont 2:5b220477045b 244 i2c.start();
jont 2:5b220477045b 245 i2c.write(Slave);
jont 2:5b220477045b 246 i2c.write(command);
jont 2:5b220477045b 247 i2c.write(data);
jont 2:5b220477045b 248 i2c.stop();
jont 2:5b220477045b 249 wait(0.01);
jont 2:5b220477045b 250 }
jont 2:5b220477045b 251
jont 2:5b220477045b 252 void LcdInit()
jont 0:8f724a47a820 253 {
jont 0:8f724a47a820 254
jont 0:8f724a47a820 255 i2c.start();
jont 0:8f724a47a820 256 i2c.write(Slave);
jont 0:8f724a47a820 257 i2c.write(0x00);
jont 0:8f724a47a820 258 i2c.write(0x38);
jont 0:8f724a47a820 259 wait(0.01);
jont 0:8f724a47a820 260 i2c.write(0x39); //i assume now we juyst need data?
jont 0:8f724a47a820 261 wait(0.01);
jont 0:8f724a47a820 262 i2c.write(0x14);
jont 0:8f724a47a820 263 i2c.write(0x74);
jont 0:8f724a47a820 264 i2c.write(0x54);
jont 0:8f724a47a820 265 i2c.write(0x6f);
jont 0:8f724a47a820 266 i2c.write(0x0c);
jont 0:8f724a47a820 267 i2c.write(0x01);
jont 0:8f724a47a820 268 i2c.write(0x06);
jont 0:8f724a47a820 269 wait(0.01);
jont 0:8f724a47a820 270 i2c.stop();
jont 0:8f724a47a820 271
jont 0:8f724a47a820 272
jont 0:8f724a47a820 273 }
jont 2:5b220477045b 274 /*
jont 0:8f724a47a820 275 void lcdclear()
jont 0:8f724a47a820 276 {
jont 0:8f724a47a820 277 i2c.start();
jont 0:8f724a47a820 278 i2c.write(Slave);
jont 0:8f724a47a820 279 i2c.write(0x00);
jont 0:8f724a47a820 280 i2c.write(0x01);
jont 0:8f724a47a820 281 i2c.stop();
jont 0:8f724a47a820 282 wait(0.2);
jont 0:8f724a47a820 283 }
jont 2:5b220477045b 284 */
jont 0:8f724a47a820 285 void test()
jont 0:8f724a47a820 286 {
jont 0:8f724a47a820 287
jont 0:8f724a47a820 288 i2c.start();
jont 0:8f724a47a820 289 i2c.write(Slave);
jont 0:8f724a47a820 290 i2c.stop();
jont 0:8f724a47a820 291 wait(0.2);
jont 0:8f724a47a820 292 }
jont 0:8f724a47a820 293
jont 0:8f724a47a820 294
jont 2:5b220477045b 295 void LcdWriteText(char *text)
jont 0:8f724a47a820 296 {
jont 1:2ded47079af1 297 int n;//,d;
jont 1:2ded47079af1 298 //d=0x00;
jont 0:8f724a47a820 299 int length = strlen(text);
jont 0:8f724a47a820 300 i2c.start();
jont 0:8f724a47a820 301 i2c.write(Slave); //Slave=0x78
jont 2:5b220477045b 302 i2c.write(LCDDATA);//Datasend=0x40
jont 0:8f724a47a820 303 for(n=0;n<length;n++){
jont 0:8f724a47a820 304 i2c.write(*text);
jont 0:8f724a47a820 305 ++text;
jont 0:8f724a47a820 306 }
jont 0:8f724a47a820 307 i2c.stop();
jont 2:5b220477045b 308 }
jont 2:5b220477045b 309
jont 2:5b220477045b 310 /*==============================================================*/
jont 3:5744bf6006e1 311 /* Writes to LCD and fills rest of line with space */
jont 3:5744bf6006e1 312 /*==============================================================*/
jont 3:5744bf6006e1 313 void LcdWriteTextAndFill(char *text, int numb)
jont 3:5744bf6006e1 314 {
jont 3:5744bf6006e1 315
jont 3:5744bf6006e1 316 int length = strlen(text);
jont 3:5744bf6006e1 317 int diff = numb -length;
jont 3:5744bf6006e1 318 LcdWriteText(text);
jont 3:5744bf6006e1 319 if (diff >0)
jont 3:5744bf6006e1 320 {
jont 3:5744bf6006e1 321 for (int n=0; n <= diff; n++)
jont 3:5744bf6006e1 322 LcdWriteText(" ");
jont 3:5744bf6006e1 323
jont 3:5744bf6006e1 324 }
jont 3:5744bf6006e1 325 }
jont 3:5744bf6006e1 326
jont 3:5744bf6006e1 327 /*==============================================================*/
jont 4:ce867009531a 328 /* Writes to LCD and fills rest of line with space based on lcd maxlength */
jont 4:ce867009531a 329 /*==============================================================*/
jont 4:ce867009531a 330 void LcdWriteTextLine(char *text)
jont 4:ce867009531a 331 {
jont 4:ce867009531a 332
jont 4:ce867009531a 333 int length = strlen(text);
jont 4:ce867009531a 334 int diff = LCD_LINE_SIZE -length;
jont 4:ce867009531a 335 LcdWriteText(text);
jont 4:ce867009531a 336 if (diff >0)
jont 4:ce867009531a 337 {
jont 4:ce867009531a 338 for (int n=0; n <= diff; n++)
jont 4:ce867009531a 339 LcdWriteText(" ");
jont 4:ce867009531a 340
jont 4:ce867009531a 341 }
jont 4:ce867009531a 342 }
jont 4:ce867009531a 343 /*==============================================================*/
jont 2:5b220477045b 344 /* attemp at lcdlcdprintf */
jont 2:5b220477045b 345 /*==============================================================*/
jont 2:5b220477045b 346
jont 2:5b220477045b 347 static void lcdputchar(char c, void *ptr)
jont 2:5b220477045b 348 {
jont 2:5b220477045b 349 //LcdReady(); /* wait for lcd ready */
jont 2:5b220477045b 350 write_E_port(LCDDATA, c); /* send data to lcd */
jont 2:5b220477045b 351 }
jont 2:5b220477045b 352 /*
jont 2:5b220477045b 353 int lcdprintf(const char *format, ...)
jont 2:5b220477045b 354 {
jont 2:5b220477045b 355 va_list ap;
jont 2:5b220477045b 356 int nr_of_chars;
jont 2:5b220477045b 357 va_start (ap, format); //variable arg begin
jont 2:5b220477045b 358 nr_of_chars = _formatted_write (format, lcdputchar, (void *) 0, ap);
jont 2:5b220477045b 359 va_end(ap); //variable arg end
jont 2:5b220477045b 360 return(nr_of_chars);
jont 2:5b220477045b 361 } */
jont 2:5b220477045b 362