You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lcd.c 8.9 KiB

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