PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * LiquidCrystalFast.cpp, version 1.1
  3. *
  4. Copyright (c) 2010 John Raines
  5. Minor changes by Paul Stoffregen <paul@pjrc.com>, Copyright 2011
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General
  15. Public License along with this library; if not, write to the
  16. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. Boston, MA 02111-1307 USA
  18. */
  19. #include "LiquidCrystalFast.h"
  20. //#include <stdio.h>
  21. //#include <string.h>
  22. /******************************************/
  23. /** hardware initialization **/
  24. /******************************************/
  25. // When the display powers up, it is configured as follows:
  26. //
  27. // 1. Display clear
  28. // 2. Function set:
  29. // DL = 1; 8-bit interface data
  30. // N = 0; 1-line display
  31. // F = 0; 5x8 dot character font
  32. // 3. Display on/off control:
  33. // D = 0; Display off
  34. // C = 0; Cursor off
  35. // B = 0; Blinking off
  36. // 4. Entry mode set:
  37. // I/D = 1; Increment by 1
  38. // S = 0; No shift
  39. //
  40. // Note, however, that resetting the Arduino doesn't reset the LCD, so we
  41. // can't assume that its in that state when a sketch starts (and the
  42. // LiquidCrystalFast constructor is called).
  43. void LiquidCrystalFast::init(uint8_t rs, uint8_t rw, uint8_t enable, uint8_t en2,
  44. uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
  45. {
  46. _rs_pin = rs;
  47. _rw_pin = 255;
  48. _enable_pin = enable;
  49. _en2 = en2;
  50. _chip = 0;
  51. _scroll_count = 0; //to fix the bug if we scroll and then setCursor w/o home() or clear()
  52. _x = 0;
  53. _y = 0;
  54. _setCursFlag = 0;
  55. _direction = LCD_Right;
  56. _data_pins[0] = d0;
  57. _data_pins[1] = d1;
  58. _data_pins[2] = d2;
  59. _data_pins[3] = d3;
  60. pinMode(d0, OUTPUT); //set data pin modes
  61. pinMode(d1, OUTPUT);
  62. pinMode(d2, OUTPUT);
  63. pinMode(d3, OUTPUT);
  64. row_offsets[0] = 00; // DDRAM addresses inside the HD44780 are strange: 0-nColumns-1 on line 0
  65. row_offsets[1] = 0x40; // 64-(63+nColumns) for line 1
  66. row_offsets[2] = 0x14; // 20- (19+nColumns) for line 2 --- NOTHING FROM 40-63 !
  67. row_offsets[3] = 0x54; // 84 - (83+nColumns) for line 3 -- so 80 characters tops out at #103 !
  68. pinMode(_rs_pin, OUTPUT);
  69. // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
  70. if (rw != 255) {
  71. pinMode(rw, OUTPUT); //once in init does it
  72. digitalWrite(rw,LOW); //write data to LCD mode
  73. }
  74. pinMode(_enable_pin, OUTPUT);
  75. if( en2 != 255) pinMode(en2,OUTPUT); //4X40 LCD
  76. begin(20, 1);
  77. _rw_pin = rw; //the game to initialize the 40x4 is over
  78. }
  79. void LiquidCrystalFast::begin(uint8_t cols, uint8_t lines, uint8_t dotsize)
  80. {
  81. numcols=_numcols=cols; //there is an implied lack of trust; the private version can't be munged up by the user.
  82. numlines=_numlines=lines;
  83. row_offsets[2] = cols + row_offsets[0]; //should autoadjust for 16/20 or whatever columns now
  84. row_offsets[3] = cols + row_offsets[1];
  85. begin2( cols, lines, dotsize, _enable_pin);
  86. if (_en2 != 255) { //if we were called with a 2nd enable line i.e. 4x40 LCD
  87. row_offsets[2] = 0;
  88. row_offsets[3] = 0x40; //each line gets its own little 40 char section of DDRAM--would be fine if there were a 4x32, I suppose
  89. _chip = 2;
  90. begin2( cols, lines, dotsize,_en2);//initialize the second HD44780 chip
  91. }
  92. }
  93. void LiquidCrystalFast::begin2(uint8_t cols, uint8_t lines, uint8_t dotsize, uint8_t enable)
  94. {
  95. uint8_t displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
  96. if (lines > 1) {
  97. displayfunction |= LCD_2LINE;
  98. }
  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.
  106. // is this delay long enough for all cases??
  107. for (uint8_t i=0;i<18;i++) {
  108. delayMicroseconds(7500);
  109. }
  110. // Now we pull both RS and R/W low to begin commands
  111. digitalWrite(_rs_pin, LOW);
  112. digitalWrite(enable, LOW);
  113. //put the LCD into 4 bit mode
  114. // this is according to the hitachi HD44780 datasheet
  115. // figure 24, pg 46
  116. // we start in 8bit mode, try to set 4 bit mode
  117. // at this point we are in 8 bit mode but of course in this
  118. // interface 4 pins are dangling unconnected and the values
  119. // on them don't matter for these instructions.
  120. digitalWrite(_rs_pin, LOW);
  121. write4bits(0x03);
  122. delayMicroseconds(5000); // I have one LCD for which 4500 here was not long enough.
  123. // second try
  124. write4bits(0x03);
  125. delayMicroseconds(150); // wait
  126. // third go!
  127. write4bits(0x03);
  128. delayMicroseconds(150);
  129. // finally, set to 4-bit interface
  130. write4bits(0x02);
  131. delayMicroseconds(150);
  132. // finally, set # lines, font size, etc.
  133. command(LCD_FUNCTIONSET | displayfunction);
  134. // turn the display on with no cursor or blinking default
  135. _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; display();
  136. // clear it off
  137. clear();
  138. // Initialize to default text direction (for romance languages)
  139. _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
  140. // set the entry mode
  141. command(LCD_ENTRYMODESET | _displaymode);
  142. }
  143. /******************************************/
  144. /** high level commands, for the user! */
  145. /******************************************/
  146. void LiquidCrystalFast::clear()
  147. {
  148. if (_en2 != 255) {
  149. _chip=2;
  150. command(LCD_CLEARDISPLAY);
  151. _chip=0;
  152. command(LCD_CLEARDISPLAY);
  153. delayPerHome();
  154. setCursor(0,0);
  155. } else {
  156. command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
  157. delayPerHome();
  158. }
  159. _scroll_count = 0;
  160. }
  161. void LiquidCrystalFast::home()
  162. {
  163. commandBoth(LCD_RETURNHOME); // set cursor position to zero both chips.
  164. delayPerHome();
  165. _scroll_count = 0;
  166. if (_en2 != 255) setCursor(0,0);
  167. }
  168. // Turn the display on/off (quickly)
  169. void LiquidCrystalFast::noDisplay() {
  170. _displaycontrol &= ~LCD_DISPLAYON;
  171. commandBoth(LCD_DISPLAYCONTROL | _displaycontrol); //both chips
  172. }
  173. void LiquidCrystalFast::display() {
  174. _displaycontrol |= LCD_DISPLAYON;
  175. commandBoth(LCD_DISPLAYCONTROL | _displaycontrol); //both chips
  176. }
  177. // Turns the underline cursor on/off
  178. void LiquidCrystalFast::noCursor() {
  179. _displaycontrol &= ~LCD_CURSORON;
  180. command(LCD_DISPLAYCONTROL | _displaycontrol);
  181. }
  182. void LiquidCrystalFast::cursor() {
  183. _displaycontrol |= LCD_CURSORON;
  184. command(LCD_DISPLAYCONTROL | _displaycontrol);
  185. }
  186. // Turn on and off the blinking cursor
  187. void LiquidCrystalFast::noBlink() {
  188. _displaycontrol &= ~LCD_BLINKON;
  189. command(LCD_DISPLAYCONTROL | _displaycontrol);
  190. }
  191. void LiquidCrystalFast::blink() {
  192. _displaycontrol |= LCD_BLINKON;
  193. command(LCD_DISPLAYCONTROL | _displaycontrol);
  194. }
  195. // These commands scroll the display without changing the RAM
  196. void LiquidCrystalFast::scrollDisplayLeft(void) {
  197. commandBoth(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT); //both chips
  198. _scroll_count++;
  199. if (_scroll_count >= 40) _scroll_count -= 40; // -39< scroll_count<39
  200. }
  201. void LiquidCrystalFast::scrollDisplayRight(void) {
  202. commandBoth(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT); //both chips
  203. _scroll_count--;
  204. if (_scroll_count <= -40) _scroll_count += 40;
  205. }
  206. // This is for text that flows Left to Right
  207. void LiquidCrystalFast::leftToRight(void) {
  208. _displaymode |= LCD_ENTRYLEFT;
  209. _direction = LCD_Right;
  210. commandBoth(LCD_ENTRYMODESET | _displaymode); //both chips
  211. }
  212. // This is for text that flows Right to Left
  213. void LiquidCrystalFast::rightToLeft(void) {
  214. _displaymode &= ~LCD_ENTRYLEFT;
  215. _direction = LCD_Left;
  216. commandBoth(LCD_ENTRYMODESET | _displaymode); //both chips
  217. }
  218. // Allows us to fill the first 8 CGRAM locations
  219. // with custom characters
  220. void LiquidCrystalFast::createChar(uint8_t location, uint8_t charmap[]) {
  221. location &= 0x7; // we only have 8 locations 0-7
  222. if (_en2 == 255) {
  223. command(LCD_SETCGRAMADDR | (location << 3));
  224. for (int i=0; i<8; i++) {
  225. send(charmap[i],HIGH);
  226. }
  227. }
  228. else {
  229. uint8_t chipSave = _chip;
  230. _chip = 0;
  231. command(LCD_SETCGRAMADDR | (location << 3));
  232. for (uint8_t i=0; i<8; i++) {
  233. send(charmap[i],HIGH);
  234. }
  235. _chip = 2;
  236. command(LCD_SETCGRAMADDR | (location << 3));
  237. for (uint8_t i=0; i<8; i++) {
  238. send(charmap[i],HIGH);
  239. }
  240. _chip = chipSave;
  241. }
  242. }
  243. void LiquidCrystalFast::setCursor(uint8_t col, uint8_t row) // this can be called by the user but is also called before writing some characters.
  244. {
  245. if ( row > _numlines ) {
  246. row = _numlines-1; // we count rows starting w/0
  247. }
  248. _y = row;
  249. _x = col;
  250. _setCursFlag = 0; //user did a setCursor--clear the flag that may have been set in write()
  251. int8_t high_bit = row_offsets[row] & 0x40; // this keeps coordinates pegged to a spot on the LCD screen even if the user scrolls right or
  252. int8_t offset = col + (row_offsets[row] &0x3f) + _scroll_count; //left under program control. Previously setCursor was pegged to a location in DDRAM
  253. //the 3 quantities we add are each <40
  254. if (offset > 39) offset -= 40; // if the display is autoscrolled this method does not work, however.
  255. if (offset < 0) offset += 40;
  256. offset |= high_bit;
  257. if (_chip != (row & 0b10)) noCursor(); //turn off cursor on chip we are leaving
  258. _chip = row & 0b10; //if it is row 0 or 1 this is 0; if it is row 2 or 3 this is 2
  259. command(LCD_SETDDRAMADDR | (byte) offset );
  260. }
  261. // This will 'right justify' text from the cursor
  262. void LiquidCrystalFast::autoscroll(void) { //to count the number of times we scrolled; here we'd need to keep track of microseconds and divide. I'm not going there.
  263. _displaymode |= LCD_ENTRYSHIFTINCREMENT;
  264. commandBoth(LCD_ENTRYMODESET | _displaymode); //both chips
  265. }
  266. // This will 'left justify' text from the cursor
  267. void LiquidCrystalFast::noAutoscroll(void) {
  268. _displaymode &= ~LCD_ENTRYSHIFTINCREMENT; //both chips
  269. commandBoth(LCD_ENTRYMODESET | _displaymode);
  270. }
  271. /*************************************************/
  272. /** mid level commands, for sending data/cmds */
  273. /*************************************************/
  274. inline void LiquidCrystalFast::command(uint8_t value) {
  275. send(value, LOW);
  276. }
  277. //for many of the commands that need to be sent twice if 2 controller chips
  278. void LiquidCrystalFast::commandBoth(uint8_t value)
  279. {
  280. if (_en2 == 255) {
  281. send(value, LOW); // normal LCD, single controller chip
  282. } else {
  283. uint8_t chipSave = _chip;
  284. _chip = 0;
  285. send(value,LOW); //send command to first HD44780
  286. _chip = 2;
  287. send(value,LOW); //send to 2nd HD44780
  288. _chip=chipSave;
  289. }
  290. }
  291. //print calls this to send characters to the LCD
  292. #if defined(ARDUINO) && ARDUINO >= 100
  293. size_t LiquidCrystalFast::write(uint8_t value) {
  294. #else
  295. void LiquidCrystalFast::write(uint8_t value) {
  296. #endif
  297. if ((_scroll_count != 0) || (_setCursFlag != 0) ) setCursor(_x,_y); //first we call setCursor and send the character
  298. if ((value != '\r') && (value != '\n') ) send(value, HIGH);
  299. _setCursFlag = 0;
  300. if (_direction == LCD_Right) { // then we update the x & y location for the NEXT character
  301. _x++;
  302. if ((value == '\r') ||(_x >= _numcols) ) { //romance languages go left to right
  303. _x = 0;
  304. _y++;
  305. _setCursFlag = 1; //we'll need a setCursor() before the next char to move to begin of next line
  306. }
  307. } else {
  308. _x--;
  309. if ( (value == '\n') || (_x < 0)) { //emulate right to left text mode which is built in but would be defeated by my code above
  310. _x = _numcols-1;
  311. _y++;
  312. _setCursFlag = 1;
  313. }
  314. }
  315. if (_y >= _numlines) _y = 0; //wrap last line up to line 0
  316. #if defined(ARDUINO) && ARDUINO >= 100
  317. return 1;
  318. #endif
  319. }
  320. /****************************************/
  321. /** low level data pushing commands **/
  322. /****************************************/
  323. // write either command or data, with automatic 4/8-bit selection
  324. void LiquidCrystalFast::send(uint8_t value, uint8_t mode) {
  325. uint8_t en = _enable_pin;
  326. if ((_en2 != 255) && (_chip)) en = _en2;
  327. if (_rw_pin == 255) {
  328. delayMicroseconds(DELAYPERCHAR);
  329. } else {
  330. pinMode(_data_pins[0], INPUT);
  331. pinMode(_data_pins[1], INPUT);
  332. pinMode(_data_pins[2], INPUT);
  333. pinMode(_data_pins[3], INPUT);
  334. digitalWrite(_rw_pin, HIGH);
  335. digitalWrite(_rs_pin, LOW);
  336. uint8_t busy;
  337. do {
  338. digitalWrite(en, HIGH);
  339. busy = digitalRead(_data_pins[3]);
  340. digitalWrite(en, LOW);
  341. digitalWrite(en, HIGH);
  342. digitalWrite(en, LOW);
  343. } while (busy == HIGH);
  344. pinMode(_data_pins[0], OUTPUT);
  345. pinMode(_data_pins[1], OUTPUT);
  346. pinMode(_data_pins[2], OUTPUT);
  347. pinMode(_data_pins[3], OUTPUT);
  348. digitalWrite(_rw_pin, LOW);
  349. }
  350. digitalWrite(_rs_pin, mode);
  351. digitalWrite(_data_pins[0], value & 0x10);
  352. digitalWrite(_data_pins[1], value & 0x20);
  353. digitalWrite(_data_pins[2], value & 0x40);
  354. digitalWrite(_data_pins[3], value & 0x80);
  355. digitalWrite(en, HIGH); // enable pulse must be >450ns
  356. digitalWrite(en, LOW);
  357. digitalWrite(_data_pins[0], value & 0x01);
  358. digitalWrite(_data_pins[1], value & 0x02);
  359. digitalWrite(_data_pins[2], value & 0x04);
  360. digitalWrite(_data_pins[3], value & 0x08);
  361. digitalWrite(en, HIGH); // enable pulse must be >450ns
  362. digitalWrite(en, LOW);
  363. }
  364. // used during init
  365. void LiquidCrystalFast::write4bits(uint8_t value)
  366. {
  367. uint8_t v=value;
  368. uint8_t *pinptr = _data_pins;
  369. digitalWrite(*pinptr++, v & 1 );
  370. digitalWrite(*pinptr++,( v >>= 1) & 1 );
  371. digitalWrite(*pinptr++,( v >>= 1) & 1 );
  372. digitalWrite(*pinptr++,( v >>= 1) & 1 );
  373. byte en = _enable_pin;
  374. if ((_en2 != 255) && (_chip)) en = _en2; // 4x40 LCD with 2 controller chips with separate enable lines if we called w 2 enable pins and are on lines 2 or 3 enable chip 2
  375. digitalWrite(en, HIGH); // enable pulse must be >450ns
  376. digitalWrite(en, LOW);
  377. }