00001 #include "LiquidCrystal.h"
00002
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <inttypes.h>
00006 #include "WProgram.h"
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
00028 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
00029 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
00030 {
00031 init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
00032 }
00033
00034 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
00035 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
00036 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
00037 {
00038 init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
00039 }
00040
00041 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
00042 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
00043 {
00044 init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
00045 }
00046
00047 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
00048 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
00049 {
00050 init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
00051 }
00052
00053 void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
00054 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
00055 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
00056 {
00057 _rs_pin = rs;
00058 _rw_pin = rw;
00059 _enable_pin = enable;
00060
00061 _data_pins[0] = d0;
00062 _data_pins[1] = d1;
00063 _data_pins[2] = d2;
00064 _data_pins[3] = d3;
00065 _data_pins[4] = d4;
00066 _data_pins[5] = d5;
00067 _data_pins[6] = d6;
00068 _data_pins[7] = d7;
00069
00070 pinMode(_rs_pin, OUTPUT);
00071
00072 if (_rw_pin != 255) {
00073 pinMode(_rw_pin, OUTPUT);
00074 }
00075 pinMode(_enable_pin, OUTPUT);
00076
00077 if (fourbitmode)
00078 _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
00079 else
00080 _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
00081
00082 begin(16, 1);
00083 }
00084
00085 void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
00086 if (lines > 1) {
00087 _displayfunction |= LCD_2LINE;
00088 }
00089 _numlines = lines;
00090 _currline = 0;
00091
00092
00093 if ((dotsize != 0) && (lines == 1)) {
00094 _displayfunction |= LCD_5x10DOTS;
00095 }
00096
00097
00098
00099
00100 delayMicroseconds(50000);
00101
00102 digitalWrite(_rs_pin, LOW);
00103 digitalWrite(_enable_pin, LOW);
00104 if (_rw_pin != 255) {
00105 digitalWrite(_rw_pin, LOW);
00106 }
00107
00108
00109 if (! (_displayfunction & LCD_8BITMODE)) {
00110
00111
00112
00113
00114 write4bits(0x03);
00115 delayMicroseconds(4500);
00116
00117
00118 write4bits(0x03);
00119 delayMicroseconds(4500);
00120
00121
00122 write4bits(0x03);
00123 delayMicroseconds(150);
00124
00125
00126 write4bits(0x02);
00127 } else {
00128
00129
00130
00131
00132 command(LCD_FUNCTIONSET | _displayfunction);
00133 delayMicroseconds(4500);
00134
00135
00136 command(LCD_FUNCTIONSET | _displayfunction);
00137 delayMicroseconds(150);
00138
00139
00140 command(LCD_FUNCTIONSET | _displayfunction);
00141 }
00142
00143
00144 command(LCD_FUNCTIONSET | _displayfunction);
00145
00146
00147 _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
00148 display();
00149
00150
00151 clear();
00152
00153
00154 _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
00155
00156 command(LCD_ENTRYMODESET | _displaymode);
00157
00158 }
00159
00160
00161 void LiquidCrystal::clear()
00162 {
00163 command(LCD_CLEARDISPLAY);
00164 delayMicroseconds(2000);
00165 }
00166
00167 void LiquidCrystal::home()
00168 {
00169 command(LCD_RETURNHOME);
00170 delayMicroseconds(2000);
00171 }
00172
00173 void LiquidCrystal::setCursor(uint8_t col, uint8_t row)
00174 {
00175 int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
00176 if ( row > _numlines ) {
00177 row = _numlines-1;
00178 }
00179
00180 command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
00181 }
00182
00183
00184 void LiquidCrystal::noDisplay() {
00185 _displaycontrol &= ~LCD_DISPLAYON;
00186 command(LCD_DISPLAYCONTROL | _displaycontrol);
00187 }
00188 void LiquidCrystal::display() {
00189 _displaycontrol |= LCD_DISPLAYON;
00190 command(LCD_DISPLAYCONTROL | _displaycontrol);
00191 }
00192
00193
00194 void LiquidCrystal::noCursor() {
00195 _displaycontrol &= ~LCD_CURSORON;
00196 command(LCD_DISPLAYCONTROL | _displaycontrol);
00197 }
00198 void LiquidCrystal::cursor() {
00199 _displaycontrol |= LCD_CURSORON;
00200 command(LCD_DISPLAYCONTROL | _displaycontrol);
00201 }
00202
00203
00204 void LiquidCrystal::noBlink() {
00205 _displaycontrol &= ~LCD_BLINKON;
00206 command(LCD_DISPLAYCONTROL | _displaycontrol);
00207 }
00208 void LiquidCrystal::blink() {
00209 _displaycontrol |= LCD_BLINKON;
00210 command(LCD_DISPLAYCONTROL | _displaycontrol);
00211 }
00212
00213
00214 void LiquidCrystal::scrollDisplayLeft(void) {
00215 command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
00216 }
00217 void LiquidCrystal::scrollDisplayRight(void) {
00218 command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
00219 }
00220
00221
00222 void LiquidCrystal::leftToRight(void) {
00223 _displaymode |= LCD_ENTRYLEFT;
00224 command(LCD_ENTRYMODESET | _displaymode);
00225 }
00226
00227
00228 void LiquidCrystal::rightToLeft(void) {
00229 _displaymode &= ~LCD_ENTRYLEFT;
00230 command(LCD_ENTRYMODESET | _displaymode);
00231 }
00232
00233
00234 void LiquidCrystal::autoscroll(void) {
00235 _displaymode |= LCD_ENTRYSHIFTINCREMENT;
00236 command(LCD_ENTRYMODESET | _displaymode);
00237 }
00238
00239
00240 void LiquidCrystal::noAutoscroll(void) {
00241 _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
00242 command(LCD_ENTRYMODESET | _displaymode);
00243 }
00244
00245
00246
00247 void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) {
00248 location &= 0x7;
00249 command(LCD_SETCGRAMADDR | (location << 3));
00250 for (int i=0; i<8; i++) {
00251 write(charmap[i]);
00252 }
00253 }
00254
00255
00256
00257 inline void LiquidCrystal::command(uint8_t value) {
00258 send(value, LOW);
00259 }
00260
00261 inline void LiquidCrystal::write(uint8_t value) {
00262 send(value, HIGH);
00263 }
00264
00265
00266
00267
00268 void LiquidCrystal::send(uint8_t value, uint8_t mode) {
00269 digitalWrite(_rs_pin, mode);
00270
00271
00272 if (_rw_pin != 255) {
00273 digitalWrite(_rw_pin, LOW);
00274 }
00275
00276 if (_displayfunction & LCD_8BITMODE) {
00277 write8bits(value);
00278 } else {
00279 write4bits(value>>4);
00280 write4bits(value);
00281 }
00282 }
00283
00284 void LiquidCrystal::pulseEnable(void) {
00285 digitalWrite(_enable_pin, LOW);
00286 delayMicroseconds(1);
00287 digitalWrite(_enable_pin, HIGH);
00288 delayMicroseconds(1);
00289 digitalWrite(_enable_pin, LOW);
00290 delayMicroseconds(100);
00291 }
00292
00293 void LiquidCrystal::write4bits(uint8_t value) {
00294 for (int i = 0; i < 4; i++) {
00295 pinMode(_data_pins[i], OUTPUT);
00296 digitalWrite(_data_pins[i], (value >> i) & 0x01);
00297 }
00298
00299 pulseEnable();
00300 }
00301
00302 void LiquidCrystal::write8bits(uint8_t value) {
00303 for (int i = 0; i < 8; i++) {
00304 pinMode(_data_pins[i], OUTPUT);
00305 digitalWrite(_data_pins[i], (value >> i) & 0x01);
00306 }
00307
00308 pulseEnable();
00309 }