My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LiquidCrystal.cpp 8.9KB

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