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:58:42 2015 +0000
Revision:
7:5b0b6167b507
Parent:
6:31bcd2c72da9
Removed old license comments

Who changed what in which revision?

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