//!/////////////////////////////////////////////////////////////////////////
/// lcd.c                                                                 //
///                                                                       //
/// Version:     0.7                                                      //
/// Date:        01/09/2010                                               //
/// Author:      Michael Skutek (michael@skutek.de)                       //
///                                                                       //
/// Description: 4 Bit Interface to LCD displays with up to 4 rows and    //
///              a single Controller (e.g. ST7066U or HD44780).           //
///                                                                       //
/// 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:                defintions 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/>.                                       //
//!/////////////////////////////////////////////////////////////////////////


#ifndef LCD_H_INCLUDED
#define LCD_H_INCLUDED



///display general definitions

//values (Do not modify - to be used for mode settings!)
#ifndef TRUE
#define TRUE                                1
#endif
#ifndef FALSE
#define FALSE                               0
#endif
#define HIGH_RES                            1             //5x11 dots
#define LOW_RES                             0             //5x8 dots
#define CURSOR_ON                           1             //cursor on
#define CURSOR_OFF                          0             //cursor off
#define BLINK_ON                            1             //cursor blinking on
#define BLINK_OFF                           0             //cursor blinking off

#define USE_LCD_RW_PIN                  FALSE            //R/W Pin connected [TRUE] or not [FALSE]

///Adapt the following parameters depending on your LCD:

//modes
#define DISPLAY_RESOLUTION            LOW_RES             //1 line and 5x11 dots [HIGH_RES] or 2 lines and 5x8 dots [LOW_RES]
#define DISPLAY_CURSOR             CURSOR_OFF             //switches cursor on [CURSOR_ON] or off [CURSOR_OFF]
#define DISPLAY_CURSOR_BLINK        BLINK_OFF             //cursor blinking on [BLINK_ON] or off [BLINK_ON]

//disable tho following two items if text is scrambled on display
#define AUTOWRAP_AROUND                  TRUE             //automatically wrap to next (or back to first) line if string exceeds current line
#define PROCESS_NEWLINE                  TRUE             //go to next line if newline character in string was detected

//display parameters
#define DISPLAY_NUMBER_ROWS                 2             //number of display rows (valid only for 5x8 dots!)
#define DISPLAY_NUMBER_COLUMNS             16             //number of display columns
#define DDRAM_OFFSET                     0x40             //address offset for 2. line (for 2-line-mode)

//timing
#define DISPLAY_ENABLE_TIME_US              1             //width of enbale pulse [us]
#define DISPLAY_ADDRESS_HOLD_TIME_US        1             //address hold time (after enable pulse) [us]
#define DISPLAY_DATATRANSFER_TIME_US       45             //wait time after data transfer [us]
#define DISPLAY_COMMANDTRANSFER_TIME_US    45             //wait time after command transfer [us]
#define DISPLAY_CLEAR_WAIT_TIME_MS          3             //wait time after clear command [ms]
#define DISPLAY_CURSORHOME_WAIT_TIME_MS     3             //wait time after cursor home command [ms]
#define DISPLAY_RESET_WAIT_TIME_MS         50             //wait time after reset [ms]
#define DISPLAY_INIT_WAIT_TIME01_MS         5             //wait time after 1. init message (function set 8 Bit) [ms]
#define DISPLAY_INIT_WAIT_TIME02_MS         2             //wait time after 2. init messages (function set 8 Bit) [ms]
#define DISPLAY_INIT_WAIT_TIME03_MS         2             //wait time after 3. init messages (function set 8 Bit) [ms]
#define DISPLAY_INIT_WAIT_TIME04_MS         2             //wait time after 4. init message (function set 4 Bit) [ms]
#define DISPLAY_INIT_WAIT_TIME_MS           1             //standard wait time after 4 Bit init message [ms]




///LCD Commands
#define CLEAR_DISPLAY                    0x01             //Clear Display and DDRAM
#define CURSOR_HOME                      0x02             //move Cursor to Home position
#define SET_DDRAM_ADDRESS                0x80             //set DDRAM Address (DB0...DB6 transport address)




///function prototypes

void lcd_send_data(unsigned char data);
void lcd_send_command(unsigned char command);
void lcd_set_cursor(uint8_t column, uint8_t row);
void lcd_send_string(const char *data, uint8_t flash_mem);
void lcd_enable(void);
void lcd_clear(void);
void lcd_cursor_home(void);
void lcd_init(void);
void lcd_pin_init(void);



#endif // LCD_H_INCLUDED
