PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

3 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. #include <LiquidCrystalFast.h>
  2. // Try to exercise everything in LiquidCrystalFast for test purposes--: You should be able to modify the first 4 lines
  3. // of code to test different options and swap LCD boards (with contrast potentiometers attached directly) by plugging them directly
  4. // into the row of digital sockets on the Mega opposite the USB socket. Be careful about current draw on the backlight pins!!
  5. //Arduino can only power these if current draw is <= 40 mAmps
  6. uint8_t nRows = 4; //number of rows on LCD
  7. uint8_t nColumns =16; //number of columns
  8. uint8_t rw = 255; //255 if rw is connected vs 47 (or 49 for the 24x2) in the examples below; it should be pulled to GND if not controlled by the interface.
  9. LiquidCrystalFast lcd(49,47,45, 35,33,31,29); //there are multiple versions of this commented below; paste 'em in
  10. //this is kind of interesting: the LIquidCrystal object is created and init/begin is run
  11. //out here before setup(); before the board has power on the Mega!!! We need another begin to really
  12. //start the board later.
  13. //======for an LCD with a single HD44780 type chip and 16 pin interface (eg 16x2 20x2 16x4 20x4) 4 data pinshttp://healthriskappraisal.org/LiquidCrystal440.zip
  14. //Alternate interfaces to test for 2x16,4x20,4x16:
  15. //LiquidCrystalFast lcd(49,47,45, 35,33,31,29); // rs,rw,en, 4 data pins remember to set rw = 255 above
  16. //LiquidCrystalFast lcd(49,47,45, 43,41,39,37, 35,33,31,29); // rs,rw,en 8 data pins remember to set rw = 255 above
  17. //LiquidCrystalFast lcd(49,45, 35,33,31,29); // rs,en, 4 data pins remember to set rw = 47 above
  18. //LiquidCrystalFast lcd(49,45, 43,41,39,37, 35,33,31,29); // rs,en 8 data pins remember to set rw = 47 above
  19. //alternate interfaces to test for 4x40:
  20. //LiquidCrystalFast lcd(48,47,46,52, 41,40,39,38); rs,rw,en1,en2, 4 data lines set rw to 255 I don't see any way to eliminate rw and include en2 with fcn overloading.
  21. //LiquidCrystalFast lcd(48,255,46,52, 41,40,39,38); rs,rw,en1,en2, 4 data lines set rw to 47 but this trick should let me use a grounded RW and skip the checkLcdBusyFlag if I want.
  22. // Connections this assumes we attached a potentiometer across LCD pins 1,2 and 3 with a standard 16 pin straight interface
  23. // and then plugged the LCD into the distal row of digital pins on a Mega
  24. // with the LCD extending away from the mega board:
  25. // this frees up the PWM pins for other uses.
  26. // rs (LCD pin 4) to Arduino pin 49
  27. // rw (LCD pin 5) to Arduino pin 47
  28. // enable (LCD pin 6) to Arduino pin 45
  29. // LCD pin 15 to Arduino pin 27
  30. // LCD pins d4, d5, d6, d7 to Arduino pins 35,33,31,29
  31. //Arduino pins 51,43,41,39,37,23 are unused and hard to get at--you could interpose something like a stackable header between
  32. // the used pins and the LCD; then you could access the unused pins
  33. //=====for a 4x40 LCD with 2 HD44780 type chips and 17 pin interface in 2 rows of 9; the pins are not arranged for
  34. // use with a breadboard, although you could solder a female socket onto the LCD board and use wires inserted
  35. // female sockets or (as I did) to solder in stackable headers so that you can use the breadboard but
  36. // also can plug it into the 2 rows of digital sockets on the Mega.
  37. // LCD Mega Signal
  38. // 18 Gnd Backlight draws 480 mAmps!
  39. // 17 +5V
  40. // 16 (53) not used
  41. // 15 52 En2 -- enable the 2nd HD44780 chip which controls the bottom 2 rows of the display
  42. // 14 51* +5V
  43. // 13 50* Gnd
  44. // 12 -- Contrast resistor to Gnd
  45. // 11 48 RS
  46. // 10 47* RW -- could be strapped to Gnd
  47. // 9 46 En1 -- enable the 1st HD44780 which controls the top 2 rows
  48. // 5-8 42-45* Data 0-3 not used in 4 bit modes
  49. // 1-4 38-41 Data 4-7
  50. //This mode for the 4x40 is not working now://LiquidCrystalFast lcd(48,47,46,52, 45,44,43,42, 41,40,39,38); // rs,rw,en1,en2, 8 data lines remember to set rw =255
  51. //=========================================
  52. //alternate versions to test for 2x24: I have not seen this one actually work.
  53. //LiquidCrystalFast lcd(50,49,48, 47,46,45,44, 43,42,41,40); // rs,rw,en 8 data pins remember to set rw = 255 above
  54. //LiquidCrystalFast lcd(50,49,48, 43,42,41,40); // rs,rw,en 4 data pins remember to set rw = 255 above
  55. //LiquidCrystalFast lcd(50,48, 47,46,45,44, 43,42,41,40); // rs,en 8 data pins remember to set rw = 49 above -- these interfaces generally have unreliable initialization
  56. //LiquidCrystalFast lcd(50,48, 43,42,41,40); // rs,en 4 data pins remember to set rw = 49 above -- these interfaces generally have unreliable initialization
  57. //====2x24 LCD also has a non standard pinout and pins are numbered in the opposite direction:
  58. // 1 53 Gnd
  59. // 2 52 +5V
  60. // 3 -- Contrast
  61. // 4 50 RS
  62. // 5 49 RW
  63. // 6 48 EN
  64. // 7-10 44-47 Data 0-3 not used in 4 bit mode
  65. // 11-14 40-43 Data 4-7
  66. // 15 -- Backlight +5v 147 mAmps!
  67. // 16 -- Backlight Gnd
  68. byte BACKLIGHT, BACKLTGND, POWER5V, GNDMain;
  69. void setup(void) {
  70. // set some digital pins to high and low to provide LCD with power and gnd:
  71. // most of them: 2x16,4x20 4x16
  72. BACKLIGHT = 27; // pin 27 will control the backlight //before I solder pins in, I check current on the backlight
  73. BACKLTGND = 25; // PIN 25 will be set LOW to provide a Gnd for the backlight
  74. POWER5V = 53; // we will set this HIGH to provide power on pin 2 of the LCD
  75. GNDMain = 255; // next to pin 53 (in the pin "55" position) is a GND--pin 1 of the LCD goes there
  76. if ((nColumns == 40) && (nRows ==4)) { //4x40
  77. BACKLIGHT = 255; // Digital IO pins cannot control the backlight for the 4x40 double HD44780 LCD draws 480 mAmps !!
  78. BACKLTGND = 255; //Arduino is limited to 40 mAmps
  79. POWER5V = 51; // we will set this HIGH to provide power on pin 2 of the LCD
  80. GNDMain = 50;
  81. }
  82. if ((nRows ==2) && (nColumns ==24) ) { //2x24
  83. BACKLIGHT = 255; // the 2x24 draws 147 mAmps !!
  84. BACKLTGND = 255; //
  85. POWER5V = 53; // we will set this HIGH to provide power on pin 2 of the LCD
  86. GNDMain = 52;
  87. }
  88. pinMode(POWER5V, OUTPUT); //We're using a digital out as a 5V power source for the LCD
  89. digitalWrite(POWER5V, HIGH);
  90. if (BACKLIGHT != 255) {
  91. pinMode(BACKLIGHT, OUTPUT); //set 255 if you need to wire backlight to gnd bo high current draw
  92. digitalWrite(BACKLIGHT, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  93. pinMode(BACKLTGND, OUTPUT); //We're using a digital out as a GND for the backlight
  94. digitalWrite(BACKLTGND,LOW);
  95. }
  96. if (GNDMain != 255) {
  97. pinMode (GNDMain, OUTPUT);
  98. digitalWrite(GNDMain, LOW);
  99. }
  100. if (rw != 255) {
  101. pinMode(rw,OUTPUT);
  102. digitalWrite(rw,LOW);
  103. }
  104. pinMode(13,OUTPUT);
  105. randomSeed(analogRead(0)); //read unconnected pin to seed random number generator
  106. lcd.begin(nColumns,nRows); //this is absolutely needed with this arrangement on the Mega--power was not
  107. // applied to the LCD when it was initialized as lcd was instantiated above!!
  108. }
  109. void loop(void) { //this runs the LCD with the interface selected above through a fairly comprehensive series of tests
  110. lcd.clear();
  111. lcd.setCursor((nColumns>>1)-2,0);
  112. lcd.print( (int)nColumns);
  113. lcd.print(",");
  114. lcd.print( (int)nRows);
  115. delay(1000);
  116. //mark the corners
  117. lcd.setCursor(0,0);
  118. lcd.print('1');
  119. lcd.setCursor(nColumns-1,nRows-1);
  120. lcd.print('4');
  121. lcd.setCursor(nColumns-1,0);
  122. lcd.print('2');
  123. lcd.setCursor(0,nRows-1);
  124. lcd.print('3');
  125. lcd.setCursor((nColumns>>1)-3,0);
  126. // lcd.print("corners ");
  127. delay(1000);
  128. lcd.setCursor((nColumns>>1)-3,0);
  129. lcd.print(" scroll ");
  130. uint8_t rand=random(150);
  131. lcd.setCursor((nColumns>>1)-3,1);
  132. lcd.print((int)rand);
  133. int i = 0;
  134. while (i < rand) {
  135. lcd.scrollDisplayLeft();
  136. i++;
  137. delay(100);
  138. }
  139. // now we repeat the corner code to be sure we fixed the behavior of setCursor after scroll
  140. delay(1000);
  141. //mark the corners
  142. lcd.setCursor(0,0);
  143. lcd.print('a');
  144. lcd.setCursor(nColumns-1,nRows-1);
  145. lcd.print('d');
  146. lcd.setCursor(nColumns-1,0);
  147. lcd.print('b');
  148. lcd.setCursor(0,nRows-1);
  149. lcd.print('c');
  150. lcd.setCursor((nColumns>>1)-3,0);
  151. lcd.print("corners ");
  152. delay(1000);
  153. lcd.clear();
  154. lcd.setCursor(0,0);
  155. lcd.print('1');
  156. lcd.setCursor(nColumns-1,nRows-1);
  157. lcd.print('4');
  158. lcd.setCursor(nColumns-1,0);
  159. lcd.print('3');
  160. lcd.setCursor(0,nRows-1);
  161. lcd.print('2');
  162. lcd.setCursor((nColumns>>1)-3,0);
  163. lcd.print("corners");
  164. delay(1000);
  165. lcd.setCursor((nColumns>>1)-3,1);
  166. lcd.print(" scroll ");
  167. rand = random(150);
  168. lcd.setCursor((nColumns>>1)-3,2);
  169. lcd.print((int) rand);
  170. i = 0;
  171. while (i < rand) {
  172. lcd.scrollDisplayRight();
  173. i++;
  174. delay(100);
  175. }
  176. // now we repeat the corner code to be sure we fixed the behavior of setCursor after scroll
  177. delay(1000);
  178. //mark the corners -- all of these corner tests are good ways to find off by one errors!
  179. lcd.setCursor(nColumns-1,nRows-1);
  180. lcd.print('d');
  181. lcd.setCursor(nColumns-1,0);
  182. lcd.print('b');
  183. lcd.setCursor(0,nRows-1);
  184. lcd.print('c');
  185. lcd.setCursor(0,0);
  186. lcd.print('a');
  187. lcd.setCursor((nColumns>>1)-3,0);
  188. lcd.print("corners ");
  189. delay(1000);
  190. //Cursor on/off
  191. lcd.clear();
  192. lcd.setCursor (0,0);
  193. lcd.print("Cursor off");
  194. lcd.noCursor();
  195. delay(1000);
  196. // Turn on the cursor:
  197. lcd.setCursor (0,0);
  198. lcd.print("Cursor on ");
  199. lcd.cursor();
  200. delay(1000);
  201. lcd.setCursor (0,nRows-1);
  202. lcd.print("Cursor off");
  203. lcd.noCursor();
  204. delay(1000);
  205. // Turn on the cursor:
  206. lcd.setCursor (0,nRows-1);
  207. lcd.print("Cursor on ");
  208. lcd.cursor();
  209. delay(1000);
  210. //==enumerate lines on the display
  211. lcd.clear();
  212. i = 0;
  213. while (i < nRows) {
  214. lcd.setCursor(nColumns-8,i);
  215. lcd.print("ROW ");
  216. lcd.print(i+1);
  217. i++;
  218. }
  219. delay(1000);
  220. //=====try println
  221. lcd.clear();
  222. lcd.setCursor(0,0);
  223. i = 0;
  224. while (i < nRows) {
  225. lcd.print("Println:# ");
  226. lcd.println(i+1);
  227. i++;
  228. }
  229. delay(1000);
  230. //=====Line wrap demo:
  231. lcd.clear();
  232. lcd.setCursor(nColumns-4,0);
  233. lcd.print("LINEWRAP01234567890");
  234. delay(200);
  235. if (nRows>=2) {
  236. lcd.setCursor(nColumns-4,1);
  237. lcd.print("linewrap01234567890");
  238. delay(1000);
  239. if (nRows >=4) {
  240. lcd.setCursor(nColumns-4,3);
  241. lcd.print("linewrap01234567890");
  242. delay(200);
  243. }
  244. }
  245. delay(1000);
  246. i = 0;
  247. while (i < 11) {
  248. lcd.scrollDisplayLeft();
  249. i++;
  250. delay(100);
  251. }
  252. delay(1000);
  253. lcd.home();
  254. int length = nRows * nColumns;
  255. lcd.setCursor(0,0);
  256. char text[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  257. text[length] = '\0';
  258. lcd.print(text); //here we are printing the entire screen with one long string lets you be sure that linewrap happens exactly as it should
  259. delay(2000);
  260. uint8_t a =0;
  261. if ((nColumns == 8) && (nRows == 2) ) a=1; //This is a trick to make a 'crazy 8' 16x1 LCD work reasonably in a right to left language; I suppose it could be added to the library routine
  262. // as a conditional but that seems like too much support. what happens is that we write the 2nd line first; when that fills with (8) characters
  263. // it wraps onto the first line. This means that lcd.print("abcdefghijklmno"); will print from right to left as you desire.
  264. lcd.clear();
  265. lcd.rightToLeft();
  266. lcd.setCursor(nColumns-1,a); //now we print it in right to left mode to test the same thing
  267. lcd.print (text);
  268. delay(3000);
  269. lcd.leftToRight();
  270. long startTime=millis(); //let's try to benchmark how fast we can go; this will give us an idea about speed of the various interfaces 4/8 bit and checking busy flag or not:
  271. uint8_t repetitions = 20;
  272. char blanks[]=" ";
  273. blanks[length] = '\0';
  274. while (repetitions--) { //fill every screen pixel with text, then fill every pixel with blanks and repeat.
  275. lcd.setCursor(0,0);
  276. lcd.print(text);
  277. lcd.setCursor(0,0);
  278. lcd.print(blanks);
  279. }
  280. long endTime = millis();
  281. lcd.clear();
  282. lcd.setCursor(0,0);
  283. lcd.print("Benchmark took ");
  284. lcd.setCursor(0,1);
  285. lcd.print(endTime - startTime);
  286. lcd.print(" millisecs.");
  287. delay(5000);
  288. //======setCursor===
  289. // loop from ASCII 'a' to ASCII 'z':
  290. lcd.home();
  291. char thisLetter = 'a';
  292. // loop over the rows:
  293. for (int thisRow = 0; thisRow < nRows; thisRow++) {
  294. // loop over the columns:
  295. for (int thisCol = 0; thisCol < nColumns; thisCol++) {
  296. // set the cursor position:
  297. lcd.setCursor(thisCol,thisRow);
  298. // print the letter:
  299. lcd.print(thisLetter);
  300. thisLetter++;
  301. if (thisLetter > 'z') thisLetter = 'a';
  302. delay(100);
  303. }
  304. }
  305. //========Autoscroll: -- my arch nemesis !
  306. lcd.clear();
  307. // set the cursor to (0,0):
  308. lcd.setCursor(0, 0);
  309. // print from 0 to 9:
  310. lcd.print("Autoscroll");
  311. for (char thisChar = '1'; thisChar < '9'; thisChar++) {
  312. lcd.print(thisChar);
  313. delay(100);
  314. }
  315. // set the cursor to (nColumns,1):
  316. lcd.setCursor(0,1);
  317. lcd.print("Autoscroll");
  318. // set the display to automatically scroll:
  319. lcd.autoscroll();
  320. // print from 0 to 9:
  321. for (int thisChar = 0; thisChar < 10; thisChar++) {
  322. lcd.print(thisChar);
  323. delay(100);
  324. }
  325. // turn off automatic scrolling
  326. lcd.noAutoscroll();
  327. if (nRows>2) {
  328. //========Autoscroll: -- my arch nemesis !
  329. // set the cursor to (0,0):
  330. lcd.setCursor(0, nRows-2);
  331. // print from 0 to 9:
  332. lcd.print("Autoscroll");
  333. for (char thisChar = '1'; thisChar < '9'; thisChar++) {
  334. lcd.print(thisChar);
  335. delay(100);
  336. }
  337. // set the cursor to (nColumns,1):
  338. lcd.setCursor(0,nRows-1);
  339. lcd.print("Autoscroll");
  340. // set the display to automatically scroll:
  341. lcd.autoscroll();
  342. // print from 0 to 9:
  343. for (int thisChar = 0; thisChar < 10; thisChar++) {
  344. lcd.print(thisChar);
  345. delay(200);
  346. }
  347. // turn off automatic scrolling
  348. lcd.noAutoscroll();
  349. }
  350. //====== Turn on the blinking cursor:
  351. lcd.clear();
  352. lcd.setCursor(0,nRows-1);
  353. lcd.print("Blinking Cursor");
  354. lcd.blink();
  355. delay(1000);
  356. // Turn off the blinking cursor:
  357. lcd.noBlink();
  358. delay(1000);
  359. lcd.setCursor(0,0);
  360. lcd.print("Blinking Cursor");
  361. lcd.blink();
  362. delay(1000);
  363. // Turn off the blinking cursor:
  364. lcd.noBlink();
  365. delay(1000);
  366. //=====Turn display on and off:
  367. lcd.clear();
  368. lcd.setCursor(0,0);
  369. lcd.print("turn display off");
  370. delay(1000);
  371. // Turn off the display:
  372. lcd.noDisplay();
  373. delay(2000);
  374. lcd.setCursor(0,0);
  375. lcd.print("Turn Display ON "); //sent to the LCD while the display is turned off.
  376. // Turn on the display:
  377. lcd.display();
  378. delay(1000);
  379. lcd.clear();
  380. lcd.home();
  381. lcd.setCursor(0,0);
  382. lcd.print(" Shift under program control ");
  383. // scroll 27 positions (display length + string length) to the left:
  384. for (int positionCounter = 0; positionCounter < 27; positionCounter++) {
  385. // scroll one position left:
  386. lcd.scrollDisplayLeft();
  387. // wait a bit:
  388. delay(100);
  389. }
  390. // scroll 27 positions (display length + string length) to the right:
  391. for (int positionCounter = 0; positionCounter < 27; positionCounter++) {
  392. // scroll one position right:
  393. lcd.scrollDisplayRight();
  394. // wait a bit:
  395. delay(100);
  396. }
  397. delay(1500);
  398. //======Text direction
  399. lcd.clear();
  400. lcd.setCursor(0,0);
  401. char thisChar ='a';
  402. for (int i = 0;i!=30; i++) {
  403. // reverse directions at 'm':
  404. if (thisChar == 'm') {
  405. // go right for the next letter
  406. lcd.rightToLeft();
  407. }
  408. // reverse again at 's':
  409. if (thisChar == 's') {
  410. // go left for the next letter
  411. lcd.leftToRight();
  412. }
  413. // reset at 'z':
  414. if (thisChar > 'z') {
  415. // go to (0,0):
  416. lcd.home();
  417. // start again at 0
  418. thisChar = 'a';
  419. }
  420. // print the character
  421. lcd.print(thisChar);
  422. delay(200);
  423. //increment the letter:
  424. thisChar++;
  425. }
  426. lcd.clear();
  427. //======define charset
  428. uint8_t bell[8] = {0x4,0xe,0xe,0xe,0x1f,0x0,0x4};
  429. uint8_t note[8] = {0x2,0x3,0x2,0xe,0x1e,0xc,0x0};
  430. uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0};
  431. uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0};
  432. uint8_t duck[8] = {0x0,0xc,0x1d,0xf,0xf,0x6,0x0};
  433. uint8_t check[8] = {0x0,0x1,0x3,0x16,0x1c,0x8,0x0};
  434. uint8_t cross[8] = {0x0,0x1b,0xe,0x4,0xe,0x1b,0x0};
  435. uint8_t retarrow[8] = { 0x1,0x1,0x5,0x9,0x1f,0x8,0x4};
  436. lcd.createChar(0, bell);
  437. lcd.createChar(1, note);
  438. lcd.createChar(2, clock);
  439. lcd.createChar(3, heart);
  440. lcd.createChar(4, duck);
  441. lcd.createChar(5, check);
  442. lcd.createChar(6, cross);
  443. lcd.createChar(7, retarrow);
  444. lcd.home();
  445. i = 0;
  446. lcd.clear();
  447. while (i<nRows) {
  448. lcd.setCursor(0,i);
  449. lcd.print("user:");
  450. for (int j=0; j<7; j++) {
  451. lcd.write(j);
  452. }
  453. i++;
  454. }
  455. delay(2000);
  456. }