My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LiquidCrystalRus.cpp 11KB

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