Demo of LCD modified to use interrupts not delays.

Dependencies:   mbed

Fork of LCD_nonblocking_demo by Oliver Broad

This is an experiment in controlling an LCD from callbacks independently of the main program loop. As it runs from callbacks it is necessary to check if the library is "busy", which is currently indicated by an integer called "busy".

Busy checking should be converted into a method so that the criteria for being "busy" can be changed without changing the API.

As this is rough I have not issued it as a library ... yet.

Also due to my inexperience using the repository manager this is currently listed as a fork of itself. Sorry.

Committer:
oliverb
Date:
Wed Nov 20 11:07:09 2013 +0000
Revision:
2:d87266912897
Parent:
1:1106d7adbe79
Child:
3:f18aa98bc9a8
Updated to support newline and "locate" command.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oliverb 0:e1231949753e 1 // Hello World! for the TextLCD
oliverb 2:d87266912897 2 /*
oliverb 2:d87266912897 3 * Heavily modified LCD "Hello world"
oliverb 2:d87266912897 4 *
oliverb 2:d87266912897 5 * LCD Library has been merged into program for convenience of development, and
oliverb 2:d87266912897 6 * can be de-merged once its looking reliable
oliverb 2:d87266912897 7 * currently this gives good results with a small LCD and with a LCD emulation VFD
oliverb 2:d87266912897 8 * but is not reliable with a GU140x32-7806A graphic VFD
oliverb 2:d87266912897 9 *
oliverb 2:d87266912897 10 * Note E is set to 1 at end of cycle in an attempt to force a bug as display works fine
oliverb 2:d87266912897 11 * on first power-up but tends to corrupt after MBED manual reset. I am speculating that
oliverb 2:d87266912897 12 * something appears on MBED pins that the display "objects" to during a manual reset
oliverb 2:d87266912897 13 *
oliverb 2:d87266912897 14 */
oliverb 0:e1231949753e 15
oliverb 0:e1231949753e 16 #include "mbed.h"
oliverb 0:e1231949753e 17 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
oliverb 2:d87266912897 18 * Copyright (c) 2013, oliverb, http://mbed.org
oliverb 0:e1231949753e 19 * Copyright (c) 2007-2010, sford, http://mbed.org
oliverb 0:e1231949753e 20 *
oliverb 0:e1231949753e 21 * Permission is hereby granted, free of charge, to any person obtaining a copy
oliverb 0:e1231949753e 22 * of this software and associated documentation files (the "Software"), to deal
oliverb 0:e1231949753e 23 * in the Software without restriction, including without limitation the rights
oliverb 0:e1231949753e 24 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
oliverb 0:e1231949753e 25 * copies of the Software, and to permit persons to whom the Software is
oliverb 0:e1231949753e 26 * furnished to do so, subject to the following conditions:
oliverb 0:e1231949753e 27 *
oliverb 0:e1231949753e 28 * The above copyright notice and this permission notice shall be included in
oliverb 0:e1231949753e 29 * all copies or substantial portions of the Software.
oliverb 0:e1231949753e 30 *
oliverb 0:e1231949753e 31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
oliverb 0:e1231949753e 32 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
oliverb 0:e1231949753e 33 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
oliverb 0:e1231949753e 34 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
oliverb 0:e1231949753e 35 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
oliverb 0:e1231949753e 36 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
oliverb 0:e1231949753e 37 * THE SOFTWARE.
oliverb 0:e1231949753e 38 */
oliverb 0:e1231949753e 39
oliverb 0:e1231949753e 40 #ifndef MBED_TEXTLCD_H
oliverb 0:e1231949753e 41 #define MBED_TEXTLCD_H
oliverb 0:e1231949753e 42
oliverb 0:e1231949753e 43 //#include "mbed.h"
oliverb 0:e1231949753e 44
oliverb 0:e1231949753e 45 /** A TextLCD interface for driving 4-bit HD44780-based LCDs
oliverb 0:e1231949753e 46 *
oliverb 0:e1231949753e 47 * Currently supports 16x2, 20x2 and 20x4 panels
oliverb 0:e1231949753e 48 *
oliverb 2:d87266912897 49 * Implements a "Busy" flag to indicate when display is unavailable
oliverb 2:d87266912897 50 * Busy is currently set by a timer and not by polling actual module
oliverb 2:d87266912897 51 *
oliverb 2:d87266912897 52 *
oliverb 0:e1231949753e 53 * @code
oliverb 0:e1231949753e 54 * #include "mbed.h"
oliverb 0:e1231949753e 55 * #include "TextLCD.h"
oliverb 2:d87266912897 56 *
oliverb 0:e1231949753e 57 * TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d4-d7
oliverb 2:d87266912897 58 *
oliverb 0:e1231949753e 59 * int main() {
oliverb 0:e1231949753e 60 * lcd.printf("Hello World!\n");
oliverb 0:e1231949753e 61 * }
oliverb 0:e1231949753e 62 * @endcode
oliverb 0:e1231949753e 63 */
oliverb 2:d87266912897 64 class TextLCD : public Stream
oliverb 2:d87266912897 65 {
oliverb 0:e1231949753e 66 public:
oliverb 0:e1231949753e 67
oliverb 0:e1231949753e 68 /** LCD panel format */
oliverb 0:e1231949753e 69 enum LCDType {
oliverb 0:e1231949753e 70 LCD16x2 /**< 16x2 LCD panel (default) */
oliverb 0:e1231949753e 71 , LCD16x2B /**< 16x2 LCD panel alternate addressing */
oliverb 0:e1231949753e 72 , LCD20x2 /**< 20x2 LCD panel */
oliverb 0:e1231949753e 73 , LCD20x4 /**< 20x4 LCD panel */
oliverb 0:e1231949753e 74 };
oliverb 0:e1231949753e 75
oliverb 0:e1231949753e 76 /** Create a TextLCD interface
oliverb 0:e1231949753e 77 *
oliverb 0:e1231949753e 78 * @param rs Instruction/data control line
oliverb 0:e1231949753e 79 * @param e Enable line (clock)
oliverb 0:e1231949753e 80 * @param d4-d7 Data lines for using as a 4-bit interface
oliverb 0:e1231949753e 81 * @param type Sets the panel size/addressing mode (default = LCD16x2)
oliverb 0:e1231949753e 82 */
oliverb 0:e1231949753e 83 TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2);
oliverb 2:d87266912897 84
oliverb 0:e1231949753e 85 void init();
oliverb 2:d87266912897 86
oliverb 0:e1231949753e 87
oliverb 0:e1231949753e 88 #if DOXYGEN_ONLY
oliverb 0:e1231949753e 89 /** Write a character to the LCD
oliverb 0:e1231949753e 90 *
oliverb 0:e1231949753e 91 * @param c The character to write to the display
oliverb 0:e1231949753e 92 */
oliverb 0:e1231949753e 93 int putc(int c);
oliverb 0:e1231949753e 94
oliverb 0:e1231949753e 95 /** Write a formated string to the LCD
oliverb 0:e1231949753e 96 *
oliverb 0:e1231949753e 97 * @param format A printf-style format string, followed by the
oliverb 0:e1231949753e 98 * variables to use in formating the string.
oliverb 0:e1231949753e 99 */
oliverb 2:d87266912897 100 volatile int printf(const char* format, ...);
oliverb 0:e1231949753e 101 #endif
oliverb 0:e1231949753e 102
oliverb 0:e1231949753e 103 /** Locate to a screen column and row
oliverb 0:e1231949753e 104 *
oliverb 0:e1231949753e 105 * @param column The horizontal position from the left, indexed from 0
oliverb 0:e1231949753e 106 * @param row The vertical position from the top, indexed from 0
oliverb 0:e1231949753e 107 */
oliverb 0:e1231949753e 108 void locate(int column, int row);
oliverb 0:e1231949753e 109
oliverb 0:e1231949753e 110 /** Clear the screen and locate to 0,0 */
oliverb 0:e1231949753e 111 void cls();
oliverb 0:e1231949753e 112
oliverb 0:e1231949753e 113 int rows();
oliverb 0:e1231949753e 114 int columns();
oliverb 2:d87266912897 115
oliverb 0:e1231949753e 116 volatile int busy;
oliverb 0:e1231949753e 117
oliverb 0:e1231949753e 118 protected:
oliverb 0:e1231949753e 119 Timeout timer;
oliverb 2:d87266912897 120
oliverb 2:d87266912897 121 // callbacks used by "init" to carry out steps of initialisation
oliverb 2:d87266912897 122 void init2();
oliverb 2:d87266912897 123 void init2b();
oliverb 2:d87266912897 124
oliverb 2:d87266912897 125 void init3();
oliverb 2:d87266912897 126 void init4();
oliverb 2:d87266912897 127 void init5();
oliverb 2:d87266912897 128 void init6();
oliverb 2:d87266912897 129 void init7();
oliverb 2:d87266912897 130 void init8();
oliverb 2:d87266912897 131 // void init9();
oliverb 0:e1231949753e 132 // Stream implementation functions
oliverb 0:e1231949753e 133 virtual int _putc(int value);
oliverb 0:e1231949753e 134 virtual int _getc();
oliverb 0:e1231949753e 135
oliverb 0:e1231949753e 136 int address(int column, int row);
oliverb 2:d87266912897 137 // void character(int column, int row, int c);
oliverb 0:e1231949753e 138 void writeByte(int value);
oliverb 0:e1231949753e 139 void writeCommand(int command);
oliverb 0:e1231949753e 140 void writeData(int data);
oliverb 0:e1231949753e 141 void callback();
oliverb 2:d87266912897 142 void locate_cb();
oliverb 0:e1231949753e 143
oliverb 0:e1231949753e 144 DigitalOut _rs, _e;
oliverb 0:e1231949753e 145 BusOut _d;
oliverb 0:e1231949753e 146 LCDType _type;
oliverb 0:e1231949753e 147
oliverb 0:e1231949753e 148 int _column;
oliverb 0:e1231949753e 149 int _row;
oliverb 0:e1231949753e 150 char _queue[40];
oliverb 0:e1231949753e 151 volatile int _head;
oliverb 0:e1231949753e 152 volatile int _tail;
oliverb 2:d87266912897 153 // int _loop;
oliverb 0:e1231949753e 154 };
oliverb 0:e1231949753e 155
oliverb 0:e1231949753e 156 #endif
oliverb 0:e1231949753e 157 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
oliverb 0:e1231949753e 158 * Copyright (c) 2007-2010, sford, http://mbed.org
oliverb 0:e1231949753e 159 *
oliverb 0:e1231949753e 160 * Permission is hereby granted, free of charge, to any person obtaining a copy
oliverb 0:e1231949753e 161 * of this software and associated documentation files (the "Software"), to deal
oliverb 0:e1231949753e 162 * in the Software without restriction, including without limitation the rights
oliverb 0:e1231949753e 163 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
oliverb 0:e1231949753e 164 * copies of the Software, and to permit persons to whom the Software is
oliverb 0:e1231949753e 165 * furnished to do so, subject to the following conditions:
oliverb 0:e1231949753e 166 *
oliverb 0:e1231949753e 167 * The above copyright notice and this permission notice shall be included in
oliverb 0:e1231949753e 168 * all copies or substantial portions of the Software.
oliverb 0:e1231949753e 169 *
oliverb 0:e1231949753e 170 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
oliverb 0:e1231949753e 171 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
oliverb 0:e1231949753e 172 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
oliverb 0:e1231949753e 173 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
oliverb 0:e1231949753e 174 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
oliverb 0:e1231949753e 175 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
oliverb 0:e1231949753e 176 * THE SOFTWARE.
oliverb 0:e1231949753e 177 */
oliverb 0:e1231949753e 178
oliverb 0:e1231949753e 179 //#include "TextLCD.h"
oliverb 0:e1231949753e 180 //#include "mbed.h"
oliverb 0:e1231949753e 181
oliverb 0:e1231949753e 182
oliverb 0:e1231949753e 183 DigitalOut Led1(LED1);
oliverb 0:e1231949753e 184 DigitalOut Led2(LED2);
oliverb 0:e1231949753e 185 DigitalOut Led3(LED3);
oliverb 0:e1231949753e 186 DigitalOut Led4(LED4);
oliverb 0:e1231949753e 187
oliverb 0:e1231949753e 188
oliverb 0:e1231949753e 189
oliverb 0:e1231949753e 190
oliverb 0:e1231949753e 191
oliverb 0:e1231949753e 192 TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5,
oliverb 0:e1231949753e 193 PinName d6, PinName d7, LCDType type) : busy(0),_rs(rs),
oliverb 2:d87266912897 194 _e(e), _d(d4, d5, d6, d7),
oliverb 2:d87266912897 195 _type(type) , _column(0),_row(0),
oliverb 2:d87266912897 196 _head(0), _tail(0) {}
oliverb 0:e1231949753e 197 void TextLCD::init()
oliverb 2:d87266912897 198 {
oliverb 2:d87266912897 199
oliverb 0:e1231949753e 200 busy=1;
oliverb 2:d87266912897 201 _rs = 1;
oliverb 2:d87266912897 202 _d = 0;
oliverb 2:d87266912897 203 wait_us(1);
oliverb 0:e1231949753e 204 _e = 0;
oliverb 0:e1231949753e 205 _rs = 0; // command mode
oliverb 2:d87266912897 206 timer.attach_us(this,&TextLCD::init2,15000);
oliverb 0:e1231949753e 207 }
oliverb 0:e1231949753e 208 void TextLCD::init2()
oliverb 2:d87266912897 209 {
oliverb 2:d87266912897 210 _rs = 0;
oliverb 2:d87266912897 211 _d = 0x3;
oliverb 2:d87266912897 212 _e = 1;
oliverb 2:d87266912897 213 wait_us(1);
oliverb 2:d87266912897 214 _e = 0;
oliverb 2:d87266912897 215 timer.attach_us(this,&TextLCD::init2b,4100);
oliverb 2:d87266912897 216 }
oliverb 2:d87266912897 217 void TextLCD::init2b()
oliverb 2:d87266912897 218 {
oliverb 0:e1231949753e 219
oliverb 0:e1231949753e 220 _d = 0x3;
oliverb 0:e1231949753e 221 _e = 1;
oliverb 0:e1231949753e 222 wait_us(1);
oliverb 0:e1231949753e 223 _e = 0;
oliverb 2:d87266912897 224 timer.attach_us(this,&TextLCD::init3,4100);
oliverb 0:e1231949753e 225 }
oliverb 0:e1231949753e 226 void TextLCD::init3()
oliverb 0:e1231949753e 227 {
oliverb 0:e1231949753e 228 _d = 0x3;
oliverb 0:e1231949753e 229 _e = 1;
oliverb 0:e1231949753e 230 wait_us(1);
oliverb 0:e1231949753e 231 _e = 0;
oliverb 2:d87266912897 232 timer.attach_us(this,&TextLCD::init4,1000);
oliverb 0:e1231949753e 233 }
oliverb 0:e1231949753e 234 void TextLCD::init4()
oliverb 0:e1231949753e 235 {
oliverb 0:e1231949753e 236 _d = 0x2;
oliverb 0:e1231949753e 237 _e = 1;
oliverb 0:e1231949753e 238 wait_us(1);
oliverb 0:e1231949753e 239 _e = 0;
oliverb 2:d87266912897 240 timer.attach_us(this,&TextLCD::init5,60);
oliverb 0:e1231949753e 241 }
oliverb 0:e1231949753e 242 void TextLCD::init5()
oliverb 0:e1231949753e 243 {
oliverb 0:e1231949753e 244
oliverb 2:d87266912897 245 writeByte(0x28); // Function set 001 BW N F - -
oliverb 2:d87266912897 246 timer.attach_us(this,&TextLCD::init6,60);
oliverb 0:e1231949753e 247 }
oliverb 0:e1231949753e 248 void TextLCD::init6()
oliverb 0:e1231949753e 249 {
oliverb 2:d87266912897 250 writeByte(0x0C);
oliverb 2:d87266912897 251 timer.attach_us(this,&TextLCD::init7,60);
oliverb 0:e1231949753e 252 }
oliverb 0:e1231949753e 253 void TextLCD::init7()
oliverb 0:e1231949753e 254 {
oliverb 2:d87266912897 255 writeByte(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
oliverb 2:d87266912897 256 timer.attach_us(this,&TextLCD::init8,60);
oliverb 0:e1231949753e 257 }
oliverb 0:e1231949753e 258 void TextLCD::init8()
oliverb 0:e1231949753e 259 {
oliverb 2:d87266912897 260 writeByte(0x01); // cls, and set cursor to 0
oliverb 2:d87266912897 261 // This command takes 1.64 ms
oliverb 2:d87266912897 262 locate(0, 0);
oliverb 2:d87266912897 263 timer.attach_us(this,&TextLCD::locate_cb,1640);
oliverb 0:e1231949753e 264 }
oliverb 2:d87266912897 265 /*
oliverb 2:d87266912897 266 void TextLCD::character(int column, int row, int c)
oliverb 2:d87266912897 267 {
oliverb 0:e1231949753e 268 int a = address(column, row);
oliverb 0:e1231949753e 269 writeCommand(a);
oliverb 0:e1231949753e 270 writeData(c);
oliverb 0:e1231949753e 271 }
oliverb 2:d87266912897 272 */
oliverb 2:d87266912897 273 void TextLCD::cls()
oliverb 2:d87266912897 274 {
oliverb 0:e1231949753e 275 writeCommand(0x01); // cls, and set cursor to 0
oliverb 0:e1231949753e 276 wait(0.00164f); // This command takes 1.64 ms
oliverb 0:e1231949753e 277 locate(0, 0);
oliverb 0:e1231949753e 278 }
oliverb 0:e1231949753e 279
oliverb 2:d87266912897 280 void TextLCD::locate(int column, int row)
oliverb 2:d87266912897 281 {
oliverb 0:e1231949753e 282 _column = column;
oliverb 0:e1231949753e 283 _row = row;
oliverb 0:e1231949753e 284 }
oliverb 2:d87266912897 285 /* this is the general buffered-write callback for writing text to the display
oliverb 2:d87266912897 286 * it normally chains to itself unless a newline occurs in which case it chains to
oliverb 2:d87266912897 287 * locate_cb
oliverb 2:d87266912897 288 * or the buffer runs out in which case it clears busy and does not chain
oliverb 2:d87266912897 289 */
oliverb 2:d87266912897 290
oliverb 0:e1231949753e 291 void TextLCD::callback()
oliverb 0:e1231949753e 292 {
oliverb 2:d87266912897 293 if (_tail<_head) {
oliverb 2:d87266912897 294 int value=_queue[_tail++];
oliverb 2:d87266912897 295 if (value == '\n') {
oliverb 2:d87266912897 296 _column = 0;
oliverb 2:d87266912897 297 _row++;
oliverb 2:d87266912897 298 if (_row >= rows()) {
oliverb 2:d87266912897 299 _row = 0;
oliverb 2:d87266912897 300 }
oliverb 2:d87266912897 301 locate_cb(); // newline isn't printed so go straight to "locate" code
oliverb 2:d87266912897 302 } else {
oliverb 2:d87266912897 303 _rs = 1; // data mode
oliverb 2:d87266912897 304 writeByte(value);
oliverb 2:d87266912897 305 _column++;
oliverb 2:d87266912897 306 if (_column >= columns()) { //if we've reached the end of line then chain to "locate"
oliverb 2:d87266912897 307 //otherwise go back to callback
oliverb 2:d87266912897 308 _column = 0;
oliverb 2:d87266912897 309 _row++;
oliverb 2:d87266912897 310 if (_row >= rows()) {
oliverb 2:d87266912897 311 _row = 0;
oliverb 2:d87266912897 312 }
oliverb 2:d87266912897 313 timer.attach_us(this,&TextLCD::locate_cb,60);
oliverb 2:d87266912897 314 } else {
oliverb 2:d87266912897 315 timer.attach_us(this,&TextLCD::callback,60);
oliverb 2:d87266912897 316 }
oliverb 2:d87266912897 317
oliverb 2:d87266912897 318 }
oliverb 2:d87266912897 319
oliverb 2:d87266912897 320 } else { //if we've run out of text then clear flag
oliverb 2:d87266912897 321 _head=_tail=0;
oliverb 2:d87266912897 322 busy=0;
oliverb 2:d87266912897 323 }
oliverb 0:e1231949753e 324 }
oliverb 0:e1231949753e 325
oliverb 2:d87266912897 326 void TextLCD::locate_cb()
oliverb 2:d87266912897 327 {
oliverb 2:d87266912897 328 int a = address(_column, _row);
oliverb 2:d87266912897 329 _rs=0;
oliverb 2:d87266912897 330 writeByte(a);
oliverb 2:d87266912897 331 timer.attach_us(this,&TextLCD::callback,60);
oliverb 2:d87266912897 332 }
oliverb 0:e1231949753e 333
oliverb 2:d87266912897 334
oliverb 2:d87266912897 335 int TextLCD::_putc(int value)
oliverb 0:e1231949753e 336 {
oliverb 2:d87266912897 337 if (_head<40) {
oliverb 2:d87266912897 338 _queue[_head++]=value;
oliverb 2:d87266912897 339 };
oliverb 2:d87266912897 340 if (busy==0) {
oliverb 2:d87266912897 341 busy=1;
oliverb 2:d87266912897 342 locate_cb(); // set cursor position before text
oliverb 2:d87266912897 343 /*
oliverb 2:d87266912897 344 int a = address(_column, _row);
oliverb 2:d87266912897 345 _rs=0;
oliverb 2:d87266912897 346 writeByte(a);
oliverb 2:d87266912897 347 timer.attach_us(this,&TextLCD::callback,60);
oliverb 2:d87266912897 348 */
oliverb 2:d87266912897 349 };
oliverb 2:d87266912897 350 return value;
oliverb 0:e1231949753e 351 }
oliverb 0:e1231949753e 352
oliverb 0:e1231949753e 353 /*{
oliverb 0:e1231949753e 354 if (value == '\n') {
oliverb 0:e1231949753e 355 _column = 0;
oliverb 0:e1231949753e 356 _row++;
oliverb 0:e1231949753e 357 if (_row >= rows()) {
oliverb 0:e1231949753e 358 _row = 0;
oliverb 0:e1231949753e 359 }
oliverb 0:e1231949753e 360 } else {
oliverb 0:e1231949753e 361 character(_column, _row, value);
oliverb 0:e1231949753e 362 _column++;
oliverb 0:e1231949753e 363 if (_column >= columns()) {
oliverb 0:e1231949753e 364 _column = 0;
oliverb 0:e1231949753e 365 _row++;
oliverb 0:e1231949753e 366 if (_row >= rows()) {
oliverb 0:e1231949753e 367 _row = 0;
oliverb 0:e1231949753e 368 }
oliverb 0:e1231949753e 369 }
oliverb 0:e1231949753e 370 }
oliverb 0:e1231949753e 371 return value;
oliverb 0:e1231949753e 372 }
oliverb 0:e1231949753e 373 */
oliverb 2:d87266912897 374 int TextLCD::_getc()
oliverb 2:d87266912897 375 {
oliverb 0:e1231949753e 376 return -1;
oliverb 0:e1231949753e 377 }
oliverb 0:e1231949753e 378
oliverb 2:d87266912897 379 void TextLCD::writeByte(int value)
oliverb 2:d87266912897 380 {
oliverb 0:e1231949753e 381 _d = value >> 4;
oliverb 0:e1231949753e 382 _e = 1;
oliverb 0:e1231949753e 383 wait_us(1);
oliverb 0:e1231949753e 384 _e = 0;
oliverb 0:e1231949753e 385 _d = value >> 0;
oliverb 2:d87266912897 386 wait_us(2);
oliverb 0:e1231949753e 387 _e = 1;
oliverb 0:e1231949753e 388 wait_us(1); // most instructions take 40us
oliverb 0:e1231949753e 389 _e = 0;
oliverb 0:e1231949753e 390 }
oliverb 0:e1231949753e 391
oliverb 2:d87266912897 392 void TextLCD::writeCommand(int command)
oliverb 2:d87266912897 393 {
oliverb 0:e1231949753e 394 _rs = 0; //command mode
oliverb 0:e1231949753e 395 wait_us(50); // most instructions take 40us
oliverb 0:e1231949753e 396 writeByte(command);
oliverb 0:e1231949753e 397 }
oliverb 0:e1231949753e 398
oliverb 2:d87266912897 399 void TextLCD::writeData(int data)
oliverb 2:d87266912897 400 {
oliverb 0:e1231949753e 401 _rs = 1; // data mode
oliverb 0:e1231949753e 402 wait_us(50); // most instructions take 40us
oliverb 0:e1231949753e 403 writeByte(data);
oliverb 0:e1231949753e 404 }
oliverb 0:e1231949753e 405
oliverb 2:d87266912897 406 int TextLCD::address(int column, int row)
oliverb 2:d87266912897 407 {
oliverb 0:e1231949753e 408 switch (_type) {
oliverb 0:e1231949753e 409 case LCD20x4:
oliverb 0:e1231949753e 410 switch (row) {
oliverb 0:e1231949753e 411 case 0:
oliverb 0:e1231949753e 412 return 0x80 + column;
oliverb 0:e1231949753e 413 case 1:
oliverb 0:e1231949753e 414 return 0xc0 + column;
oliverb 0:e1231949753e 415 case 2:
oliverb 0:e1231949753e 416 return 0x94 + column;
oliverb 0:e1231949753e 417 case 3:
oliverb 0:e1231949753e 418 return 0xd4 + column;
oliverb 0:e1231949753e 419 }
oliverb 0:e1231949753e 420 case LCD16x2B:
oliverb 0:e1231949753e 421 return 0x80 + (row * 40) + column;
oliverb 0:e1231949753e 422 case LCD16x2:
oliverb 0:e1231949753e 423 case LCD20x2:
oliverb 0:e1231949753e 424 default:
oliverb 0:e1231949753e 425 return 0x80 + (row * 0x40) + column;
oliverb 0:e1231949753e 426 }
oliverb 0:e1231949753e 427 }
oliverb 0:e1231949753e 428
oliverb 2:d87266912897 429 int TextLCD::columns()
oliverb 2:d87266912897 430 {
oliverb 0:e1231949753e 431 switch (_type) {
oliverb 0:e1231949753e 432 case LCD20x4:
oliverb 0:e1231949753e 433 case LCD20x2:
oliverb 0:e1231949753e 434 return 20;
oliverb 0:e1231949753e 435 case LCD16x2:
oliverb 0:e1231949753e 436 case LCD16x2B:
oliverb 0:e1231949753e 437 default:
oliverb 0:e1231949753e 438 return 16;
oliverb 0:e1231949753e 439 }
oliverb 0:e1231949753e 440 }
oliverb 0:e1231949753e 441
oliverb 2:d87266912897 442 int TextLCD::rows()
oliverb 2:d87266912897 443 {
oliverb 0:e1231949753e 444 switch (_type) {
oliverb 0:e1231949753e 445 case LCD20x4:
oliverb 0:e1231949753e 446 return 4;
oliverb 0:e1231949753e 447 case LCD16x2:
oliverb 0:e1231949753e 448 case LCD16x2B:
oliverb 0:e1231949753e 449 case LCD20x2:
oliverb 0:e1231949753e 450 default:
oliverb 0:e1231949753e 451 return 2;
oliverb 0:e1231949753e 452 }
oliverb 0:e1231949753e 453 }
oliverb 0:e1231949753e 454
oliverb 2:d87266912897 455 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7
oliverb 0:e1231949753e 456
oliverb 0:e1231949753e 457
oliverb 2:d87266912897 458 int main()
oliverb 2:d87266912897 459 {
oliverb 2:d87266912897 460 while(1) {
oliverb 2:d87266912897 461 Led1=1;
oliverb 2:d87266912897 462 lcd.init();
oliverb 2:d87266912897 463 Led2=1;
oliverb 2:d87266912897 464 Led1=0;
oliverb 2:d87266912897 465 while (lcd.busy) {}
oliverb 2:d87266912897 466 Led3=1;
oliverb 2:d87266912897 467 Led2=0;
oliverb 2:d87266912897 468 lcd.printf("Test String that goes over end of line!\n");
oliverb 2:d87266912897 469 Led4=1;
oliverb 2:d87266912897 470 Led3=0;
oliverb 2:d87266912897 471 while (lcd.busy) {}
oliverb 2:d87266912897 472 Led1=1;
oliverb 2:d87266912897 473 wait(1);
oliverb 2:d87266912897 474 // DigitalOut E(p17);
oliverb 2:d87266912897 475 // E=1; // try to break it
oliverb 2:d87266912897 476 }
oliverb 0:e1231949753e 477 }