lcd.cpp 10 KB

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