/****************************************************************************/
/**
* \file lcd.c
*
* \brief 4 Bit Interface to LCD displays with up to 4 rows and
*        a single Controller (e.g. ST7066U or HD44780).
*
* \author   Michael Skutek
* \date	    11.09.2011
* \version  0.9
*
* Platform:    AVR ATmega (tested on ATmega16)
*
* Include Files:
*       - avr/io.h:             port accessing (part of avr-gcc)
*       - util/delay.h:         time delays (part of avr-gcc)
*       - port_definitions.h:   definition of output ports
*       - lcd.h:                definitions and display setup
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*
****************************************************************************/




/******************************************
	\internal
	Include files
*******************************************/

#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>

#include "port_definitions.h"
#include "lcd.h"





/******************************************
	\internal
	Global variables
*******************************************/

uint8_t next_ddram_address;                               //!< contains next ddram address
uint8_t lcd_backlight_state;                              //!< contains the current backlight state





/****************************************************************************/
/**
* This function sets or alternates the backlight (pin) of the LCD. \n
*
*
* \param [in] control     sets the backlight, possible values are:
*                           - LCD_BL_SWITCH_OFF: switches the backlight off
*                           - LCD_BL_SWITCH_ON: switches the backlight on
*                           - LCD_BL_ALTERNATE: alternates the backlight
*
*
* \date
* - Created 05.08.2011
* - Modified  05.08.2011
*
* \author
*   Michael Skutek, michael@skutek.de
*/
/****************************************************************************/

void lcd_set_backlight(uint8_t control) {


  switch (control) {

  case LCD_BL_SWITCH_OFF:
    LCDBL_PORT = LCDBL_PORT & ~(1 << LCDBL_CTRL);    //switch off backlight
    lcd_backlight_state = LCD_BL_OFF;
    break;

  case LCD_BL_SWITCH_ON:
    LCDBL_PORT = LCDBL_PORT | (1 << LCDBL_CTRL);     //switch on backlight
    lcd_backlight_state = LCD_BL_ON;
    break;

  case LCD_BL_ALTERNATE:

    if (lcd_backlight_state == LCD_BL_OFF) {
      LCDBL_PORT = LCDBL_PORT | (1 << LCDBL_CTRL);   //switch on backlight
      lcd_backlight_state = LCD_BL_ON;
    } else {
      LCDBL_PORT = LCDBL_PORT & ~(1 << LCDBL_CTRL);  //switch off backlight
      lcd_backlight_state = LCD_BL_OFF;
    }
    break;

    //unknown control value --> switch on the backlight
  default:
    LCDBL_PORT = LCDBL_PORT | (1 << LCDBL_CTRL);
    lcd_backlight_state = LCD_BL_ON;

  }

}


/****************************************************************************/
/**
* This function creates the enable signal for the LCD. \n
* This is an internal subfunction and no part of the user
* interface. \n
*
*
* \date
* - Created 26.07.2009
* - Modified  26.07.2009
*
* \author
*   Michael Skutek, michael@skutek.de
*/
/****************************************************************************/

void lcd_enable(void) {

  //raise Enable
  LCD_PORT = LCD_PORT | (1 << LCD_EN);
  _delay_us(DISPLAY_ENABLE_TIME_US);                       //Enable Pulse Width (at least 140 ns)

  //set Enable to 0
  LCD_PORT = LCD_PORT & ~(1 << LCD_EN);
  _delay_us(DISPLAY_ADDRESS_HOLD_TIME_US);                 //Adress Hold Time (at least 10 ns)

}


/****************************************************************************/
/**
* Sends a data byte to the LCD via the 4 Bit Bus to display it. If
* enabled (definition of PROCESS_NEWLINE), it automatically moves to a
* new line. \n
* This is an internal subfunction and no part of the user
* interface. \n
*
*
* \param [in] data          data byte to send
*
*
* \date
* - Created 26.07.2009
* - Modified  26.07.2009
*
* \see PROCESS_NEWLINE
* \see DISPLAY_RESOLUTION
* \see DISPLAY_NUMBER_ROWS
*
* \author
*   Michael Skutek, michael@skutek.de
*/
/****************************************************************************/

void lcd_send_data(unsigned char data) {


//check for newline

#if PROCESS_NEWLINE == TRUE

#if DISPLAY_RESOLUTION == HIGH_RES
#if DISPLAY_NUMBER_ROWS == 2
  if (data == '\n') {
    lcd_set_cursor(0, 0);    //wrap around to first line
    return;
  }
#else
  if (data == '\n') {
    if (next_ddram_address <= (DISPLAY_NUMBER_COLUMNS - 1)) {
      lcd_set_cursor(0, 1);
    } else {
      lcd_set_cursor(0, 0);    //wrap around to first line
    }
    return;
  }
#endif
#else
#if DISPLAY_NUMBER_ROWS == 2
  if (data == '\n') {
    if (next_ddram_address <= DISPLAY_NUMBER_COLUMNS - 1) {
      lcd_set_cursor(0, 1);
    } else if (next_ddram_address <= (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS - 1)) {
      lcd_set_cursor(0, 0);    //wrap around to first line
    }
    return;
  }
#else
  if (data == '\n') {
    if (next_ddram_address <= DISPLAY_NUMBER_COLUMNS - 1) {
      lcd_set_cursor(0, 1);
    } else if ((next_ddram_address <= (DISPLAY_NUMBER_COLUMNS + DISPLAY_NUMBER_COLUMNS - 1))) {
      lcd_set_cursor(0, 3);
    } else if (next_ddram_address <= (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS - 1)) {
      lcd_set_cursor(0, 2);
    } else if (next_ddram_address <= (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS + DISPLAY_NUMBER_COLUMNS - 1)) {
      lcd_set_cursor(0, 0);    //wrap around to first line
    }
    return;
  }
#endif
#endif

#endif


  //send data byte
#if USE_LCD_RW_PIN == TRUE
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#else
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#endif
  LCD_PORT = LCD_PORT | (1 << LCD_RS);                       //raise RS for writing data into LCD RAM
  LCD_PORT = LCD_PORT | (((data & 0x10) >> 4) << LCD_DB4);   //Bit 4
  LCD_PORT = LCD_PORT | (((data & 0x20) >> 5) << LCD_DB5);   //Bit 5
  LCD_PORT = LCD_PORT | (((data & 0x40) >> 6) << LCD_DB6);   //Bit 6
  LCD_PORT = LCD_PORT | (((data & 0x80) >> 7) << LCD_DB7);   //Bit 7

  lcd_enable();                                          //enable pulse

#if USE_LCD_RW_PIN == TRUE
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#else
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#endif
  LCD_PORT = LCD_PORT | (1 << LCD_RS);
  LCD_PORT = LCD_PORT | ((data & 0x01) << LCD_DB4);          //Bit 0
  LCD_PORT = LCD_PORT | (((data & 0x02) >> 1) << LCD_DB5);   //Bit 1
  LCD_PORT = LCD_PORT | (((data & 0x04) >> 2) << LCD_DB6);   //Bit 2
  LCD_PORT = LCD_PORT | (((data & 0x08) >> 3) << LCD_DB7);   //Bit 3

  lcd_enable();                                          //enable pulse
  _delay_us(DISPLAY_DATATRANSFER_TIME_US);               //wait time for next data transfer (alternative: check for busy flag)



  //check for ddram address and set new address
#if AUTOWRAP_AROUND == TRUE

#if DISPLAY_RESOLUTION == HIGH_RES       //5x11 Dots
#if DISPLAY_NUMBER_ROWS == 2
  if (next_ddram_address == (DISPLAY_NUMBER_COLUMNS - 1)) {
    lcd_set_cursor(0, 0);    //wrap around to first line
  } else {
    next_ddram_address++;    //increase address pointer
  }
#else
  if (next_ddram_address == (DISPLAY_NUMBER_COLUMNS + DISPLAY_NUMBER_COLUMNS - 1)) {
    lcd_set_cursor(0, 0);    //wrap around to first line
  } else {
    next_ddram_address++;    //increase address pointer
  }
#endif
#else
#if DISPLAY_NUMBER_ROWS == 2
  if (next_ddram_address == DISPLAY_NUMBER_COLUMNS - 1) {
    lcd_set_cursor(0, 1);
  } else if (next_ddram_address == (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS - 1)) {
    lcd_set_cursor(0, 0);
  } else {
    next_ddram_address++;    //increase address pointer
  }
#else
  if (next_ddram_address == DISPLAY_NUMBER_COLUMNS - 1) {
    lcd_set_cursor(0, 1);
  } else if (next_ddram_address == (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS - 1)) {
    lcd_set_cursor(0, 2);
  } else if ((next_ddram_address == (DISPLAY_NUMBER_COLUMNS + DISPLAY_NUMBER_COLUMNS - 1))) {
    lcd_set_cursor(0, 3);
  } else if (next_ddram_address == (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS + DISPLAY_NUMBER_COLUMNS - 1)) {
    lcd_set_cursor(0, 0);    //wrap around to first line
  } else {
    next_ddram_address++;    //increase address pointer
  }
#endif
#endif

#endif

}


/****************************************************************************/
/**
* Sends a command byte to the LCD. \n
* This is an internal subfunction and no part of the user
* interface. \n
*
*
* \param [in] command          command byte to send
*
*
* \date
* - Created 26.07.2009
* - Modified  26.07.2009
*
*
* \author
*   Michael Skutek, michael@skutek.de
*/
/****************************************************************************/

void lcd_send_command(unsigned char command) {

#if USE_LCD_RW_PIN == TRUE
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#else
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#endif
  LCD_PORT = LCD_PORT | (((command & 0x10) >> 4) << LCD_DB4);   //Bit 4
  LCD_PORT = LCD_PORT | (((command & 0x20) >> 5) << LCD_DB5);   //Bit 5
  LCD_PORT = LCD_PORT | (((command & 0x40) >> 6) << LCD_DB6);   //Bit 6
  LCD_PORT = LCD_PORT | (((command & 0x80) >> 7) << LCD_DB7);   //Bit 7

  lcd_enable();

#if USE_LCD_RW_PIN == TRUE
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#else
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#endif
  LCD_PORT = LCD_PORT | ((command & 0x01) << LCD_DB4);          //Bit 0
  LCD_PORT = LCD_PORT | (((command & 0x02) >> 1) << LCD_DB5);   //Bit 1
  LCD_PORT = LCD_PORT | (((command & 0x04) >> 2) << LCD_DB6);   //Bit 2
  LCD_PORT = LCD_PORT | (((command & 0x08) >> 3) << LCD_DB7);   //Bit 3

  lcd_enable();
  _delay_us(DISPLAY_COMMANDTRANSFER_TIME_US);         //wait time for next data transfer (alternative: check for busy flag)

}


/****************************************************************************/
/**
* Sets the cursor to a specified position. \n
*
*
* \param [in] column          LCD column (starts with 0)
* \param [in] row             LCD row (starts with 0, maximum: 3)
*
*
* \date
* - Created 26.07.2009
* - Modified  26.07.2009
*
*
* \see DISPLAY_RESOLUTION
* \see DISPLAY_NUMBER_COLUMNS
* \see DISPLAY_NUMBER_ROWS
*
*
* \author
*   Michael Skutek, michael@skutek.de
*/
/****************************************************************************/

void lcd_set_cursor(uint8_t column, uint8_t row) {

  uint8_t address;


  //check for number of rows and columns
  if (column > DISPLAY_NUMBER_COLUMNS || row > DISPLAY_NUMBER_ROWS) {
    return;
  }

#if DISPLAY_RESOLUTION == HIGH_RES   //5x11 Dots

  //for LCDs up to 2 rows
  switch (row) {

  case 1:           //1. row
    address = SET_DDRAM_ADDRESS | column;                                            //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address
    break;

  case 2:           //2. row
    address = SET_DDRAM_ADDRESS | (DISPLAY_NUMBER_COLUMNS + column);                 //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address
    break;

  default:
    return;                   //do nothing if wrong row requested

  }

#else      //5x8 Dots

  //for LCDs up to 4 rows
  switch (row) {

  case 0:           //1. row
    address = SET_DDRAM_ADDRESS | column;                                            //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address
    break;

  case 1:           //2. row
    address = SET_DDRAM_ADDRESS | (DDRAM_OFFSET + column);                           //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address
    break;

  case 2:           //3. row
    address = SET_DDRAM_ADDRESS | (DISPLAY_NUMBER_COLUMNS + column);                 //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address
    break;

  case 3:           //4. row
    address = SET_DDRAM_ADDRESS | (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS + column);  //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address
    break;

  default:
    return;                   //do nothing if wrong row requested
  }
#endif

  lcd_send_command(address);   //send new address to LCD
  next_ddram_address = address - 0x80;         //set next ddram address (and remove command bit)
}


/****************************************************************************/
/**
* Sends a string to the LCD. It reads the string data from RAM or from
* PROGMEM if requested. \n
*
*
* \param [in] data          pointer to string data
* \param [in] flash_mem     specifies the string data location:
*                             - flash_mem == TRUE: data source is the
*                               Program Memory
*                             - flash_mem == FALSE: data source is RAM
*
*
* \date
* - Created 26.07.2009
* - Modified  26.07.2009
*
*
* \see pgm_read_byte() (avr/pgmspace.h)
*
* \author
*   Michael Skutek, michael@skutek.de
*/
/****************************************************************************/

void lcd_send_string(const char *data, uint8_t flash_mem) {

  char c;

  if (flash_mem == TRUE) {

    c = pgm_read_byte(data);
    while (c != '\0') {   //until *s != '\0' --> means "string end sign"
      lcd_send_data(c);
      data++;
      c = pgm_read_byte(data);
    }

  } else {

    while (*data != '\0') {  //until *s != '\0' --> means "string end sign"
      lcd_send_data(*data);
      data++;
    }

  }
}


/****************************************************************************/
/**
* Clears the complete display. \n
*
*
* \date
* - Created 26.07.2009
* - Modified  26.07.2009
*
*
* \author
*   Michael Skutek, michael@skutek.de
*/
/****************************************************************************/

void lcd_clear(void) {

  lcd_send_command(CLEAR_DISPLAY);
  next_ddram_address = 0x00;                     //set next ddram address
  _delay_ms(DISPLAY_CLEAR_WAIT_TIME_MS);         //wait time for clear command (at least 1,5 ms)

}


/****************************************************************************/
/**
* Sets cursor to home position. \n
*
*
* \date
* - Created 26.07.2009
* - Modified  26.07.2009
*
*
* \author
*   Michael Skutek, michael@skutek.de
*/
/****************************************************************************/

void lcd_cursor_home(void) {

  lcd_send_command(CURSOR_HOME);
  next_ddram_address = 0x00;                     //set next ddram address
  _delay_ms(DISPLAY_CURSORHOME_WAIT_TIME_MS);    //wait time for clear command (at least 1,5 ms)

}


/****************************************************************************/
/**
* Initializes the LCD after Reset. This function should be called AFTER
* lcd_pin_init() and BEFORE any other display function can be used. \n
*
*
* \date
* - Created 26.07.2009
* - Modified  26.07.2009
*
*
* \author
*   Michael Skutek, michael@skutek.de
*/
/****************************************************************************/

void lcd_init(void) {

  uint8_t tmp;

  //wait for some time after Reset
  _delay_ms(DISPLAY_RESET_WAIT_TIME_MS);

  //Function Set (8 Bit Mode)
#if USE_LCD_RW_PIN == TRUE
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#else
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#endif
  LCD_PORT = LCD_PORT | (1 << LCD_DB5) | (1 << LCD_DB4);
  lcd_enable();
  _delay_ms(DISPLAY_INIT_WAIT_TIME01_MS);

  //Function Set (8 Bit Mode)
#if USE_LCD_RW_PIN == TRUE
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#else
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#endif
  LCD_PORT = LCD_PORT | (1 << LCD_DB5) | (1 << LCD_DB4);
  lcd_enable();
  _delay_ms(DISPLAY_INIT_WAIT_TIME02_MS);

  //Function Set (8 Bit Mode)
#if USE_LCD_RW_PIN == TRUE
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#else
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#endif
  LCD_PORT = LCD_PORT | (1 << LCD_DB5) | (1 << LCD_DB4);
  lcd_enable();
  _delay_ms(DISPLAY_INIT_WAIT_TIME03_MS);

  //Function Set (switch to 4 Bit Mode)
#if USE_LCD_RW_PIN == TRUE
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#else
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#endif
  LCD_PORT = LCD_PORT | (1 << LCD_DB5);
  lcd_enable();
  _delay_ms(DISPLAY_INIT_WAIT_TIME04_MS);


  //Function Set (4 Bit)
#if DISPLAY_RESOLUTION == HIGH_RES
  lcd_send_command(0x24);                //1 Line and 5x11 dots
#else
  lcd_send_command(0x28);                //2 lines and 5x8 dots
#endif
  _delay_ms(DISPLAY_INIT_WAIT_TIME_MS);

  //Display Off
  lcd_send_command(0x08);
  _delay_ms(DISPLAY_INIT_WAIT_TIME_MS);

  //Clear Display Command
  lcd_send_command(CLEAR_DISPLAY);
  _delay_ms(DISPLAY_CLEAR_WAIT_TIME_MS);

  //Entry Mode (Increment, Shift Off)
  lcd_send_command(0x06);
  _delay_ms(DISPLAY_INIT_WAIT_TIME_MS);

  //Display On
  tmp = 0x0c;
#if DISPLAY_CURSOR == CURSOR_ON        //Cursor On?
  tmp = tmp | 0x02;
#endif
#if DISPLAY_CURSOR_BLINK == BLINK_ON   //Blinking On?
  tmp = tmp | 0x01;
#endif
  lcd_send_command(tmp);
  _delay_ms(DISPLAY_INIT_WAIT_TIME_MS);

  //Cursor Home Command
  lcd_send_command(CURSOR_HOME);
  _delay_ms(DISPLAY_CURSORHOME_WAIT_TIME_MS);

  //reset LCD port
#if USE_LCD_RW_PIN == TRUE
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#else
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
#endif


}


/****************************************************************************/
/**
* Initializes the I/O pins for interfacing the LCD. This function
* should be called BEFORE any other display function can be used and
* lcd_init() should be called AFTER it. \n
*
*
* \date
* - Created 26.07.2009
* - Modified  26.07.2009
*
* \see port_definitions.h
*
* \author
*   Michael Skutek, michael@skutek.de
*/
/****************************************************************************/

void lcd_pin_init(void) {

  //initial values = 0
#if USE_LCD_RW_PIN == TRUE
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
  //set LCD Port Pins to Output ---> see "port_definitions.h"
  LCD_DDR = LCD_DDR | (1 << LCD_RS) | (1 << LCD_RW) | (1 << LCD_EN) | (1 << LCD_DB4) | (1 << LCD_DB5) | (1 << LCD_DB6) | (1 << LCD_DB7);
#else
  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
  //set LCD Port Pins to Output ---> see "port_definitions.h"
  LCD_DDR = LCD_DDR | (1 << LCD_RS) | (1 << LCD_EN) | (1 << LCD_DB4) | (1 << LCD_DB5) | (1 << LCD_DB6) | (1 << LCD_DB7);
#endif


}



/****************************************************************************/
/**
* Initializes the I/O pins for interfacing the LCD backlight. This function
* should be called BEFORE lcd_set_backlight() can be used. \n
*
*
* \date
* - Created 05.08.2011
* - Modified  05.08.2011
*
* \see port_definitions.h
*
* \author
*   Michael Skutek, michael@skutek.de
*/
/****************************************************************************/

void lcd_blpin_init(void) {

  //initial values = 0
  LCDBL_PORT = LCDBL_PORT & ~(1 << LCDBL_CTRL);
  //set LCD Port Pins to Output ---> see "port_definitions.h"
  LCDBL_DDR = LCDBL_DDR | (1 << LCDBL_CTRL);

  lcd_backlight_state = LCD_BL_OFF;

}
