Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

387 řádky
8.2 KiB

  1. /**
  2. * ---------------------------------------------------------------+
  3. * @desc HD44780 LCD Driver
  4. * ---------------------------------------------------------------+
  5. * Copyright (C) 2020 Marian Hrinko.
  6. * Written by Marian Hrinko (mato.hrinko@gmail.com)
  7. *
  8. * @author Marian Hrinko
  9. * @datum 18.11.2020
  10. * @file hd44780.c
  11. * @tested AVR Atmega16a
  12. *
  13. * @depend
  14. * ---------------------------------------------------------------+
  15. * @usage default set 16x2 LCD
  16. * 4-bit with 3 control wires (RW, RS, E)
  17. *
  18. */
  19. #ifndef __HD44780_H__
  20. #define __HD44780_H__
  21. // Success
  22. #ifndef SUCCESS
  23. #define SUCCESS 0
  24. #endif
  25. // Error
  26. #ifndef ERROR
  27. #define ERROR 1
  28. #endif
  29. // define clock
  30. #if defined(__AVR_ATmega8__)
  31. #define _FCPU 8000000
  32. #elif defined(__AVR_ATmega16__)
  33. #define _FCPU 16000000
  34. #endif
  35. //#if defined(__AVR_ATmega16__)
  36. // E port
  37. // --------------------------------------
  38. #ifndef HD44780_DDR_E
  39. #define HD44780_DDR_E TRISB
  40. #endif
  41. #ifndef HD44780_PORT_E
  42. #define HD44780_PORT_E PORTB
  43. #endif
  44. #ifndef HD44780_E
  45. #define HD44780_E 12
  46. #endif
  47. // RW port
  48. // --------------------------------------
  49. #ifndef HD44780_DDR_RW
  50. #define HD44780_DDR_RW TRISB
  51. #endif
  52. #ifndef HD44780_PORT_RW
  53. #define HD44780_PORT_RW PORTB
  54. #endif
  55. #ifndef HD44780_RW
  56. #define HD44780_RW 13
  57. #endif
  58. // RS port
  59. // --------------------------------------
  60. #ifndef HD44780_DDR_RS
  61. #define HD44780_DDR_RS TRISD
  62. #endif
  63. #ifndef HD44780_PORT_RS
  64. #define HD44780_PORT_RS PORTD
  65. #endif
  66. #ifndef HD44780_RS
  67. #define HD44780_RS 5
  68. #endif
  69. // DATA port / pin
  70. // --------------------------------------
  71. #ifndef HD44780_DDR_DATA
  72. #define HD44780_DDR_DATA TRISB
  73. #endif
  74. #ifndef HD44780_PORT_DATA
  75. #define HD44780_PORT_DATA PORTB
  76. #endif
  77. #ifndef HD44780_PIN_DATA
  78. #define HD44780_PIN_DATA PORTB
  79. #endif
  80. // pins
  81. #ifndef HD44780_DATA7
  82. #define HD44780_DATA7 PORTBbits.RB11 // LCD PORT DB7
  83. #endif
  84. #ifndef HD44780_DATA6
  85. #define HD44780_DATA6 10 // LCD PORT DB6
  86. #endif
  87. #ifndef HD44780_DATA5
  88. #define HD44780_DATA5 9 // LCD PORT DB5
  89. #endif
  90. #ifndef HD44780_DATA4
  91. #define HD44780_DATA4 3 // LCD PORT DB4
  92. #endif
  93. #ifndef HD44780_DATA3
  94. #define HD44780_DATA3 0 // LCD PORT DB3
  95. #endif
  96. #ifndef HD44780_DATA2
  97. #define HD44780_DATA2 0 // LCD PORT DB2
  98. #endif
  99. #ifndef HD44780_DATA1
  100. #define HD44780_DATA1 0 // LCD PORT DB1
  101. #endif
  102. #ifndef HD44780_DATA0
  103. #define HD44780_DATA0 0 // LCD PORT DB0
  104. #endif
  105. // #endif
  106. #define _delay_ms(x) Sleep(x)
  107. #define _delay_us(x) Sleep(1)
  108. #define BIT7 0x80
  109. #define BIT6 0x40
  110. #define BIT5 0x20
  111. #define BIT4 0x10
  112. #define BIT3 0x08
  113. #define BIT2 0x04
  114. #define BIT1 0x02
  115. #define BIT0 0x01
  116. #define HD44780_BUSY_FLAG HD44780_DATA7
  117. #define HD44780_INIT_SEQ 0x30
  118. #define HD44780_DISP_CLEAR 0x01
  119. #define HD44780_DISP_OFF 0x08
  120. #define HD44780_DISP_ON 0x0C
  121. #define HD44780_CURSOR_ON 0x0E
  122. #define HD44780_CURSOR_OFF 0x0C
  123. #define HD44780_CURSOR_BLINK 0x0F
  124. #define HD44780_RETURN_HOME 0x02
  125. #define HD44780_ENTRY_MODE 0x06
  126. #define HD44780_4BIT_MODE 0x20
  127. #define HD44780_8BIT_MODE 0x30
  128. #define HD44780_2_ROWS 0x08
  129. #define HD44780_FONT_5x8 0x00
  130. #define HD44780_FONT_5x10 0x04
  131. #define HD44780_POSITION 0x80
  132. #define HD44780_SHIFT 0x10
  133. #define HD44780_CURSOR 0x00
  134. #define HD44780_DISPLAY 0x08
  135. #define HD44780_LEFT 0x00
  136. #define HD44780_RIGHT 0x04
  137. #define HD44780_ROWS 2
  138. #define HD44780_COLS 16
  139. #define HD44780_ROW1_START 0x00
  140. #define HD44780_ROW1_END HD44780_COLS
  141. #define HD44780_ROW2_START 0x40
  142. #define HD44780_ROW2_END HD44780_COLS
  143. // **********************************************
  144. // !!!
  145. // MODE DEFINITION - CORRECTLY DEFINED
  146. //
  147. // ----------------------------------------------
  148. //
  149. // HD44780_4BIT_MODE - 4 bit mode / 4 data wires
  150. // HD44780_8BIT_MODE - 8 bit mode / 8 data wires
  151. //
  152. // **********************************************
  153. #define HD44780_MODE HD44780_4BIT_MODE
  154. // set bit
  155. #define SETBIT(REG, BIT) { REG |= (1 << BIT); }
  156. // clear bit
  157. #define CLRBIT(REG, BIT) { REG &= ~(1 << BIT); }
  158. // set port / pin if bit is set
  159. #define SET_IF_BIT_IS_SET(REG, PORT, DATA, BIT) { if((DATA & BIT) > 0) { SETBIT(REG, PORT); } }
  160. /**
  161. * @desc LCD init - initialisation routine
  162. *
  163. * @param void
  164. *
  165. * @return void
  166. */
  167. void HD44780_Init (void);
  168. /**
  169. * @desc LCD display clear
  170. *
  171. * @param void
  172. *
  173. * @return void
  174. */
  175. void HD44780_DisplayClear (void);
  176. /**
  177. * @desc LCD display on
  178. *
  179. * @param void
  180. *
  181. * @return void
  182. */
  183. void HD44780_DisplayOn (void);
  184. /**
  185. * @desc LCD cursor on, display on
  186. *
  187. * @param void
  188. *
  189. * @return void
  190. */
  191. void HD44780_CursorOn (void);
  192. /**
  193. * @desc LCD cursor off
  194. *
  195. * @param void
  196. *
  197. * @return void
  198. */
  199. void HD44780_CursorOff (void);
  200. /**
  201. * @desc LCD cursor blink, cursor on, display on
  202. *
  203. * @param void
  204. *
  205. * @return void
  206. */
  207. void HD44780_CursorBlink (void);
  208. /**
  209. * @desc LCD draw char
  210. *
  211. * @param char
  212. * @return void
  213. */
  214. void HD44780_DrawChar (char character);
  215. /**
  216. * @desc LCD draw string
  217. *
  218. * @param char *
  219. *
  220. * @return void
  221. */
  222. void HD44780_DrawString (char *str);
  223. /**
  224. * @desc Got to position x,y
  225. *
  226. * @param char
  227. * @param char
  228. *
  229. * @return char
  230. */
  231. char HD44780_PositionXY (char x, char y);
  232. /**
  233. * @desc Shift cursor / display to left / right
  234. *
  235. * @param char item {HD44780_CURSOR; HD44780_DISPLAY}
  236. * @param char direction {HD44780_RIGHT; HD44780_LEFT}
  237. *
  238. * @return char
  239. */
  240. char HD44780_Shift (char item, char direction);
  241. /**
  242. * @desc Check Busy Flag (BF) in 8 bit mode
  243. *
  244. * @param void
  245. *
  246. * @return void
  247. */
  248. void HD44780_CheckBFin8bitMode (void);
  249. /**
  250. * @desc Check Busy Flag (BF) in 4 bit mode
  251. *
  252. * @param void
  253. *
  254. * @return void
  255. */
  256. void HD44780_CheckBFin4bitMode (void);
  257. /**
  258. * @desc LCD send instruction
  259. *
  260. * @param unsigned short int
  261. *
  262. * @return void
  263. */
  264. void HD44780_SendInstruction (unsigned short int);
  265. /**
  266. * @desc LCD send data
  267. *
  268. * @param unsigned short int
  269. *
  270. * @return void
  271. */
  272. void HD44780_SendData (unsigned short int);
  273. /**
  274. * @desc LCD send 4bits instruction in 4 bit mode
  275. *
  276. * @param unsigned short int
  277. *
  278. * @return void
  279. */
  280. void HD44780_Send4bitsIn4bitMode (unsigned short int);
  281. /**
  282. * @desc LCD send 8bits instruction in 4 bit mode
  283. *
  284. * @param unsigned short int
  285. *
  286. * @return void
  287. */
  288. void HD44780_Send8bitsIn4bitMode (unsigned short int);
  289. /**
  290. * @desc LCD send 8bits instruction in 8 bit mode
  291. *
  292. * @param unsigned short int
  293. *
  294. * @return void
  295. */
  296. void HD44780_Send8bitsIn8bitMode (unsigned short int);
  297. /**
  298. * @desc LCD send upper nibble
  299. *
  300. * @param unsigned short int
  301. *
  302. * @return void
  303. */
  304. void HD44780_SetUppNibble (unsigned short int);
  305. /**
  306. * @desc LCD send lower nibble
  307. *
  308. * @param unsigned short int
  309. *
  310. * @return void
  311. */
  312. void HD44780_SetLowNibble (unsigned short int);
  313. /**
  314. * @desc LCD pulse E
  315. *
  316. * @param void
  317. *
  318. * @return void
  319. */
  320. void HD44780_PulseE (void);
  321. /**
  322. * @desc Set PORT DB4 to DB7
  323. *
  324. * @param void
  325. *
  326. * @return void
  327. */
  328. void HD44780_SetPORT_DATA4to7 (void);
  329. /**
  330. * @desc Set DDR DB4 to DB7
  331. *
  332. * @param void
  333. *
  334. * @return void
  335. */
  336. void HD44780_SetDDR_DATA4to7 (void);
  337. /**
  338. * @desc Clear DDR DB4 to DB7
  339. *
  340. * @param void
  341. *
  342. * @return void
  343. */
  344. void HD44780_ClearDDR_DATA4to7 (void);
  345. #endif