[136] | 1 | #include "LiquidCrystal.h"
|
---|
| 2 |
|
---|
| 3 | #include <stdio.h>
|
---|
| 4 | #include <string.h>
|
---|
| 5 | #include <inttypes.h>
|
---|
| 6 | #include "Arduino.h"
|
---|
| 7 |
|
---|
| 8 | // When the display powers up, it is configured as follows:
|
---|
| 9 | //
|
---|
| 10 | // 1. Display clear
|
---|
| 11 | // 2. Function set:
|
---|
| 12 | // DL = 1; 8-bit interface data
|
---|
| 13 | // N = 0; 1-line display
|
---|
| 14 | // F = 0; 5x8 dot character font
|
---|
| 15 | // 3. Display on/off control:
|
---|
| 16 | // D = 0; Display off
|
---|
| 17 | // C = 0; Cursor off
|
---|
| 18 | // B = 0; Blinking off
|
---|
| 19 | // 4. Entry mode set:
|
---|
| 20 | // I/D = 1; Increment by 1
|
---|
| 21 | // S = 0; No shift
|
---|
| 22 | //
|
---|
| 23 | // Note, however, that resetting the Arduino doesn't reset the LCD, so we
|
---|
| 24 | // can't assume that its in that state when a sketch starts (and the
|
---|
| 25 | // LiquidCrystal constructor is called).
|
---|
| 26 |
|
---|
| 27 | LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
|
---|
| 28 | uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
|
---|
| 29 | uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
|
---|
| 30 | {
|
---|
| 31 | init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
|
---|
| 35 | uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
|
---|
| 36 | uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
|
---|
| 37 | {
|
---|
| 38 | init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
|
---|
| 42 | uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
|
---|
| 43 | {
|
---|
| 44 | init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
|
---|
| 48 | uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
|
---|
| 49 | {
|
---|
| 50 | init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
|
---|
| 54 | uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
|
---|
| 55 | uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
|
---|
| 56 | {
|
---|
| 57 | _rs_pin = rs;
|
---|
| 58 | _rw_pin = rw;
|
---|
| 59 | _enable_pin = enable;
|
---|
| 60 |
|
---|
| 61 | _data_pins[0] = d0;
|
---|
| 62 | _data_pins[1] = d1;
|
---|
| 63 | _data_pins[2] = d2;
|
---|
| 64 | _data_pins[3] = d3;
|
---|
| 65 | _data_pins[4] = d4;
|
---|
| 66 | _data_pins[5] = d5;
|
---|
| 67 | _data_pins[6] = d6;
|
---|
| 68 | _data_pins[7] = d7;
|
---|
| 69 |
|
---|
| 70 | pinMode(_rs_pin, OUTPUT);
|
---|
| 71 | // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
|
---|
| 72 | if (_rw_pin != 255) {
|
---|
| 73 | pinMode(_rw_pin, OUTPUT);
|
---|
| 74 | }
|
---|
| 75 | pinMode(_enable_pin, OUTPUT);
|
---|
| 76 |
|
---|
| 77 | if (fourbitmode)
|
---|
| 78 | _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
|
---|
| 79 | else
|
---|
| 80 | _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
|
---|
| 81 |
|
---|
| 82 | begin(16, 1);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
|
---|
| 86 | if (lines > 1) {
|
---|
| 87 | _displayfunction |= LCD_2LINE;
|
---|
| 88 | }
|
---|
| 89 | _numlines = lines;
|
---|
| 90 |
|
---|
| 91 | setRowOffsets(0x00, 0x40, 0x00 + cols, 0x40 + cols);
|
---|
| 92 |
|
---|
| 93 | // for some 1 line displays you can select a 10 pixel high font
|
---|
| 94 | if ((dotsize != LCD_5x8DOTS) && (lines == 1)) {
|
---|
| 95 | _displayfunction |= LCD_5x10DOTS;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
|
---|
| 99 | // according to datasheet, we need at least 40ms after power rises above 2.7V
|
---|
| 100 | // before sending commands. Arduino can turn on way before 4.5V so we'll wait 50
|
---|
| 101 | delayMicroseconds(50000);
|
---|
| 102 | // Now we pull both RS and R/W low to begin commands
|
---|
| 103 | digitalWrite(_rs_pin, LOW);
|
---|
| 104 | digitalWrite(_enable_pin, LOW);
|
---|
| 105 | if (_rw_pin != 255) {
|
---|
| 106 | digitalWrite(_rw_pin, LOW);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | //put the LCD into 4 bit or 8 bit mode
|
---|
| 110 | if (! (_displayfunction & LCD_8BITMODE)) {
|
---|
| 111 | // this is according to the hitachi HD44780 datasheet
|
---|
| 112 | // figure 24, pg 46
|
---|
| 113 |
|
---|
| 114 | // we start in 8bit mode, try to set 4 bit mode
|
---|
| 115 | write4bits(0x03);
|
---|
| 116 | delayMicroseconds(4500); // wait min 4.1ms
|
---|
| 117 |
|
---|
| 118 | // second try
|
---|
| 119 | write4bits(0x03);
|
---|
| 120 | delayMicroseconds(4500); // wait min 4.1ms
|
---|
| 121 |
|
---|
| 122 | // third go!
|
---|
| 123 | write4bits(0x03);
|
---|
| 124 | delayMicroseconds(150);
|
---|
| 125 |
|
---|
| 126 | // finally, set to 4-bit interface
|
---|
| 127 | write4bits(0x02);
|
---|
| 128 | } else {
|
---|
| 129 | // this is according to the hitachi HD44780 datasheet
|
---|
| 130 | // page 45 figure 23
|
---|
| 131 |
|
---|
| 132 | // Send function set command sequence
|
---|
| 133 | command(LCD_FUNCTIONSET | _displayfunction);
|
---|
| 134 | delayMicroseconds(4500); // wait more than 4.1ms
|
---|
| 135 |
|
---|
| 136 | // second try
|
---|
| 137 | command(LCD_FUNCTIONSET | _displayfunction);
|
---|
| 138 | delayMicroseconds(150);
|
---|
| 139 |
|
---|
| 140 | // third go
|
---|
| 141 | command(LCD_FUNCTIONSET | _displayfunction);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | // finally, set # lines, font size, etc.
|
---|
| 145 | command(LCD_FUNCTIONSET | _displayfunction);
|
---|
| 146 |
|
---|
| 147 | // turn the display on with no cursor or blinking default
|
---|
| 148 | _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
|
---|
| 149 | display();
|
---|
| 150 |
|
---|
| 151 | // clear it off
|
---|
| 152 | clear();
|
---|
| 153 |
|
---|
| 154 | // Initialize to default text direction (for romance languages)
|
---|
| 155 | _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
|
---|
| 156 | // set the entry mode
|
---|
| 157 | command(LCD_ENTRYMODESET | _displaymode);
|
---|
| 158 |
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | void LiquidCrystal::setRowOffsets(int row0, int row1, int row2, int row3)
|
---|
| 162 | {
|
---|
| 163 | _row_offsets[0] = row0;
|
---|
| 164 | _row_offsets[1] = row1;
|
---|
| 165 | _row_offsets[2] = row2;
|
---|
| 166 | _row_offsets[3] = row3;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /********** high level commands, for the user! */
|
---|
| 170 | void LiquidCrystal::clear()
|
---|
| 171 | {
|
---|
| 172 | command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
|
---|
| 173 | delayMicroseconds(2000); // this command takes a long time!
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | void LiquidCrystal::home()
|
---|
| 177 | {
|
---|
| 178 | command(LCD_RETURNHOME); // set cursor position to zero
|
---|
| 179 | delayMicroseconds(2000); // this command takes a long time!
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | void LiquidCrystal::setCursor(uint8_t col, uint8_t row)
|
---|
| 183 | {
|
---|
| 184 | const size_t max_lines = sizeof(_row_offsets) / sizeof(*_row_offsets);
|
---|
| 185 | if ( row >= max_lines ) {
|
---|
| 186 | row = max_lines - 1; // we count rows starting w/0
|
---|
| 187 | }
|
---|
| 188 | if ( row >= _numlines ) {
|
---|
| 189 | row = _numlines - 1; // we count rows starting w/0
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | command(LCD_SETDDRAMADDR | (col + _row_offsets[row]));
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | // Turn the display on/off (quickly)
|
---|
| 196 | void LiquidCrystal::noDisplay() {
|
---|
| 197 | _displaycontrol &= ~LCD_DISPLAYON;
|
---|
| 198 | command(LCD_DISPLAYCONTROL | _displaycontrol);
|
---|
| 199 | }
|
---|
| 200 | void LiquidCrystal::display() {
|
---|
| 201 | _displaycontrol |= LCD_DISPLAYON;
|
---|
| 202 | command(LCD_DISPLAYCONTROL | _displaycontrol);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | // Turns the underline cursor on/off
|
---|
| 206 | void LiquidCrystal::noCursor() {
|
---|
| 207 | _displaycontrol &= ~LCD_CURSORON;
|
---|
| 208 | command(LCD_DISPLAYCONTROL | _displaycontrol);
|
---|
| 209 | }
|
---|
| 210 | void LiquidCrystal::cursor() {
|
---|
| 211 | _displaycontrol |= LCD_CURSORON;
|
---|
| 212 | command(LCD_DISPLAYCONTROL | _displaycontrol);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | // Turn on and off the blinking cursor
|
---|
| 216 | void LiquidCrystal::noBlink() {
|
---|
| 217 | _displaycontrol &= ~LCD_BLINKON;
|
---|
| 218 | command(LCD_DISPLAYCONTROL | _displaycontrol);
|
---|
| 219 | }
|
---|
| 220 | void LiquidCrystal::blink() {
|
---|
| 221 | _displaycontrol |= LCD_BLINKON;
|
---|
| 222 | command(LCD_DISPLAYCONTROL | _displaycontrol);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | // These commands scroll the display without changing the RAM
|
---|
| 226 | void LiquidCrystal::scrollDisplayLeft(void) {
|
---|
| 227 | command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
|
---|
| 228 | }
|
---|
| 229 | void LiquidCrystal::scrollDisplayRight(void) {
|
---|
| 230 | command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | // This is for text that flows Left to Right
|
---|
| 234 | void LiquidCrystal::leftToRight(void) {
|
---|
| 235 | _displaymode |= LCD_ENTRYLEFT;
|
---|
| 236 | command(LCD_ENTRYMODESET | _displaymode);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | // This is for text that flows Right to Left
|
---|
| 240 | void LiquidCrystal::rightToLeft(void) {
|
---|
| 241 | _displaymode &= ~LCD_ENTRYLEFT;
|
---|
| 242 | command(LCD_ENTRYMODESET | _displaymode);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | // This will 'right justify' text from the cursor
|
---|
| 246 | void LiquidCrystal::autoscroll(void) {
|
---|
| 247 | _displaymode |= LCD_ENTRYSHIFTINCREMENT;
|
---|
| 248 | command(LCD_ENTRYMODESET | _displaymode);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | // This will 'left justify' text from the cursor
|
---|
| 252 | void LiquidCrystal::noAutoscroll(void) {
|
---|
| 253 | _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
|
---|
| 254 | command(LCD_ENTRYMODESET | _displaymode);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | // Allows us to fill the first 8 CGRAM locations
|
---|
| 258 | // with custom characters
|
---|
| 259 | void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) {
|
---|
| 260 | location &= 0x7; // we only have 8 locations 0-7
|
---|
| 261 | command(LCD_SETCGRAMADDR | (location << 3));
|
---|
| 262 | for (int i=0; i<8; i++) {
|
---|
| 263 | write(charmap[i]);
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | /*********** mid level commands, for sending data/cmds */
|
---|
| 268 |
|
---|
| 269 | inline void LiquidCrystal::command(uint8_t value) {
|
---|
| 270 | send(value, LOW);
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | inline size_t LiquidCrystal::write(uint8_t value) {
|
---|
| 274 | send(value, HIGH);
|
---|
| 275 | return 1; // assume sucess
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | /************ low level data pushing commands **********/
|
---|
| 279 |
|
---|
| 280 | // write either command or data, with automatic 4/8-bit selection
|
---|
| 281 | void LiquidCrystal::send(uint8_t value, uint8_t mode) {
|
---|
| 282 | digitalWrite(_rs_pin, mode);
|
---|
| 283 |
|
---|
| 284 | // if there is a RW pin indicated, set it low to Write
|
---|
| 285 | if (_rw_pin != 255) {
|
---|
| 286 | digitalWrite(_rw_pin, LOW);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | if (_displayfunction & LCD_8BITMODE) {
|
---|
| 290 | write8bits(value);
|
---|
| 291 | } else {
|
---|
| 292 | write4bits(value>>4);
|
---|
| 293 | write4bits(value);
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | void LiquidCrystal::pulseEnable(void) {
|
---|
| 298 | digitalWrite(_enable_pin, LOW);
|
---|
| 299 | delayMicroseconds(1);
|
---|
| 300 | digitalWrite(_enable_pin, HIGH);
|
---|
| 301 | delayMicroseconds(1); // enable pulse must be >450ns
|
---|
| 302 | digitalWrite(_enable_pin, LOW);
|
---|
| 303 | delayMicroseconds(100); // commands need > 37us to settle
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | void LiquidCrystal::write4bits(uint8_t value) {
|
---|
| 307 | for (int i = 0; i < 4; i++) {
|
---|
| 308 | pinMode(_data_pins[i], OUTPUT);
|
---|
| 309 | digitalWrite(_data_pins[i], (value >> i) & 0x01);
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | pulseEnable();
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | void LiquidCrystal::write8bits(uint8_t value) {
|
---|
| 316 | for (int i = 0; i < 8; i++) {
|
---|
| 317 | pinMode(_data_pins[i], OUTPUT);
|
---|
| 318 | digitalWrite(_data_pins[i], (value >> i) & 0x01);
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | pulseEnable();
|
---|
| 322 | }
|
---|