daily pastebin goal
9%
SHARE
TWEET

xlcd.c

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