lcd.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <stdint.h>
  29. #include <stdarg.h>
  30. #include "wiringPi.h"
  31. #include "lcd.h"
  32. // Commands
  33. #define LCD_CLEAR 0x01
  34. #define LCD_HOME 0x02
  35. #define LCD_ENTRY 0x04
  36. #define LCD_ON_OFF 0x08
  37. #define LCD_CDSHIFT 0x10
  38. #define LCD_FUNC 0x20
  39. #define LCD_CGRAM 0x40
  40. #define LCD_DGRAM 0x80
  41. #define LCD_ENTRY_SH 0x01
  42. #define LCD_ENTRY_ID 0x02
  43. #define LCD_ON_OFF_B 0x01
  44. #define LCD_ON_OFF_C 0x02
  45. #define LCD_ON_OFF_D 0x04
  46. #define LCD_FUNC_F 0x04
  47. #define LCD_FUNC_N 0x08
  48. #define LCD_FUNC_DL 0x10
  49. #define LCD_CDSHIFT_RL 0x04
  50. struct lcdDataStruct
  51. {
  52. uint8_t bits, rows, cols ;
  53. uint8_t rsPin, strbPin ;
  54. uint8_t dataPins [8] ;
  55. } ;
  56. struct lcdDataStruct *lcds [MAX_LCDS] ;
  57. /*
  58. * strobe:
  59. * Toggle the strobe (Really the "E") pin to the device.
  60. * According to the docs, data is latched on the falling edge.
  61. *********************************************************************************
  62. */
  63. static void strobe (struct lcdDataStruct *lcd)
  64. {
  65. digitalWrite (lcd->strbPin, 1) ; delayMicroseconds (1) ;
  66. digitalWrite (lcd->strbPin, 0) ; delayMicroseconds (50) ;
  67. }
  68. /*
  69. * sentDataCmd:
  70. * Send an data or command byte to the display.
  71. *********************************************************************************
  72. */
  73. static void sendDataCmd (struct lcdDataStruct *lcd, uint8_t data)
  74. {
  75. uint8_t i, d4 ;
  76. if (lcd->bits == 4)
  77. {
  78. d4 = (data >> 4) & 0x0F;
  79. for (i = 0 ; i < 4 ; ++i)
  80. {
  81. digitalWrite (lcd->dataPins [i], (d4 & 1)) ;
  82. d4 >>= 1 ;
  83. }
  84. strobe (lcd) ;
  85. d4 = data & 0x0F ;
  86. for (i = 0 ; i < 4 ; ++i)
  87. {
  88. digitalWrite (lcd->dataPins [i], (d4 & 1)) ;
  89. d4 >>= 1 ;
  90. }
  91. }
  92. else
  93. {
  94. for (i = 0 ; i < 8 ; ++i)
  95. {
  96. digitalWrite (lcd->dataPins [i], (data & 1)) ;
  97. data >>= 1 ;
  98. }
  99. }
  100. strobe (lcd) ;
  101. }
  102. /*
  103. * putCommand:
  104. * Send a command byte to the display
  105. *********************************************************************************
  106. */
  107. static void putCommand (struct lcdDataStruct *lcd, uint8_t command)
  108. {
  109. digitalWrite (lcd->rsPin, 0) ;
  110. sendDataCmd (lcd, command) ;
  111. }
  112. static void put4Command (struct lcdDataStruct *lcd, uint8_t command)
  113. {
  114. uint8_t i ;
  115. digitalWrite (lcd->rsPin, 0) ;
  116. for (i = 0 ; i < 4 ; ++i)
  117. {
  118. digitalWrite (lcd->dataPins [i], (command & 1)) ;
  119. command >>= 1 ;
  120. }
  121. strobe (lcd) ;
  122. }
  123. /*
  124. *********************************************************************************
  125. * User Code below here
  126. *********************************************************************************
  127. */
  128. /*
  129. * lcdHome: lcdClear:
  130. * Home the cursor or clear the screen.
  131. *********************************************************************************
  132. */
  133. void lcdHome (int fd)
  134. {
  135. struct lcdDataStruct *lcd = lcds [fd] ;
  136. putCommand (lcd, LCD_HOME) ;
  137. }
  138. void lcdClear (int fd)
  139. {
  140. struct lcdDataStruct *lcd = lcds [fd] ;
  141. putCommand (lcd, LCD_CLEAR) ;
  142. }
  143. /*
  144. * lcdPosition:
  145. * Update the position of the cursor on the display
  146. *********************************************************************************
  147. */
  148. void lcdPosition (int fd, int x, int y)
  149. {
  150. static uint8_t rowOff [4] = { 0x00, 0x40, 0x14, 0x54 } ;
  151. struct lcdDataStruct *lcd = lcds [fd] ;
  152. putCommand (lcd, x + (LCD_DGRAM | rowOff [y])) ;
  153. }
  154. /*
  155. * lcdPutchar:
  156. * Send a data byte to be displayed on the display
  157. *********************************************************************************
  158. */
  159. void lcdPutchar (int fd, uint8_t data)
  160. {
  161. struct lcdDataStruct *lcd = lcds [fd] ;
  162. digitalWrite (lcd->rsPin, 1) ;
  163. sendDataCmd (lcd, data) ;
  164. }
  165. /*
  166. * lcdPuts:
  167. * Send a string to be displayed on the display
  168. *********************************************************************************
  169. */
  170. void lcdPuts (int fd, char *string)
  171. {
  172. while (*string)
  173. lcdPutchar (fd, *string++) ;
  174. }
  175. /*
  176. * lcdPrintf:
  177. * Printf to an LCD display
  178. *********************************************************************************
  179. */
  180. void lcdPrintf (int fd, char *message, ...)
  181. {
  182. va_list argp ;
  183. char buffer [1024] ;
  184. va_start (argp, message) ;
  185. vsnprintf (buffer, 1023, message, argp) ;
  186. va_end (argp) ;
  187. lcdPuts (fd, buffer) ;
  188. }
  189. /*
  190. * lcdInit:
  191. * Take a lot of parameters and initialise the LCD, and return a handle to
  192. * that LCD, or -1 if any error.
  193. *********************************************************************************
  194. */
  195. int lcdInit (int rows, int cols, int bits, int rs, int strb,
  196. int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7)
  197. {
  198. static int initialised = 0 ;
  199. uint8_t func ;
  200. int i ;
  201. int lcdFd = -1 ;
  202. struct lcdDataStruct *lcd ;
  203. if (initialised == 0)
  204. {
  205. initialised = 1 ;
  206. for (i = 0 ; i < MAX_LCDS ; ++i)
  207. lcds [i] = NULL ;
  208. }
  209. // Simple sanity checks
  210. if (! ((bits == 4) || (bits == 8)))
  211. return -1 ;
  212. if ((rows < 0) || (rows > 20))
  213. return -1 ;
  214. if ((cols < 0) || (cols > 20))
  215. return -1 ;
  216. // Create a new LCD:
  217. for (i = 0 ; i < MAX_LCDS ; ++i)
  218. {
  219. if (lcds [i] == NULL)
  220. {
  221. lcdFd = i ;
  222. break ;
  223. }
  224. }
  225. if (lcdFd == -1)
  226. return -1 ;
  227. lcd = malloc (sizeof (struct lcdDataStruct)) ;
  228. if (lcd == NULL)
  229. return -1 ;
  230. lcd->rsPin = rs ;
  231. lcd->strbPin = strb ;
  232. lcd->bits = 8 ; // For now - we'll set it properly later.
  233. lcd->rows = rows ;
  234. lcd->cols = cols ;
  235. lcd->dataPins [0] = d0 ;
  236. lcd->dataPins [1] = d1 ;
  237. lcd->dataPins [2] = d2 ;
  238. lcd->dataPins [3] = d3 ;
  239. lcd->dataPins [4] = d4 ;
  240. lcd->dataPins [5] = d5 ;
  241. lcd->dataPins [6] = d6 ;
  242. lcd->dataPins [7] = d7 ;
  243. lcds [lcdFd] = lcd ;
  244. digitalWrite (lcd->rsPin, 0) ; pinMode (lcd->rsPin, OUTPUT) ;
  245. digitalWrite (lcd->strbPin, 0) ; pinMode (lcd->strbPin, OUTPUT) ;
  246. for (i = 0 ; i < bits ; ++i)
  247. {
  248. digitalWrite (lcd->dataPins [i], 0) ;
  249. pinMode (lcd->dataPins [i], OUTPUT) ;
  250. }
  251. delay (35) ; // mS
  252. // 4-bit mode?
  253. // OK. This is a PIG and it's not at all obvious from the documentation I had,
  254. // so I guess some others have worked through either with better documentation
  255. // or more trial and error... Anyway here goes:
  256. //
  257. // It seems that the controller needs to see the FUNC command at least 3 times
  258. // consecutively - in 8-bit mode. If you're only using 8-bit mode, then it appears
  259. // that you can get away with one func-set, however I'd not rely on it...
  260. //
  261. // So to set 4-bit mode, you need to send the commands one nibble at a time,
  262. // the same three times, but send the command to set it into 8-bit mode those
  263. // three times, then send a final 4th command to set it into 4-bit mode, and only
  264. // then can you flip the switch for the rest of the library to work in 4-bit
  265. // mode which sends the commands as 2 x 4-bit values.
  266. if (bits == 4)
  267. {
  268. func = LCD_FUNC | LCD_FUNC_DL ; // Set 8-bit mode 3 times
  269. put4Command (lcd, func >> 4) ; delay (35) ;
  270. put4Command (lcd, func >> 4) ; delay (35) ;
  271. put4Command (lcd, func >> 4) ; delay (35) ;
  272. func = LCD_FUNC ; // 4th set: 4-bit mode
  273. put4Command (lcd, func >> 4) ; delay (35) ;
  274. lcd->bits = 4 ;
  275. }
  276. else
  277. {
  278. func = LCD_FUNC | LCD_FUNC_DL ;
  279. putCommand (lcd, func ) ; delay (35) ;
  280. putCommand (lcd, func ) ; delay (35) ;
  281. putCommand (lcd, func ) ; delay (35) ;
  282. }
  283. if (lcd->rows > 1)
  284. {
  285. func |= LCD_FUNC_N ;
  286. putCommand (lcd, func) ; delay (35) ;
  287. }
  288. // Rest of the initialisation sequence
  289. putCommand (lcd, LCD_ON_OFF | LCD_ON_OFF_D) ; delay (2) ;
  290. putCommand (lcd, LCD_ENTRY | LCD_ENTRY_ID) ; delay (2) ;
  291. putCommand (lcd, LCD_CDSHIFT | LCD_CDSHIFT_RL) ; delay (2) ;
  292. putCommand (lcd, LCD_CLEAR) ; delay (5) ;
  293. return lcdFd ;
  294. }