Peter's keyboard firmware (QMK) https://qmk.fm/
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.

hd44780.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. {
  83. lcd_e_high();
  84. lcd_e_delay();
  85. lcd_e_low();
  86. }
  87. #endif
  88. /*************************************************************************
  89. Low-level function to write byte to LCD controller
  90. Input: data byte to write to LCD
  91. rs 1: write data
  92. 0: write instruction
  93. Returns: none
  94. *************************************************************************/
  95. #if LCD_IO_MODE
  96. static void lcd_write(uint8_t data,uint8_t rs)
  97. {
  98. unsigned char dataBits ;
  99. if (rs) { /* write data (RS=1, RW=0) */
  100. lcd_rs_high();
  101. } else { /* write instruction (RS=0, RW=0) */
  102. lcd_rs_low();
  103. }
  104. lcd_rw_low(); /* RW=0 write mode */
  105. if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )
  106. && (LCD_DATA0_PIN == 0) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3) )
  107. {
  108. /* configure data pins as output */
  109. DDR(LCD_DATA0_PORT) |= 0x0F;
  110. /* output high nibble first */
  111. dataBits = LCD_DATA0_PORT & 0xF0;
  112. LCD_DATA0_PORT = dataBits |((data>>4)&0x0F);
  113. lcd_e_toggle();
  114. /* output low nibble */
  115. LCD_DATA0_PORT = dataBits | (data&0x0F);
  116. lcd_e_toggle();
  117. /* all data pins high (inactive) */
  118. LCD_DATA0_PORT = dataBits | 0x0F;
  119. }
  120. else
  121. {
  122. /* configure data pins as output */
  123. DDR(LCD_DATA0_PORT) |= _BV(LCD_DATA0_PIN);
  124. DDR(LCD_DATA1_PORT) |= _BV(LCD_DATA1_PIN);
  125. DDR(LCD_DATA2_PORT) |= _BV(LCD_DATA2_PIN);
  126. DDR(LCD_DATA3_PORT) |= _BV(LCD_DATA3_PIN);
  127. /* output high nibble first */
  128. LCD_DATA3_PORT &= ~_BV(LCD_DATA3_PIN);
  129. LCD_DATA2_PORT &= ~_BV(LCD_DATA2_PIN);
  130. LCD_DATA1_PORT &= ~_BV(LCD_DATA1_PIN);
  131. LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN);
  132. if(data & 0x80) LCD_DATA3_PORT |= _BV(LCD_DATA3_PIN);
  133. if(data & 0x40) LCD_DATA2_PORT |= _BV(LCD_DATA2_PIN);
  134. if(data & 0x20) LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);
  135. if(data & 0x10) LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN);
  136. lcd_e_toggle();
  137. /* output low nibble */
  138. LCD_DATA3_PORT &= ~_BV(LCD_DATA3_PIN);
  139. LCD_DATA2_PORT &= ~_BV(LCD_DATA2_PIN);
  140. LCD_DATA1_PORT &= ~_BV(LCD_DATA1_PIN);
  141. LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN);
  142. if(data & 0x08) LCD_DATA3_PORT |= _BV(LCD_DATA3_PIN);
  143. if(data & 0x04) LCD_DATA2_PORT |= _BV(LCD_DATA2_PIN);
  144. if(data & 0x02) LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);
  145. if(data & 0x01) LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN);
  146. lcd_e_toggle();
  147. /* all data pins high (inactive) */
  148. LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN);
  149. LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);
  150. LCD_DATA2_PORT |= _BV(LCD_DATA2_PIN);
  151. LCD_DATA3_PORT |= _BV(LCD_DATA3_PIN);
  152. }
  153. }
  154. #else
  155. #define lcd_write(d,rs) if (rs) *(volatile uint8_t*)(LCD_IO_DATA) = d; else *(volatile uint8_t*)(LCD_IO_FUNCTION) = d;
  156. /* rs==0 -> write instruction to LCD_IO_FUNCTION */
  157. /* rs==1 -> write data to LCD_IO_DATA */
  158. #endif
  159. /*************************************************************************
  160. Low-level function to read byte from LCD controller
  161. Input: rs 1: read data
  162. 0: read busy flag / address counter
  163. Returns: byte read from LCD controller
  164. *************************************************************************/
  165. #if LCD_IO_MODE
  166. static uint8_t lcd_read(uint8_t rs)
  167. {
  168. uint8_t data;
  169. if (rs)
  170. lcd_rs_high(); /* RS=1: read data */
  171. else
  172. lcd_rs_low(); /* RS=0: read busy flag */
  173. lcd_rw_high(); /* RW=1 read mode */
  174. if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )
  175. && ( LCD_DATA0_PIN == 0 )&& (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3) )
  176. {
  177. DDR(LCD_DATA0_PORT) &= 0xF0; /* configure data pins as input */
  178. lcd_e_high();
  179. lcd_e_delay();
  180. data = PIN(LCD_DATA0_PORT) << 4; /* read high nibble first */
  181. lcd_e_low();
  182. lcd_e_delay(); /* Enable 500ns low */
  183. lcd_e_high();
  184. lcd_e_delay();
  185. data |= PIN(LCD_DATA0_PORT)&0x0F; /* read low nibble */
  186. lcd_e_low();
  187. }
  188. else
  189. {
  190. /* configure data pins as input */
  191. DDR(LCD_DATA0_PORT) &= ~_BV(LCD_DATA0_PIN);
  192. DDR(LCD_DATA1_PORT) &= ~_BV(LCD_DATA1_PIN);
  193. DDR(LCD_DATA2_PORT) &= ~_BV(LCD_DATA2_PIN);
  194. DDR(LCD_DATA3_PORT) &= ~_BV(LCD_DATA3_PIN);
  195. /* read high nibble first */
  196. lcd_e_high();
  197. lcd_e_delay();
  198. data = 0;
  199. if ( PIN(LCD_DATA0_PORT) & _BV(LCD_DATA0_PIN) ) data |= 0x10;
  200. if ( PIN(LCD_DATA1_PORT) & _BV(LCD_DATA1_PIN) ) data |= 0x20;
  201. if ( PIN(LCD_DATA2_PORT) & _BV(LCD_DATA2_PIN) ) data |= 0x40;
  202. if ( PIN(LCD_DATA3_PORT) & _BV(LCD_DATA3_PIN) ) data |= 0x80;
  203. lcd_e_low();
  204. lcd_e_delay(); /* Enable 500ns low */
  205. /* read low nibble */
  206. lcd_e_high();
  207. lcd_e_delay();
  208. if ( PIN(LCD_DATA0_PORT) & _BV(LCD_DATA0_PIN) ) data |= 0x01;
  209. if ( PIN(LCD_DATA1_PORT) & _BV(LCD_DATA1_PIN) ) data |= 0x02;
  210. if ( PIN(LCD_DATA2_PORT) & _BV(LCD_DATA2_PIN) ) data |= 0x04;
  211. if ( PIN(LCD_DATA3_PORT) & _BV(LCD_DATA3_PIN) ) data |= 0x08;
  212. lcd_e_low();
  213. }
  214. return data;
  215. }
  216. #else
  217. #define lcd_read(rs) (rs) ? *(volatile uint8_t*)(LCD_IO_DATA+LCD_IO_READ) : *(volatile uint8_t*)(LCD_IO_FUNCTION+LCD_IO_READ)
  218. /* rs==0 -> read instruction from LCD_IO_FUNCTION */
  219. /* rs==1 -> read data from LCD_IO_DATA */
  220. #endif
  221. /*************************************************************************
  222. loops while lcd is busy, returns address counter
  223. *************************************************************************/
  224. static uint8_t lcd_waitbusy(void)
  225. {
  226. register uint8_t c;
  227. /* wait until busy flag is cleared */
  228. while ( (c=lcd_read(0)) & (1<<LCD_BUSY)) {}
  229. /* the address counter is updated 4us after the busy flag is cleared */
  230. delay(LCD_DELAY_BUSY_FLAG);
  231. /* now read the address counter */
  232. return (lcd_read(0)); // return address counter
  233. }/* lcd_waitbusy */
  234. /*************************************************************************
  235. Move cursor to the start of next line or to the first line if the cursor
  236. is already on the last line.
  237. *************************************************************************/
  238. static inline void lcd_newline(uint8_t pos)
  239. {
  240. register uint8_t addressCounter;
  241. #if LCD_LINES==1
  242. addressCounter = 0;
  243. #endif
  244. #if LCD_LINES==2
  245. if ( pos < (LCD_START_LINE2) )
  246. addressCounter = LCD_START_LINE2;
  247. else
  248. addressCounter = LCD_START_LINE1;
  249. #endif
  250. #if LCD_LINES==4
  251. #if KS0073_4LINES_MODE
  252. if ( pos < LCD_START_LINE2 )
  253. addressCounter = LCD_START_LINE2;
  254. else if ( (pos >= LCD_START_LINE2) && (pos < LCD_START_LINE3) )
  255. addressCounter = LCD_START_LINE3;
  256. else if ( (pos >= LCD_START_LINE3) && (pos < LCD_START_LINE4) )
  257. addressCounter = LCD_START_LINE4;
  258. else
  259. addressCounter = LCD_START_LINE1;
  260. #else
  261. if ( pos < LCD_START_LINE3 )
  262. addressCounter = LCD_START_LINE2;
  263. else if ( (pos >= LCD_START_LINE2) && (pos < LCD_START_LINE4) )
  264. addressCounter = LCD_START_LINE3;
  265. else if ( (pos >= LCD_START_LINE3) && (pos < LCD_START_LINE2) )
  266. addressCounter = LCD_START_LINE4;
  267. else
  268. addressCounter = LCD_START_LINE1;
  269. #endif
  270. #endif
  271. lcd_command((1<<LCD_DDRAM)+addressCounter);
  272. }/* lcd_newline */
  273. /*
  274. ** PUBLIC FUNCTIONS
  275. */
  276. /*************************************************************************
  277. Send LCD controller instruction command
  278. Input: instruction to send to LCD controller, see HD44780 data sheet
  279. Returns: none
  280. *************************************************************************/
  281. void lcd_command(uint8_t cmd)
  282. {
  283. lcd_waitbusy();
  284. lcd_write(cmd,0);
  285. }
  286. /*************************************************************************
  287. Send data byte to LCD controller
  288. Input: data to send to LCD controller, see HD44780 data sheet
  289. Returns: none
  290. *************************************************************************/
  291. void lcd_data(uint8_t data)
  292. {
  293. lcd_waitbusy();
  294. lcd_write(data,1);
  295. }
  296. /*************************************************************************
  297. Set cursor to specified position
  298. Input: x horizontal position (0: left most position)
  299. y vertical position (0: first line)
  300. Returns: none
  301. *************************************************************************/
  302. void lcd_gotoxy(uint8_t x, uint8_t y)
  303. {
  304. #if LCD_LINES==1
  305. lcd_command((1<<LCD_DDRAM)+LCD_START_LINE1+x);
  306. #endif
  307. #if LCD_LINES==2
  308. if ( y==0 )
  309. lcd_command((1<<LCD_DDRAM)+LCD_START_LINE1+x);
  310. else
  311. lcd_command((1<<LCD_DDRAM)+LCD_START_LINE2+x);
  312. #endif
  313. #if LCD_LINES==4
  314. if ( y==0 )
  315. lcd_command((1<<LCD_DDRAM)+LCD_START_LINE1+x);
  316. else if ( y==1)
  317. lcd_command((1<<LCD_DDRAM)+LCD_START_LINE2+x);
  318. else if ( y==2)
  319. lcd_command((1<<LCD_DDRAM)+LCD_START_LINE3+x);
  320. else /* y==3 */
  321. lcd_command((1<<LCD_DDRAM)+LCD_START_LINE4+x);
  322. #endif
  323. }/* lcd_gotoxy */
  324. /*************************************************************************
  325. *************************************************************************/
  326. int lcd_getxy(void)
  327. {
  328. return lcd_waitbusy();
  329. }
  330. /*************************************************************************
  331. Clear display and set cursor to home position
  332. *************************************************************************/
  333. void lcd_clrscr(void)
  334. {
  335. lcd_command(1<<LCD_CLR);
  336. }
  337. /*************************************************************************
  338. Set cursor to home position
  339. *************************************************************************/
  340. void lcd_home(void)
  341. {
  342. lcd_command(1<<LCD_HOME);
  343. }
  344. /*************************************************************************
  345. Display character at current cursor position
  346. Input: character to be displayed
  347. Returns: none
  348. *************************************************************************/
  349. void lcd_putc(char c)
  350. {
  351. uint8_t pos;
  352. pos = lcd_waitbusy(); // read busy-flag and address counter
  353. if (c=='\n')
  354. {
  355. lcd_newline(pos);
  356. }
  357. else
  358. {
  359. #if LCD_WRAP_LINES==1
  360. #if LCD_LINES==1
  361. if ( pos == LCD_START_LINE1+LCD_DISP_LENGTH ) {
  362. lcd_write((1<<LCD_DDRAM)+LCD_START_LINE1,0);
  363. }
  364. #elif LCD_LINES==2
  365. if ( pos == LCD_START_LINE1+LCD_DISP_LENGTH ) {
  366. lcd_write((1<<LCD_DDRAM)+LCD_START_LINE2,0);
  367. }else if ( pos == LCD_START_LINE2+LCD_DISP_LENGTH ){
  368. lcd_write((1<<LCD_DDRAM)+LCD_START_LINE1,0);
  369. }
  370. #elif LCD_LINES==4
  371. if ( pos == LCD_START_LINE1+LCD_DISP_LENGTH ) {
  372. lcd_write((1<<LCD_DDRAM)+LCD_START_LINE2,0);
  373. }else if ( pos == LCD_START_LINE2+LCD_DISP_LENGTH ) {
  374. lcd_write((1<<LCD_DDRAM)+LCD_START_LINE3,0);
  375. }else if ( pos == LCD_START_LINE3+LCD_DISP_LENGTH ) {
  376. lcd_write((1<<LCD_DDRAM)+LCD_START_LINE4,0);
  377. }else if ( pos == LCD_START_LINE4+LCD_DISP_LENGTH ) {
  378. lcd_write((1<<LCD_DDRAM)+LCD_START_LINE1,0);
  379. }
  380. #endif
  381. lcd_waitbusy();
  382. #endif
  383. lcd_write(c, 1);
  384. }
  385. }/* lcd_putc */
  386. /*************************************************************************
  387. Display string without auto linefeed
  388. Input: string to be displayed
  389. Returns: none
  390. *************************************************************************/
  391. void lcd_puts(const char *s)
  392. /* print string on lcd (no auto linefeed) */
  393. {
  394. register char c;
  395. while ( (c = *s++) ) {
  396. lcd_putc(c);
  397. }
  398. }/* lcd_puts */
  399. /*************************************************************************
  400. Display string from program memory without auto linefeed
  401. Input: string from program memory be be displayed
  402. Returns: none
  403. *************************************************************************/
  404. void lcd_puts_p(const char *progmem_s)
  405. /* print string from program memory on lcd (no auto linefeed) */
  406. {
  407. register char c;
  408. while ( (c = pgm_read_byte(progmem_s++)) ) {
  409. lcd_putc(c);
  410. }
  411. }/* lcd_puts_p */
  412. /*************************************************************************
  413. Initialize display and select type of cursor
  414. Input: dispAttr LCD_DISP_OFF display off
  415. LCD_DISP_ON display on, cursor off
  416. LCD_DISP_ON_CURSOR display on, cursor on
  417. LCD_DISP_CURSOR_BLINK display on, cursor on flashing
  418. Returns: none
  419. *************************************************************************/
  420. void lcd_init(uint8_t dispAttr)
  421. {
  422. #if LCD_IO_MODE
  423. /*
  424. * Initialize LCD to 4 bit I/O mode
  425. */
  426. if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )
  427. && ( &LCD_RS_PORT == &LCD_DATA0_PORT) && ( &LCD_RW_PORT == &LCD_DATA0_PORT) && (&LCD_E_PORT == &LCD_DATA0_PORT)
  428. && (LCD_DATA0_PIN == 0 ) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3)
  429. && (LCD_RS_PIN == 4 ) && (LCD_RW_PIN == 5) && (LCD_E_PIN == 6 ) )
  430. {
  431. /* configure all port bits as output (all LCD lines on same port) */
  432. DDR(LCD_DATA0_PORT) |= 0x7F;
  433. }
  434. else if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )
  435. && (LCD_DATA0_PIN == 0 ) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3) )
  436. {
  437. /* configure all port bits as output (all LCD data lines on same port, but control lines on different ports) */
  438. DDR(LCD_DATA0_PORT) |= 0x0F;
  439. DDR(LCD_RS_PORT) |= _BV(LCD_RS_PIN);
  440. DDR(LCD_RW_PORT) |= _BV(LCD_RW_PIN);
  441. DDR(LCD_E_PORT) |= _BV(LCD_E_PIN);
  442. }
  443. else
  444. {
  445. /* configure all port bits as output (LCD data and control lines on different ports */
  446. DDR(LCD_RS_PORT) |= _BV(LCD_RS_PIN);
  447. DDR(LCD_RW_PORT) |= _BV(LCD_RW_PIN);
  448. DDR(LCD_E_PORT) |= _BV(LCD_E_PIN);
  449. DDR(LCD_DATA0_PORT) |= _BV(LCD_DATA0_PIN);
  450. DDR(LCD_DATA1_PORT) |= _BV(LCD_DATA1_PIN);
  451. DDR(LCD_DATA2_PORT) |= _BV(LCD_DATA2_PIN);
  452. DDR(LCD_DATA3_PORT) |= _BV(LCD_DATA3_PIN);
  453. }
  454. delay(LCD_DELAY_BOOTUP); /* wait 16ms or more after power-on */
  455. /* initial write to lcd is 8bit */
  456. LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN); // LCD_FUNCTION>>4;
  457. LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN); // LCD_FUNCTION_8BIT>>4;
  458. lcd_e_toggle();
  459. delay(LCD_DELAY_INIT); /* delay, busy flag can't be checked here */
  460. /* repeat last command */
  461. lcd_e_toggle();
  462. delay(LCD_DELAY_INIT_REP); /* delay, busy flag can't be checked here */
  463. /* repeat last command a third time */
  464. lcd_e_toggle();
  465. delay(LCD_DELAY_INIT_REP); /* delay, busy flag can't be checked here */
  466. /* now configure for 4bit mode */
  467. LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN); // LCD_FUNCTION_4BIT_1LINE>>4
  468. lcd_e_toggle();
  469. delay(LCD_DELAY_INIT_4BIT); /* some displays need this additional delay */
  470. /* from now the LCD only accepts 4 bit I/O, we can use lcd_command() */
  471. #else
  472. /*
  473. * Initialize LCD to 8 bit memory mapped mode
  474. */
  475. /* enable external SRAM (memory mapped lcd) and one wait state */
  476. MCUCR = _BV(SRE) | _BV(SRW);
  477. /* reset LCD */
  478. delay(LCD_DELAY_BOOTUP); /* wait 16ms after power-on */
  479. lcd_write(LCD_FUNCTION_8BIT_1LINE,0); /* function set: 8bit interface */
  480. delay(LCD_DELAY_INIT); /* wait 5ms */
  481. lcd_write(LCD_FUNCTION_8BIT_1LINE,0); /* function set: 8bit interface */
  482. delay(LCD_DELAY_INIT_REP); /* wait 64us */
  483. lcd_write(LCD_FUNCTION_8BIT_1LINE,0); /* function set: 8bit interface */
  484. delay(LCD_DELAY_INIT_REP); /* wait 64us */
  485. #endif
  486. #if KS0073_4LINES_MODE
  487. /* Display with KS0073 controller requires special commands for enabling 4 line mode */
  488. lcd_command(KS0073_EXTENDED_FUNCTION_REGISTER_ON);
  489. lcd_command(KS0073_4LINES_MODE);
  490. lcd_command(KS0073_EXTENDED_FUNCTION_REGISTER_OFF);
  491. #else
  492. lcd_command(LCD_FUNCTION_DEFAULT); /* function set: display lines */
  493. #endif
  494. lcd_command(LCD_DISP_OFF); /* display off */
  495. lcd_clrscr(); /* display clear */
  496. lcd_command(LCD_MODE_DEFAULT); /* set entry mode */
  497. lcd_command(dispAttr); /* display/cursor control */
  498. }/* lcd_init */