LiquidCrystalRus.cpp 11 KB

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