Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

369 рядки
8.6 KiB

  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. * lcdPosition:
  146. * Update the position of the cursor on the display
  147. *********************************************************************************
  148. */
  149. void lcdPosition (int fd, int x, int y)
  150. {
  151. static uint8_t rowOff [4] = { 0x00, 0x40, 0x14, 0x54 } ;
  152. struct lcdDataStruct *lcd = lcds [fd] ;
  153. putCommand (lcd, x + (LCD_DGRAM | rowOff [y])) ;
  154. }
  155. /*
  156. * lcdPutchar:
  157. * Send a data byte to be displayed on the display
  158. *********************************************************************************
  159. */
  160. void lcdPutchar (int fd, uint8_t data)
  161. {
  162. struct lcdDataStruct *lcd = lcds [fd] ;
  163. digitalWrite (lcd->rsPin, 1) ;
  164. sendDataCmd (lcd, data) ;
  165. }
  166. /*
  167. * lcdPuts:
  168. * Send a string to be displayed on the display
  169. *********************************************************************************
  170. */
  171. void lcdPuts (int fd, char *string)
  172. {
  173. while (*string)
  174. lcdPutchar (fd, *string++) ;
  175. }
  176. /*
  177. * lcdPrintf:
  178. * Printf to an LCD display
  179. *********************************************************************************
  180. */
  181. void lcdPrintf (int fd, char *message, ...)
  182. {
  183. va_list argp ;
  184. char buffer [1024] ;
  185. va_start (argp, message) ;
  186. vsnprintf (buffer, 1023, message, argp) ;
  187. va_end (argp) ;
  188. lcdPuts (fd, buffer) ;
  189. }
  190. /*
  191. * lcdInit:
  192. * Take a lot of parameters and initialise the LCD, and return a handle to
  193. * that LCD, or -1 if any error.
  194. *********************************************************************************
  195. */
  196. int lcdInit (int rows, int cols, int bits, int rs, int strb,
  197. int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7)
  198. {
  199. static int initialised = 0 ;
  200. uint8_t func ;
  201. int i ;
  202. int lcdFd = -1 ;
  203. struct lcdDataStruct *lcd ;
  204. if (initialised == 0)
  205. {
  206. initialised = 1 ;
  207. for (i = 0 ; i < MAX_LCDS ; ++i)
  208. lcds [i] = NULL ;
  209. }
  210. // Simple sanity checks
  211. if (! ((bits == 4) || (bits == 8)))
  212. return -1 ;
  213. if ((rows < 0) || (rows > 20))
  214. return -1 ;
  215. if ((cols < 0) || (cols > 20))
  216. return -1 ;
  217. // Create a new LCD:
  218. for (i = 0 ; i < MAX_LCDS ; ++i)
  219. {
  220. if (lcds [i] == NULL)
  221. {
  222. lcdFd = i ;
  223. break ;
  224. }
  225. }
  226. if (lcdFd == -1)
  227. return -1 ;
  228. lcd = malloc (sizeof (struct lcdDataStruct)) ;
  229. if (lcd == NULL)
  230. return -1 ;
  231. lcd->rsPin = rs ;
  232. lcd->strbPin = strb ;
  233. lcd->bits = 8 ; // For now - we'll set it properly later.
  234. lcd->rows = rows ;
  235. lcd->cols = cols ;
  236. lcd->dataPins [0] = d0 ;
  237. lcd->dataPins [1] = d1 ;
  238. lcd->dataPins [2] = d2 ;
  239. lcd->dataPins [3] = d3 ;
  240. lcd->dataPins [4] = d4 ;
  241. lcd->dataPins [5] = d5 ;
  242. lcd->dataPins [6] = d6 ;
  243. lcd->dataPins [7] = d7 ;
  244. lcds [lcdFd] = lcd ;
  245. digitalWrite (lcd->rsPin, 0) ; pinMode (lcd->rsPin, OUTPUT) ;
  246. digitalWrite (lcd->strbPin, 0) ; pinMode (lcd->strbPin, OUTPUT) ;
  247. for (i = 0 ; i < bits ; ++i)
  248. {
  249. digitalWrite (lcd->dataPins [i], 0) ;
  250. pinMode (lcd->dataPins [i], OUTPUT) ;
  251. }
  252. delay (35) ; // mS
  253. // 4-bit mode?
  254. // OK. This is a PIG and it's not at all obvious from the documentation I had,
  255. // so I guess some others have worked through either with better documentation
  256. // or more trial and error... Anyway here goes:
  257. //
  258. // It seems that the controller needs to see the FUNC command at least 3 times
  259. // consecutively - in 8-bit mode. If you're only using 8-bit mode, then it appears
  260. // that you can get away with one func-set, however I'd not rely on it...
  261. //
  262. // So to set 4-bit mode, you need to send the commands one nibble at a time,
  263. // the same three times, but send the command to set it into 8-bit mode those
  264. // three times, then send a final 4th command to set it into 4-bit mode, and only
  265. // then can you flip the switch for the rest of the library to work in 4-bit
  266. // mode which sends the commands as 2 x 4-bit values.
  267. if (bits == 4)
  268. {
  269. func = LCD_FUNC | LCD_FUNC_DL ; // Set 8-bit mode 3 times
  270. put4Command (lcd, func >> 4) ; delay (35) ;
  271. put4Command (lcd, func >> 4) ; delay (35) ;
  272. put4Command (lcd, func >> 4) ; delay (35) ;
  273. func = LCD_FUNC ; // 4th set: 4-bit mode
  274. put4Command (lcd, func >> 4) ; delay (35) ;
  275. lcd->bits = 4 ;
  276. }
  277. else
  278. {
  279. func = LCD_FUNC | LCD_FUNC_DL ;
  280. putCommand (lcd, func ) ; delay (35) ;
  281. putCommand (lcd, func ) ; delay (35) ;
  282. putCommand (lcd, func ) ; delay (35) ;
  283. }
  284. if (lcd->rows > 1)
  285. {
  286. func |= LCD_FUNC_N ;
  287. putCommand (lcd, func) ; delay (35) ;
  288. }
  289. // Rest of the initialisation sequence
  290. putCommand (lcd, LCD_ON_OFF | LCD_ON_OFF_D) ; delay (2) ;
  291. putCommand (lcd, LCD_ENTRY | LCD_ENTRY_ID) ; delay (2) ;
  292. putCommand (lcd, LCD_CDSHIFT | LCD_CDSHIFT_RL) ; delay (2) ;
  293. putCommand (lcd, LCD_CLEAR) ; delay (5) ;
  294. return lcdFd ;
  295. }