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.

542 lines
20 KiB

  1. /****************************************************************************
  2. Title: HD44780U LCD library
  3. Author: Peter Fleury <pfleury@gmx.ch> http://tinyurl.com/peterfleury
  4. License: GNU General Public License Version 3
  5. File: $Id: lcd.c,v 1.15.2.2 2015/01/17 12:16:05 peter Exp $
  6. Software: AVR-GCC 3.3
  7. Target: any AVR device, memory mapped mode only for AT90S4414/8515/Mega
  8. DESCRIPTION
  9. Basic routines for interfacing a HD44780U-based text lcd display
  10. Originally based on Volker Oth's lcd library,
  11. changed lcd_init(), added additional constants for lcd_command(),
  12. added 4-bit I/O mode, improved and optimized code.
  13. Library can be operated in memory mapped mode (LCD_IO_MODE=0) or in
  14. 4-bit IO port mode (LCD_IO_MODE=1). 8-bit IO port mode not supported.
  15. Memory mapped mode compatible with Kanda STK200, but supports also
  16. generation of R/W signal through A8 address line.
  17. USAGE
  18. See the C include lcd.h file for a description of each function
  19. *****************************************************************************/
  20. #include <inttypes.h>
  21. #include <avr/io.h>
  22. #include <avr/pgmspace.h>
  23. #include <util/delay.h>
  24. #include "hd44780.h"
  25. /*
  26. ** constants/macros
  27. */
  28. #define DDR(x) (*(&x - 1)) /* address of data direction register of port x */
  29. #if defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
  30. /* on ATmega64/128 PINF is on port 0x00 and not 0x60 */
  31. # define PIN(x) (&PORTF == &(x) ? _SFR_IO8(0x00) : (*(&x - 2)))
  32. #else
  33. # define PIN(x) (*(&x - 2)) /* address of input register of port x */
  34. #endif
  35. #if LCD_IO_MODE
  36. # define lcd_e_delay() _delay_us(LCD_DELAY_ENABLE_PULSE)
  37. # define lcd_e_high() LCD_E_PORT |= _BV(LCD_E_PIN);
  38. # define lcd_e_low() LCD_E_PORT &= ~_BV(LCD_E_PIN);
  39. # define lcd_e_toggle() toggle_e()
  40. # define lcd_rw_high() LCD_RW_PORT |= _BV(LCD_RW_PIN)
  41. # define lcd_rw_low() LCD_RW_PORT &= ~_BV(LCD_RW_PIN)
  42. # define lcd_rs_high() LCD_RS_PORT |= _BV(LCD_RS_PIN)
  43. # define lcd_rs_low() LCD_RS_PORT &= ~_BV(LCD_RS_PIN)
  44. #endif
  45. #if LCD_IO_MODE
  46. # if LCD_LINES == 1
  47. # define LCD_FUNCTION_DEFAULT LCD_FUNCTION_4BIT_1LINE
  48. # else
  49. # define LCD_FUNCTION_DEFAULT LCD_FUNCTION_4BIT_2LINES
  50. # endif
  51. #else
  52. # if LCD_LINES == 1
  53. # define LCD_FUNCTION_DEFAULT LCD_FUNCTION_8BIT_1LINE
  54. # else
  55. # define LCD_FUNCTION_DEFAULT LCD_FUNCTION_8BIT_2LINES
  56. # endif
  57. #endif
  58. #if LCD_CONTROLLER_KS0073
  59. # if LCD_LINES == 4
  60. # define KS0073_EXTENDED_FUNCTION_REGISTER_ON 0x2C /* |0|010|1100 4-bit mode, extension-bit RE = 1 */
  61. # define KS0073_EXTENDED_FUNCTION_REGISTER_OFF 0x28 /* |0|010|1000 4-bit mode, extension-bit RE = 0 */
  62. # define KS0073_4LINES_MODE 0x09 /* |0|000|1001 4 lines mode */
  63. # endif
  64. #endif
  65. /*
  66. ** function prototypes
  67. */
  68. #if LCD_IO_MODE
  69. static void toggle_e(void);
  70. #endif
  71. /*
  72. ** local functions
  73. */
  74. /*************************************************************************
  75. delay for a minimum of <us> microseconds
  76. the number of loops is calculated at compile-time from MCU clock frequency
  77. *************************************************************************/
  78. #define delay(us) _delay_us(us)
  79. #if LCD_IO_MODE
  80. /* toggle Enable Pin to initiate write */
  81. static void toggle_e(void) {
  82. lcd_e_high();
  83. lcd_e_delay();
  84. lcd_e_low();
  85. }
  86. #endif
  87. /*************************************************************************
  88. Low-level function to write byte to LCD controller
  89. Input: data byte to write to LCD
  90. rs 1: write data
  91. 0: write instruction
  92. Returns: none
  93. *************************************************************************/
  94. #if LCD_IO_MODE
  95. static void lcd_write(uint8_t data, uint8_t rs) {
  96. unsigned char dataBits;
  97. if (rs) { /* write data (RS=1, RW=0) */
  98. lcd_rs_high();
  99. } else { /* write instruction (RS=0, RW=0) */
  100. lcd_rs_low();
  101. }
  102. lcd_rw_low(); /* RW=0 write mode */
  103. if ((&LCD_DATA0_PORT == &LCD_DATA1_PORT) && (&LCD_DATA1_PORT == &LCD_DATA2_PORT) && (&LCD_DATA2_PORT == &LCD_DATA3_PORT) && (LCD_DATA0_PIN == 0) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3)) {
  104. /* configure data pins as output */
  105. DDR(LCD_DATA0_PORT) |= 0x0F;
  106. /* output high nibble first */
  107. dataBits = LCD_DATA0_PORT & 0xF0;
  108. LCD_DATA0_PORT = dataBits | ((data >> 4) & 0x0F);
  109. lcd_e_toggle();
  110. /* output low nibble */
  111. LCD_DATA0_PORT = dataBits | (data & 0x0F);
  112. lcd_e_toggle();
  113. /* all data pins high (inactive) */
  114. LCD_DATA0_PORT = dataBits | 0x0F;
  115. } else {
  116. /* configure data pins as output */
  117. DDR(LCD_DATA0_PORT) |= _BV(LCD_DATA0_PIN);
  118. DDR(LCD_DATA1_PORT) |= _BV(LCD_DATA1_PIN);
  119. DDR(LCD_DATA2_PORT) |= _BV(LCD_DATA2_PIN);
  120. DDR(LCD_DATA3_PORT) |= _BV(LCD_DATA3_PIN);
  121. /* output high nibble first */
  122. LCD_DATA3_PORT &= ~_BV(LCD_DATA3_PIN);
  123. LCD_DATA2_PORT &= ~_BV(LCD_DATA2_PIN);
  124. LCD_DATA1_PORT &= ~_BV(LCD_DATA1_PIN);
  125. LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN);
  126. if (data & 0x80) LCD_DATA3_PORT |= _BV(LCD_DATA3_PIN);
  127. if (data & 0x40) LCD_DATA2_PORT |= _BV(LCD_DATA2_PIN);
  128. if (data & 0x20) LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);
  129. if (data & 0x10) LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN);
  130. lcd_e_toggle();
  131. /* output low nibble */
  132. LCD_DATA3_PORT &= ~_BV(LCD_DATA3_PIN);
  133. LCD_DATA2_PORT &= ~_BV(LCD_DATA2_PIN);
  134. LCD_DATA1_PORT &= ~_BV(LCD_DATA1_PIN);
  135. LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN);
  136. if (data & 0x08) LCD_DATA3_PORT |= _BV(LCD_DATA3_PIN);
  137. if (data & 0x04) LCD_DATA2_PORT |= _BV(LCD_DATA2_PIN);
  138. if (data & 0x02) LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);
  139. if (data & 0x01) LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN);
  140. lcd_e_toggle();
  141. /* all data pins high (inactive) */
  142. LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN);
  143. LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);
  144. LCD_DATA2_PORT |= _BV(LCD_DATA2_PIN);
  145. LCD_DATA3_PORT |= _BV(LCD_DATA3_PIN);
  146. }
  147. }
  148. #else
  149. # define lcd_write(d, rs) \
  150. if (rs) \
  151. *(volatile uint8_t *)(LCD_IO_DATA) = d; \
  152. else \
  153. *(volatile uint8_t *)(LCD_IO_FUNCTION) = d;
  154. /* rs==0 -> write instruction to LCD_IO_FUNCTION */
  155. /* rs==1 -> write data to LCD_IO_DATA */
  156. #endif
  157. /*************************************************************************
  158. Low-level function to read byte from LCD controller
  159. Input: rs 1: read data
  160. 0: read busy flag / address counter
  161. Returns: byte read from LCD controller
  162. *************************************************************************/
  163. #if LCD_IO_MODE
  164. static uint8_t lcd_read(uint8_t rs) {
  165. uint8_t data;
  166. if (rs)
  167. lcd_rs_high(); /* RS=1: read data */
  168. else
  169. lcd_rs_low(); /* RS=0: read busy flag */
  170. lcd_rw_high(); /* RW=1 read mode */
  171. if ((&LCD_DATA0_PORT == &LCD_DATA1_PORT) && (&LCD_DATA1_PORT == &LCD_DATA2_PORT) && (&LCD_DATA2_PORT == &LCD_DATA3_PORT) && (LCD_DATA0_PIN == 0) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3)) {
  172. DDR(LCD_DATA0_PORT) &= 0xF0; /* configure data pins as input */
  173. lcd_e_high();
  174. lcd_e_delay();
  175. data = PIN(LCD_DATA0_PORT) << 4; /* read high nibble first */
  176. lcd_e_low();
  177. lcd_e_delay(); /* Enable 500ns low */
  178. lcd_e_high();
  179. lcd_e_delay();
  180. data |= PIN(LCD_DATA0_PORT) & 0x0F; /* read low nibble */
  181. lcd_e_low();
  182. } else {
  183. /* configure data pins as input */
  184. DDR(LCD_DATA0_PORT) &= ~_BV(LCD_DATA0_PIN);
  185. DDR(LCD_DATA1_PORT) &= ~_BV(LCD_DATA1_PIN);
  186. DDR(LCD_DATA2_PORT) &= ~_BV(LCD_DATA2_PIN);
  187. DDR(LCD_DATA3_PORT) &= ~_BV(LCD_DATA3_PIN);
  188. /* read high nibble first */
  189. lcd_e_high();
  190. lcd_e_delay();
  191. data = 0;
  192. if (PIN(LCD_DATA0_PORT) & _BV(LCD_DATA0_PIN)) data |= 0x10;
  193. if (PIN(LCD_DATA1_PORT) & _BV(LCD_DATA1_PIN)) data |= 0x20;
  194. if (PIN(LCD_DATA2_PORT) & _BV(LCD_DATA2_PIN)) data |= 0x40;
  195. if (PIN(LCD_DATA3_PORT) & _BV(LCD_DATA3_PIN)) data |= 0x80;
  196. lcd_e_low();
  197. lcd_e_delay(); /* Enable 500ns low */
  198. /* read low nibble */
  199. lcd_e_high();
  200. lcd_e_delay();
  201. if (PIN(LCD_DATA0_PORT) & _BV(LCD_DATA0_PIN)) data |= 0x01;
  202. if (PIN(LCD_DATA1_PORT) & _BV(LCD_DATA1_PIN)) data |= 0x02;
  203. if (PIN(LCD_DATA2_PORT) & _BV(LCD_DATA2_PIN)) data |= 0x04;
  204. if (PIN(LCD_DATA3_PORT) & _BV(LCD_DATA3_PIN)) data |= 0x08;
  205. lcd_e_low();
  206. }
  207. return data;
  208. }
  209. #else
  210. # define lcd_read(rs) (rs) ? *(volatile uint8_t *)(LCD_IO_DATA + LCD_IO_READ) : *(volatile uint8_t *)(LCD_IO_FUNCTION + LCD_IO_READ)
  211. /* rs==0 -> read instruction from LCD_IO_FUNCTION */
  212. /* rs==1 -> read data from LCD_IO_DATA */
  213. #endif
  214. /*************************************************************************
  215. loops while lcd is busy, returns address counter
  216. *************************************************************************/
  217. static uint8_t lcd_waitbusy(void)
  218. {
  219. register uint8_t c;
  220. /* wait until busy flag is cleared */
  221. while ((c = lcd_read(0)) & (1 << LCD_BUSY)) {
  222. }
  223. /* the address counter is updated 4us after the busy flag is cleared */
  224. delay(LCD_DELAY_BUSY_FLAG);
  225. /* now read the address counter */
  226. return (lcd_read(0)); // return address counter
  227. } /* lcd_waitbusy */
  228. /*************************************************************************
  229. Move cursor to the start of next line or to the first line if the cursor
  230. is already on the last line.
  231. *************************************************************************/
  232. static inline void lcd_newline(uint8_t pos) {
  233. register uint8_t addressCounter;
  234. #if LCD_LINES == 1
  235. addressCounter = 0;
  236. #endif
  237. #if LCD_LINES == 2
  238. if (pos < (LCD_START_LINE2))
  239. addressCounter = LCD_START_LINE2;
  240. else
  241. addressCounter = LCD_START_LINE1;
  242. #endif
  243. #if LCD_LINES == 4
  244. # if KS0073_4LINES_MODE
  245. if (pos < LCD_START_LINE2)
  246. addressCounter = LCD_START_LINE2;
  247. else if ((pos >= LCD_START_LINE2) && (pos < LCD_START_LINE3))
  248. addressCounter = LCD_START_LINE3;
  249. else if ((pos >= LCD_START_LINE3) && (pos < LCD_START_LINE4))
  250. addressCounter = LCD_START_LINE4;
  251. else
  252. addressCounter = LCD_START_LINE1;
  253. # else
  254. if (pos < LCD_START_LINE3)
  255. addressCounter = LCD_START_LINE2;
  256. else if ((pos >= LCD_START_LINE2) && (pos < LCD_START_LINE4))
  257. addressCounter = LCD_START_LINE3;
  258. else if ((pos >= LCD_START_LINE3) && (pos < LCD_START_LINE2))
  259. addressCounter = LCD_START_LINE4;
  260. else
  261. addressCounter = LCD_START_LINE1;
  262. # endif
  263. #endif
  264. lcd_command((1 << LCD_DDRAM) + addressCounter);
  265. } /* lcd_newline */
  266. /*
  267. ** PUBLIC FUNCTIONS
  268. */
  269. /*************************************************************************
  270. Send LCD controller instruction command
  271. Input: instruction to send to LCD controller, see HD44780 data sheet
  272. Returns: none
  273. *************************************************************************/
  274. void lcd_command(uint8_t cmd) {
  275. lcd_waitbusy();
  276. lcd_write(cmd, 0);
  277. }
  278. /*************************************************************************
  279. Send data byte to LCD controller
  280. Input: data to send to LCD controller, see HD44780 data sheet
  281. Returns: none
  282. *************************************************************************/
  283. void lcd_data(uint8_t data) {
  284. lcd_waitbusy();
  285. lcd_write(data, 1);
  286. }
  287. /*************************************************************************
  288. Set cursor to specified position
  289. Input: x horizontal position (0: left most position)
  290. y vertical position (0: first line)
  291. Returns: none
  292. *************************************************************************/
  293. void lcd_gotoxy(uint8_t x, uint8_t y) {
  294. #if LCD_LINES == 1
  295. lcd_command((1 << LCD_DDRAM) + LCD_START_LINE1 + x);
  296. #endif
  297. #if LCD_LINES == 2
  298. if (y == 0)
  299. lcd_command((1 << LCD_DDRAM) + LCD_START_LINE1 + x);
  300. else
  301. lcd_command((1 << LCD_DDRAM) + LCD_START_LINE2 + x);
  302. #endif
  303. #if LCD_LINES == 4
  304. if (y == 0)
  305. lcd_command((1 << LCD_DDRAM) + LCD_START_LINE1 + x);
  306. else if (y == 1)
  307. lcd_command((1 << LCD_DDRAM) + LCD_START_LINE2 + x);
  308. else if (y == 2)
  309. lcd_command((1 << LCD_DDRAM) + LCD_START_LINE3 + x);
  310. else /* y==3 */
  311. lcd_command((1 << LCD_DDRAM) + LCD_START_LINE4 + x);
  312. #endif
  313. } /* lcd_gotoxy */
  314. /*************************************************************************
  315. *************************************************************************/
  316. int lcd_getxy(void) {
  317. return lcd_waitbusy();
  318. }
  319. /*************************************************************************
  320. Clear display and set cursor to home position
  321. *************************************************************************/
  322. void lcd_clrscr(void) {
  323. lcd_command(1 << LCD_CLR);
  324. }
  325. /*************************************************************************
  326. Set cursor to home position
  327. *************************************************************************/
  328. void lcd_home(void) {
  329. lcd_command(1 << LCD_HOME);
  330. }
  331. /*************************************************************************
  332. Display character at current cursor position
  333. Input: character to be displayed
  334. Returns: none
  335. *************************************************************************/
  336. void lcd_putc(char c) {
  337. uint8_t pos;
  338. pos = lcd_waitbusy(); // read busy-flag and address counter
  339. if (c == '\n') {
  340. lcd_newline(pos);
  341. } else {
  342. #if LCD_WRAP_LINES == 1
  343. # if LCD_LINES == 1
  344. if (pos == LCD_START_LINE1 + LCD_DISP_LENGTH) {
  345. lcd_write((1 << LCD_DDRAM) + LCD_START_LINE1, 0);
  346. }
  347. # elif LCD_LINES == 2
  348. if (pos == LCD_START_LINE1 + LCD_DISP_LENGTH) {
  349. lcd_write((1 << LCD_DDRAM) + LCD_START_LINE2, 0);
  350. } else if (pos == LCD_START_LINE2 + LCD_DISP_LENGTH) {
  351. lcd_write((1 << LCD_DDRAM) + LCD_START_LINE1, 0);
  352. }
  353. # elif LCD_LINES == 4
  354. if (pos == LCD_START_LINE1 + LCD_DISP_LENGTH) {
  355. lcd_write((1 << LCD_DDRAM) + LCD_START_LINE2, 0);
  356. } else if (pos == LCD_START_LINE2 + LCD_DISP_LENGTH) {
  357. lcd_write((1 << LCD_DDRAM) + LCD_START_LINE3, 0);
  358. } else if (pos == LCD_START_LINE3 + LCD_DISP_LENGTH) {
  359. lcd_write((1 << LCD_DDRAM) + LCD_START_LINE4, 0);
  360. } else if (pos == LCD_START_LINE4 + LCD_DISP_LENGTH) {
  361. lcd_write((1 << LCD_DDRAM) + LCD_START_LINE1, 0);
  362. }
  363. # endif
  364. lcd_waitbusy();
  365. #endif
  366. lcd_write(c, 1);
  367. }
  368. } /* lcd_putc */
  369. /*************************************************************************
  370. Display string without auto linefeed
  371. Input: string to be displayed
  372. Returns: none
  373. *************************************************************************/
  374. void lcd_puts(const char *s)
  375. /* print string on lcd (no auto linefeed) */
  376. {
  377. register char c;
  378. while ((c = *s++)) {
  379. lcd_putc(c);
  380. }
  381. } /* lcd_puts */
  382. /*************************************************************************
  383. Display string from program memory without auto linefeed
  384. Input: string from program memory be be displayed
  385. Returns: none
  386. *************************************************************************/
  387. void lcd_puts_p(const char *progmem_s)
  388. /* print string from program memory on lcd (no auto linefeed) */
  389. {
  390. register char c;
  391. while ((c = pgm_read_byte(progmem_s++))) {
  392. lcd_putc(c);
  393. }
  394. } /* lcd_puts_p */
  395. /*************************************************************************
  396. Initialize display and select type of cursor
  397. Input: dispAttr LCD_DISP_OFF display off
  398. LCD_DISP_ON display on, cursor off
  399. LCD_DISP_ON_CURSOR display on, cursor on
  400. LCD_DISP_CURSOR_BLINK display on, cursor on flashing
  401. Returns: none
  402. *************************************************************************/
  403. void lcd_init(uint8_t dispAttr) {
  404. #if LCD_IO_MODE
  405. /*
  406. * Initialize LCD to 4 bit I/O mode
  407. */
  408. if ((&LCD_DATA0_PORT == &LCD_DATA1_PORT) && (&LCD_DATA1_PORT == &LCD_DATA2_PORT) && (&LCD_DATA2_PORT == &LCD_DATA3_PORT) && (&LCD_RS_PORT == &LCD_DATA0_PORT) && (&LCD_RW_PORT == &LCD_DATA0_PORT) && (&LCD_E_PORT == &LCD_DATA0_PORT) && (LCD_DATA0_PIN == 0) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3) && (LCD_RS_PIN == 4) && (LCD_RW_PIN == 5) && (LCD_E_PIN == 6)) {
  409. /* configure all port bits as output (all LCD lines on same port) */
  410. DDR(LCD_DATA0_PORT) |= 0x7F;
  411. } else if ((&LCD_DATA0_PORT == &LCD_DATA1_PORT) && (&LCD_DATA1_PORT == &LCD_DATA2_PORT) && (&LCD_DATA2_PORT == &LCD_DATA3_PORT) && (LCD_DATA0_PIN == 0) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3)) {
  412. /* configure all port bits as output (all LCD data lines on same port, but control lines on different ports) */
  413. DDR(LCD_DATA0_PORT) |= 0x0F;
  414. DDR(LCD_RS_PORT) |= _BV(LCD_RS_PIN);
  415. DDR(LCD_RW_PORT) |= _BV(LCD_RW_PIN);
  416. DDR(LCD_E_PORT) |= _BV(LCD_E_PIN);
  417. } else {
  418. /* configure all port bits as output (LCD data and control lines on different ports */
  419. DDR(LCD_RS_PORT) |= _BV(LCD_RS_PIN);
  420. DDR(LCD_RW_PORT) |= _BV(LCD_RW_PIN);
  421. DDR(LCD_E_PORT) |= _BV(LCD_E_PIN);
  422. DDR(LCD_DATA0_PORT) |= _BV(LCD_DATA0_PIN);
  423. DDR(LCD_DATA1_PORT) |= _BV(LCD_DATA1_PIN);
  424. DDR(LCD_DATA2_PORT) |= _BV(LCD_DATA2_PIN);
  425. DDR(LCD_DATA3_PORT) |= _BV(LCD_DATA3_PIN);
  426. }
  427. delay(LCD_DELAY_BOOTUP); /* wait 16ms or more after power-on */
  428. /* initial write to lcd is 8bit */
  429. LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN); // LCD_FUNCTION>>4;
  430. LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN); // LCD_FUNCTION_8BIT>>4;
  431. lcd_e_toggle();
  432. delay(LCD_DELAY_INIT); /* delay, busy flag can't be checked here */
  433. /* repeat last command */
  434. lcd_e_toggle();
  435. delay(LCD_DELAY_INIT_REP); /* delay, busy flag can't be checked here */
  436. /* repeat last command a third time */
  437. lcd_e_toggle();
  438. delay(LCD_DELAY_INIT_REP); /* delay, busy flag can't be checked here */
  439. /* now configure for 4bit mode */
  440. LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN); // LCD_FUNCTION_4BIT_1LINE>>4
  441. lcd_e_toggle();
  442. delay(LCD_DELAY_INIT_4BIT); /* some displays need this additional delay */
  443. /* from now the LCD only accepts 4 bit I/O, we can use lcd_command() */
  444. #else
  445. /*
  446. * Initialize LCD to 8 bit memory mapped mode
  447. */
  448. /* enable external SRAM (memory mapped lcd) and one wait state */
  449. MCUCR = _BV(SRE) | _BV(SRW);
  450. /* reset LCD */
  451. delay(LCD_DELAY_BOOTUP); /* wait 16ms after power-on */
  452. lcd_write(LCD_FUNCTION_8BIT_1LINE, 0); /* function set: 8bit interface */
  453. delay(LCD_DELAY_INIT); /* wait 5ms */
  454. lcd_write(LCD_FUNCTION_8BIT_1LINE, 0); /* function set: 8bit interface */
  455. delay(LCD_DELAY_INIT_REP); /* wait 64us */
  456. lcd_write(LCD_FUNCTION_8BIT_1LINE, 0); /* function set: 8bit interface */
  457. delay(LCD_DELAY_INIT_REP); /* wait 64us */
  458. #endif
  459. #if KS0073_4LINES_MODE
  460. /* Display with KS0073 controller requires special commands for enabling 4 line mode */
  461. lcd_command(KS0073_EXTENDED_FUNCTION_REGISTER_ON);
  462. lcd_command(KS0073_4LINES_MODE);
  463. lcd_command(KS0073_EXTENDED_FUNCTION_REGISTER_OFF);
  464. #else
  465. lcd_command(LCD_FUNCTION_DEFAULT); /* function set: display lines */
  466. #endif
  467. lcd_command(LCD_DISP_OFF); /* display off */
  468. lcd_clrscr(); /* display clear */
  469. lcd_command(LCD_MODE_DEFAULT); /* set entry mode */
  470. lcd_command(dispAttr); /* display/cursor control */
  471. } /* lcd_init */