lcd.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * lcd.c:
  3. * Text-based LCD driver.
  4. * This is designed to drive the parallel interface LCD drivers
  5. * based in the Hitachi HD44780U controller and compatables.
  6. *
  7. * Copyright (c) 2012 Gordon Henderson.
  8. ***********************************************************************
  9. * This file is part of wiringPi:
  10. * https://projects.drogon.net/raspberry-pi/wiringpi/
  11. *
  12. * wiringPi is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Lesser General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * wiringPi is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  24. ***********************************************************************
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdarg.h>
  29. #include <wiringPi.h>
  30. #include "spi.h"
  31. #include "lcd.h"
  32. #ifndef TRUE
  33. # define TRUE (1==1)
  34. # define FALSE (1==2)
  35. #endif
  36. // HD44780U Commands
  37. #define LCD_CLEAR 0x01
  38. #define LCD_HOME 0x02
  39. #define LCD_ENTRY 0x04
  40. #define LCD_CTRL 0x08
  41. #define LCD_CDSHIFT 0x10
  42. #define LCD_FUNC 0x20
  43. #define LCD_CGRAM 0x40
  44. #define LCD_DGRAM 0x80
  45. // Bits in the entry register
  46. #define LCD_ENTRY_SH 0x01
  47. #define LCD_ENTRY_ID 0x02
  48. // Bits in the control register
  49. #define LCD_BLINK_CTRL 0x01
  50. #define LCD_CURSOR_CTRL 0x02
  51. #define LCD_DISPLAY_CTRL 0x04
  52. // Bits in the function register
  53. #define LCD_FUNC_F 0x04
  54. #define LCD_FUNC_N 0x08
  55. #define LCD_FUNC_DL 0x10
  56. #define LCD_CDSHIFT_RL 0x04
  57. struct lcdDataStruct {
  58. int bits, rows, cols;
  59. int rsPin, strbPin;
  60. int cx, cy;
  61. };
  62. struct lcdDataStruct *lcds[MAX_LCDS];
  63. static int lcdControl;
  64. // Row offsets
  65. static const int rowOff[4] = { 0x00, 0x40, 0x14, 0x54 };
  66. /*
  67. * strobe:
  68. * Toggle the strobe (Really the "E") pin to the device.
  69. * According to the docs, data is latched on the falling edge.
  70. *********************************************************************************
  71. */
  72. static void strobe(const struct lcdDataStruct *lcd) {
  73. // Note timing changes for new version of delayMicroseconds ()
  74. digitalWrite(lcd->strbPin, 1);
  75. delayMicroseconds(50);
  76. digitalWrite(lcd->strbPin, 0);
  77. delayMicroseconds(50);
  78. }
  79. /*
  80. * sentDataCmd:
  81. * Send an data or command byte to the display.
  82. *********************************************************************************
  83. */
  84. static void sendDataCmd(const struct lcdDataStruct *lcd, unsigned char data) {
  85. register unsigned char myData = data;
  86. unsigned char i, d4;
  87. if (lcd->bits == 4) {
  88. d4 = (myData >> 4) & 0x0F;
  89. shift_data(d4);
  90. d4 >>= 4;
  91. strobe(lcd);
  92. d4 = myData & 0x0F;
  93. shift_data(d4);
  94. } else {
  95. shift_data(myData);
  96. }
  97. strobe(lcd);
  98. }
  99. /*
  100. * putCommand:
  101. * Send a command byte to the display
  102. *********************************************************************************
  103. */
  104. static void putCommand(const struct lcdDataStruct *lcd, unsigned char command) {
  105. digitalWrite(lcd->rsPin, 0);
  106. sendDataCmd(lcd, command);
  107. delay(2);
  108. }
  109. static void put4Command(const struct lcdDataStruct *lcd,
  110. unsigned char command) {
  111. register unsigned char myCommand = command;
  112. register unsigned char i;
  113. digitalWrite(lcd->rsPin, 0);
  114. shift_data(command & 0x0F);
  115. strobe(lcd);
  116. }
  117. /*
  118. *********************************************************************************
  119. * User Callable code below here
  120. *********************************************************************************
  121. */
  122. /*
  123. * lcdHome: lcdClear:
  124. * Home the cursor or clear the screen.
  125. *********************************************************************************
  126. */
  127. void lcdHome(const int fd) {
  128. struct lcdDataStruct *lcd = lcds[fd];
  129. putCommand(lcd, LCD_HOME);
  130. lcd->cx = lcd->cy = 0;
  131. delay(5);
  132. }
  133. void lcdClear(const int fd) {
  134. struct lcdDataStruct *lcd = lcds[fd];
  135. putCommand(lcd, LCD_CLEAR);
  136. putCommand(lcd, LCD_HOME);
  137. lcd->cx = lcd->cy = 0;
  138. delay(5);
  139. }
  140. /*
  141. * lcdDisplay: lcdCursor: lcdCursorBlink:
  142. * Turn the display, cursor, cursor blinking on/off
  143. *********************************************************************************
  144. */
  145. void lcdDisplay(const int fd, int state) {
  146. struct lcdDataStruct *lcd = lcds[fd];
  147. if (state)
  148. lcdControl |= LCD_DISPLAY_CTRL;
  149. else
  150. lcdControl &= ~LCD_DISPLAY_CTRL;
  151. putCommand(lcd, LCD_CTRL | lcdControl);
  152. }
  153. void lcdCursor(const int fd, int state) {
  154. struct lcdDataStruct *lcd = lcds[fd];
  155. if (state)
  156. lcdControl |= LCD_CURSOR_CTRL;
  157. else
  158. lcdControl &= ~LCD_CURSOR_CTRL;
  159. putCommand(lcd, LCD_CTRL | lcdControl);
  160. }
  161. void lcdCursorBlink(const int fd, int state) {
  162. struct lcdDataStruct *lcd = lcds[fd];
  163. if (state)
  164. lcdControl |= LCD_BLINK_CTRL;
  165. else
  166. lcdControl &= ~LCD_BLINK_CTRL;
  167. putCommand(lcd, LCD_CTRL | lcdControl);
  168. }
  169. /*
  170. * lcdSendCommand:
  171. * Send any arbitary command to the display
  172. *********************************************************************************
  173. */
  174. void lcdSendCommand(const int fd, unsigned char command) {
  175. struct lcdDataStruct *lcd = lcds[fd];
  176. putCommand(lcd, command);
  177. }
  178. /*
  179. * lcdPosition:
  180. * Update the position of the cursor on the display.
  181. * Ignore invalid locations.
  182. *********************************************************************************
  183. */
  184. void lcdPosition(const int fd, int x, int y) {
  185. struct lcdDataStruct *lcd = lcds[fd];
  186. if ((x > lcd->cols) || (x < 0))
  187. return;
  188. if ((y > lcd->rows) || (y < 0))
  189. return;
  190. putCommand(lcd, x + (LCD_DGRAM | rowOff[y]));
  191. lcd->cx = x;
  192. lcd->cy = y;
  193. }
  194. /*
  195. * lcdCharDef:
  196. * Defines a new character in the CGRAM
  197. *********************************************************************************
  198. */
  199. void lcdCharDef(const int fd, int index, unsigned char data[8]) {
  200. struct lcdDataStruct *lcd = lcds[fd];
  201. int i;
  202. putCommand(lcd, LCD_CGRAM | ((index & 7) << 3));
  203. digitalWrite(lcd->rsPin, 1);
  204. for (i = 0; i < 8; ++i)
  205. sendDataCmd(lcd, data[i]);
  206. }
  207. /*
  208. * lcdPutchar:
  209. * Send a data byte to be displayed on the display. We implement a very
  210. * simple terminal here - with line wrapping, but no scrolling. Yet.
  211. *********************************************************************************
  212. */
  213. void lcdPutchar(const int fd, unsigned char data) {
  214. struct lcdDataStruct *lcd = lcds[fd];
  215. digitalWrite(lcd->rsPin, 1);
  216. sendDataCmd(lcd, data);
  217. if (++lcd->cx == lcd->cols) {
  218. lcd->cx = 0;
  219. if (++lcd->cy == lcd->rows)
  220. lcd->cy = 0;
  221. putCommand(lcd, lcd->cx + (LCD_DGRAM | rowOff[lcd->cy]));
  222. }
  223. }
  224. /*
  225. * lcdPuts:
  226. * Send a string to be displayed on the display
  227. *********************************************************************************
  228. */
  229. void lcdPuts(const int fd, const char *string) {
  230. while (*string)
  231. lcdPutchar(fd, *string++);
  232. }
  233. /*
  234. * lcdPrintf:
  235. * Printf to an LCD display
  236. *********************************************************************************
  237. */
  238. void lcdPrintf(const int fd, const char *message, ...) {
  239. va_list argp;
  240. char buffer[1024];
  241. va_start (argp, message);
  242. vsnprintf(buffer, 1023, message, argp);
  243. va_end (argp);
  244. lcdPuts(fd, buffer);
  245. }
  246. /*
  247. * lcdInit:
  248. * directly initialises the LCD for the CoffeePi
  249. *********************************************************************************
  250. */
  251. int lcdInit(void) {
  252. spi_init();
  253. shift_init();
  254. pinMode(LCD_RW, OUTPUT);
  255. digitalWrite(LCD_RW, LOW);
  256. return lcdInitI(2, 16, 8, LCD_RS, LCD_EN);
  257. }
  258. /*
  259. * lcdInit:
  260. * Take a lot of parameters and initialise the LCD, and return a handle to
  261. * that LCD, or -1 if any error.
  262. *********************************************************************************
  263. */
  264. int lcdInitI(const int rows, const int cols, const int bits, const int rs,
  265. const int strb) {
  266. static int initialised = 0;
  267. unsigned char func;
  268. int i;
  269. int lcdFd = -1;
  270. struct lcdDataStruct *lcd;
  271. if (initialised == 0) {
  272. initialised = 1;
  273. for (i = 0; i < MAX_LCDS; ++i)
  274. lcds[i] = NULL;
  275. }
  276. // Simple sanity checks
  277. if (!((bits == 4) || (bits == 8)))
  278. return -1;
  279. if ((rows < 0) || (rows > 20))
  280. return -1;
  281. if ((cols < 0) || (cols > 20))
  282. return -1;
  283. // Create a new LCD:
  284. for (i = 0; i < MAX_LCDS; ++i) {
  285. if (lcds[i] == NULL) {
  286. lcdFd = i;
  287. break;
  288. }
  289. }
  290. if (lcdFd == -1)
  291. return -1;
  292. lcd = (struct lcdDataStruct *) malloc(sizeof(struct lcdDataStruct));
  293. if (lcd == NULL)
  294. return -1;
  295. lcd->rsPin = rs;
  296. lcd->strbPin = strb;
  297. lcd->bits = 8; // For now - we'll set it properly later.
  298. lcd->rows = rows;
  299. lcd->cols = cols;
  300. lcd->cx = 0;
  301. lcd->cy = 0;
  302. lcds[lcdFd] = lcd;
  303. digitalWrite(lcd->rsPin, 0);
  304. pinMode(lcd->rsPin, OUTPUT);
  305. digitalWrite(lcd->strbPin, 0);
  306. pinMode(lcd->strbPin, OUTPUT);
  307. delay(35); // mS
  308. // 4-bit mode?
  309. // OK. This is a PIG and it's not at all obvious from the documentation I had,
  310. // so I guess some others have worked through either with better documentation
  311. // or more trial and error... Anyway here goes:
  312. //
  313. // It seems that the controller needs to see the FUNC command at least 3 times
  314. // consecutively - in 8-bit mode. If you're only using 8-bit mode, then it appears
  315. // that you can get away with one func-set, however I'd not rely on it...
  316. //
  317. // So to set 4-bit mode, you need to send the commands one nibble at a time,
  318. // the same three times, but send the command to set it into 8-bit mode those
  319. // three times, then send a final 4th command to set it into 4-bit mode, and only
  320. // then can you flip the switch for the rest of the library to work in 4-bit
  321. // mode which sends the commands as 2 x 4-bit values.
  322. if (bits == 4) {
  323. func = LCD_FUNC | LCD_FUNC_DL; // Set 8-bit mode 3 times
  324. put4Command(lcd, func >> 4);
  325. delay(35);
  326. put4Command(lcd, func >> 4);
  327. delay(35);
  328. put4Command(lcd, func >> 4);
  329. delay(35);
  330. func = LCD_FUNC; // 4th set: 4-bit mode
  331. put4Command(lcd, func >> 4);
  332. delay(35);
  333. lcd->bits = 4;
  334. } else {
  335. func = LCD_FUNC | LCD_FUNC_DL;
  336. putCommand(lcd, func);
  337. delay(35);
  338. putCommand(lcd, func);
  339. delay(35);
  340. putCommand(lcd, func);
  341. delay(35);
  342. }
  343. if (lcd->rows > 1) {
  344. func |= LCD_FUNC_N;
  345. putCommand(lcd, func);
  346. delay(35);
  347. }
  348. // Rest of the initialisation sequence
  349. lcdDisplay(lcdFd, TRUE);
  350. lcdCursor(lcdFd, FALSE);
  351. lcdCursorBlink(lcdFd, FALSE);
  352. lcdClear(lcdFd);
  353. putCommand(lcd, LCD_ENTRY | LCD_ENTRY_ID);
  354. putCommand(lcd, LCD_CDSHIFT | LCD_CDSHIFT_RL);
  355. return lcdFd;
  356. }