LiquidCrystal.cpp 11 KB

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