PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

395 行
10KB

  1. #include <LiquidCrystalFast.h>
  2. // Try to exercise everything in LiquidCrystal
  3. uint8_t nRows = 2; //number of rows on LCD
  4. uint8_t nColumns = 16; //number of columns
  5. // initialize the library with the numbers of the interface pins
  6. LiquidCrystalFast lcd(12, 10, 11, 5, 4, 3, 2);
  7. // LCD pins: RS RW EN D4 D5 D6 D7
  8. void setup(void) {
  9. randomSeed(analogRead(0));
  10. lcd.begin(nColumns,nRows);
  11. }
  12. void loop(void) {
  13. lcd.clear();
  14. lcd.setCursor((nColumns>>1)-2,0);
  15. lcd.print( (int)nColumns);
  16. lcd.print(",");
  17. lcd.print( (int)nRows);
  18. delay(1000);
  19. //mark the corners
  20. lcd.setCursor(0,0);
  21. lcd.print('1');
  22. lcd.setCursor(nColumns-1,nRows-1);
  23. lcd.print('4');
  24. lcd.setCursor(nColumns-1,0);
  25. lcd.print('2');
  26. lcd.setCursor(0,nRows-1);
  27. lcd.print('3');
  28. lcd.setCursor((nColumns>>1)-3,0);
  29. // lcd.print("corners ");
  30. delay(1000);
  31. lcd.setCursor((nColumns>>1)-3,0);
  32. lcd.print(" scroll ");
  33. uint8_t rand=random(150);
  34. lcd.setCursor((nColumns>>1)-3,1);
  35. lcd.print((int)rand);
  36. int i = 0;
  37. while (i < rand) {
  38. lcd.scrollDisplayLeft();
  39. i++;
  40. delay(100);
  41. }
  42. // now we repeat the corner code to be sure we fixed the behavior of setCursor after scroll
  43. delay(1000);
  44. //mark the corners
  45. lcd.setCursor(0,0);
  46. lcd.print('a');
  47. lcd.setCursor(nColumns-1,nRows-1);
  48. lcd.print('d');
  49. lcd.setCursor(nColumns-1,0);
  50. lcd.print('b');
  51. lcd.setCursor(0,nRows-1);
  52. lcd.print('c');
  53. lcd.setCursor((nColumns>>1)-3,0);
  54. lcd.print("corners ");
  55. delay(1000);
  56. lcd.clear();
  57. lcd.setCursor(0,0);
  58. lcd.print('1');
  59. lcd.setCursor(nColumns-1,nRows-1);
  60. lcd.print('4');
  61. lcd.setCursor(nColumns-1,0);
  62. lcd.print('3');
  63. lcd.setCursor(0,nRows-1);
  64. lcd.print('2');
  65. lcd.setCursor((nColumns>>1)-3,0);
  66. lcd.print("corners");
  67. delay(1000);
  68. lcd.setCursor((nColumns>>1)-3,1);
  69. lcd.print(" scroll ");
  70. rand = random(150);
  71. lcd.setCursor((nColumns>>1)-3,2);
  72. lcd.print((int) rand);
  73. i = 0;
  74. while (i < rand) {
  75. lcd.scrollDisplayRight();
  76. i++;
  77. delay(100);
  78. }
  79. // now we repeat the corner code to be sure we fixed the behavior of setCursor after scroll
  80. delay(1000);
  81. //mark the corners -- all of these corner tests are good ways to find off by one errors!
  82. lcd.setCursor(nColumns-1,nRows-1);
  83. lcd.print('d');
  84. lcd.setCursor(nColumns-1,0);
  85. lcd.print('b');
  86. lcd.setCursor(0,nRows-1);
  87. lcd.print('c');
  88. lcd.setCursor(0,0);
  89. lcd.print('a');
  90. lcd.setCursor((nColumns>>1)-3,0);
  91. lcd.print("corners ");
  92. delay(1000);
  93. //Cursor on/off
  94. lcd.clear();
  95. lcd.setCursor (0,0);
  96. lcd.print("Cursor off");
  97. lcd.noCursor();
  98. delay(1000);
  99. // Turn on the cursor:
  100. lcd.setCursor (0,0);
  101. lcd.print("Cursor on ");
  102. lcd.cursor();
  103. delay(1000);
  104. lcd.setCursor (0,nRows-1);
  105. lcd.print("Cursor off");
  106. lcd.noCursor();
  107. delay(1000);
  108. // Turn on the cursor:
  109. lcd.setCursor (0,nRows-1);
  110. lcd.print("Cursor on ");
  111. lcd.cursor();
  112. delay(1000);
  113. //==enumerate lines on the display
  114. lcd.clear();
  115. i = 0;
  116. while (i < nRows) {
  117. lcd.setCursor(nColumns-8,i);
  118. lcd.print("ROW ");
  119. lcd.print(i+1);
  120. i++;
  121. }
  122. delay(1000);
  123. //=====try println
  124. lcd.clear();
  125. lcd.setCursor(0,0);
  126. i = 0;
  127. while (i < nRows) {
  128. lcd.print("Println:# ");
  129. lcd.println(i+1);
  130. i++;
  131. }
  132. delay(1000);
  133. //=====Line wrap demo:
  134. lcd.clear();
  135. lcd.setCursor(nColumns-4,0);
  136. lcd.print("LINEWRAP01234567890");
  137. delay(200);
  138. if (nRows>=2) {
  139. lcd.setCursor(nColumns-4,1);
  140. lcd.print("linewrap01234567890");
  141. delay(1000);
  142. if (nRows >=4) {
  143. lcd.setCursor(nColumns-4,3);
  144. lcd.print("linewrap01234567890");
  145. delay(200);
  146. }
  147. }
  148. delay(1000);
  149. i = 0;
  150. while (i < 11) {
  151. lcd.scrollDisplayLeft();
  152. i++;
  153. delay(100);
  154. }
  155. delay(1000);
  156. lcd.home();
  157. int length = nRows * nColumns;
  158. lcd.setCursor(0,0);
  159. char text[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  160. text[length] = '\0';
  161. 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
  162. delay(2000);
  163. uint8_t a = 0;
  164. 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
  165. // 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
  166. // it wraps onto the first line. This means that lcd.print("abcdefghijklmno"); will print from right to left as you desire.
  167. lcd.clear();
  168. lcd.rightToLeft();
  169. lcd.setCursor(nColumns-1,a); //now we print it in right to left mode to test the same thing
  170. lcd.print (text);
  171. delay(3000);
  172. lcd.leftToRight();
  173. 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:
  174. uint8_t repetitions = 20;
  175. char blanks[]=" ";
  176. blanks[length] = '\0';
  177. while (repetitions--) { //fill every screen pixel with text, then fill every pixel with blanks and repeat.
  178. lcd.setCursor(0,0);
  179. lcd.print(text);
  180. lcd.setCursor(0,0);
  181. lcd.print(blanks);
  182. }
  183. long endTime = millis();
  184. lcd.clear();
  185. lcd.setCursor(0,0);
  186. lcd.print("Benchmark took ");
  187. lcd.setCursor(0,1);
  188. lcd.print(endTime - startTime);
  189. lcd.print(" millisecs.");
  190. delay(5000);
  191. //======setCursor===
  192. // loop from ASCII 'a' to ASCII 'z':
  193. lcd.home();
  194. char thisLetter = 'a';
  195. // loop over the rows:
  196. for (int thisRow = 0; thisRow < nRows; thisRow++) {
  197. // loop over the columns:
  198. for (int thisCol = 0; thisCol < nColumns; thisCol++) {
  199. // set the cursor position:
  200. lcd.setCursor(thisCol,thisRow);
  201. // print the letter:
  202. lcd.print(thisLetter);
  203. thisLetter++;
  204. if (thisLetter > 'z') thisLetter = 'a';
  205. delay(100);
  206. }
  207. }
  208. //========Autoscroll: -- my arch nemesis !
  209. lcd.clear();
  210. // set the cursor to (0,0):
  211. lcd.setCursor(0, 0);
  212. // print from 0 to 9:
  213. lcd.print("Autoscroll");
  214. for (char thisChar = '1'; thisChar < '9'; thisChar++) {
  215. lcd.print(thisChar);
  216. delay(100);
  217. }
  218. // set the cursor to (nColumns,1):
  219. lcd.setCursor(0,1);
  220. lcd.print("Autoscroll");
  221. // set the display to automatically scroll:
  222. lcd.autoscroll();
  223. // print from 0 to 9:
  224. for (int thisChar = 0; thisChar < 10; thisChar++) {
  225. lcd.print(thisChar);
  226. delay(100);
  227. }
  228. // turn off automatic scrolling
  229. lcd.noAutoscroll();
  230. if (nRows>2) {
  231. //========Autoscroll: -- my arch nemesis !
  232. // set the cursor to (0,0):
  233. lcd.setCursor(0, nRows-2);
  234. // print from 0 to 9:
  235. lcd.print("Autoscroll");
  236. for (char thisChar = '1'; thisChar < '9'; thisChar++) {
  237. lcd.print(thisChar);
  238. delay(100);
  239. }
  240. // set the cursor to (nColumns,1):
  241. lcd.setCursor(0,nRows-1);
  242. lcd.print("Autoscroll");
  243. // set the display to automatically scroll:
  244. lcd.autoscroll();
  245. // print from 0 to 9:
  246. for (int thisChar = 0; thisChar < 10; thisChar++) {
  247. lcd.print(thisChar);
  248. delay(200);
  249. }
  250. // turn off automatic scrolling
  251. lcd.noAutoscroll();
  252. }
  253. //====== Turn on the blinking cursor:
  254. lcd.clear();
  255. lcd.setCursor(0,nRows-1);
  256. lcd.print("Blinking Cursor");
  257. lcd.blink();
  258. delay(1000);
  259. // Turn off the blinking cursor:
  260. lcd.noBlink();
  261. delay(1000);
  262. lcd.setCursor(0,0);
  263. lcd.print("Blinking Cursor");
  264. lcd.blink();
  265. delay(1000);
  266. // Turn off the blinking cursor:
  267. lcd.noBlink();
  268. delay(1000);
  269. //=====Turn display on and off:
  270. lcd.clear();
  271. lcd.setCursor(0,0);
  272. lcd.print("turn display off");
  273. delay(1000);
  274. // Turn off the display:
  275. lcd.noDisplay();
  276. delay(2000);
  277. lcd.setCursor(0,0);
  278. lcd.print("Turn Display ON "); //sent to the LCD while the display is turned off.
  279. // Turn on the display:
  280. lcd.display();
  281. delay(1000);
  282. lcd.clear();
  283. lcd.home();
  284. lcd.setCursor(0,0);
  285. lcd.print(" Shift under program control ");
  286. // scroll 27 positions (display length + string length) to the left:
  287. for (int positionCounter = 0; positionCounter < 27; positionCounter++) {
  288. // scroll one position left:
  289. lcd.scrollDisplayLeft();
  290. // wait a bit:
  291. delay(100);
  292. }
  293. // scroll 27 positions (display length + string length) to the right:
  294. for (int positionCounter = 0; positionCounter < 27; positionCounter++) {
  295. // scroll one position right:
  296. lcd.scrollDisplayRight();
  297. // wait a bit:
  298. delay(100);
  299. }
  300. delay(1500);
  301. //======Text direction
  302. lcd.clear();
  303. lcd.setCursor(0,0);
  304. char thisChar ='a';
  305. for (int i = 0;i!=30; i++) {
  306. // reverse directions at 'm':
  307. if (thisChar == 'm') {
  308. // go right for the next letter
  309. lcd.rightToLeft();
  310. }
  311. // reverse again at 's':
  312. if (thisChar == 's') {
  313. // go left for the next letter
  314. lcd.leftToRight();
  315. }
  316. // reset at 'z':
  317. if (thisChar > 'z') {
  318. // go to (0,0):
  319. lcd.home();
  320. // start again at 0
  321. thisChar = 'a';
  322. }
  323. // print the character
  324. lcd.print(thisChar);
  325. delay(200);
  326. //increment the letter:
  327. thisChar++;
  328. }
  329. lcd.clear();
  330. //======define charset
  331. uint8_t bell[8] = {0x4,0xe,0xe,0xe,0x1f,0x0,0x4};
  332. uint8_t note[8] = {0x2,0x3,0x2,0xe,0x1e,0xc,0x0};
  333. uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0};
  334. uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0};
  335. uint8_t duck[8] = {0x0,0xc,0x1d,0xf,0xf,0x6,0x0};
  336. uint8_t check[8] = {0x0,0x1,0x3,0x16,0x1c,0x8,0x0};
  337. uint8_t cross[8] = {0x0,0x1b,0xe,0x4,0xe,0x1b,0x0};
  338. uint8_t retarrow[8] = { 0x1,0x1,0x5,0x9,0x1f,0x8,0x4};
  339. lcd.createChar(0, bell);
  340. lcd.createChar(1, note);
  341. lcd.createChar(2, clock);
  342. lcd.createChar(3, heart);
  343. lcd.createChar(4, duck);
  344. lcd.createChar(5, check);
  345. lcd.createChar(6, cross);
  346. lcd.createChar(7, retarrow);
  347. lcd.home();
  348. i = 0;
  349. lcd.clear();
  350. while (i<nRows) {
  351. lcd.setCursor(0,i);
  352. lcd.print("user:");
  353. for (int j=0; j<7; j++) {
  354. lcd.write(j);
  355. }
  356. i++;
  357. }
  358. delay(2000);
  359. }