Subversion Repositories HomeAutomation

Rev

Rev 775 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #ifndef LCD_H
  2. #define LCD_H
  3.  
  4. /* For compatibility with BIOS */
  5. #include <bios.h>
  6. #include <drivers/mcu/gpio.h>
  7. #define F_OSC F_CPU
  8.  
  9. /*************************************************************************
  10.  Title  :   C include file for the HD44780U LCD library (lcd.c)
  11.  Author:    Peter Fleury <pfleury@gmx.ch>  http://jump.to/fleury
  12.  File:      $Id: lcd.h,v 1.13.2.2 2006/01/30 19:51:33 peter Exp $
  13.  Software:  AVR-GCC 3.3
  14.  Hardware:  any AVR device, memory mapped mode only for AT90S4414/8515/Mega
  15. ***************************************************************************/
  16.  
  17. /**
  18.  @defgroup pfleury_lcd LCD library
  19.  @code #include <lcd.h> @endcode
  20.  
  21.  @brief Basic routines for interfacing a HD44780U-based text LCD display
  22.  
  23.  Originally based on Volker Oth's LCD library,
  24.  changed lcd_init(), added additional constants for lcd_command(),
  25.  added 4-bit I/O mode, improved and optimized code.
  26.        
  27.  Library can be operated in memory mapped mode (LCD_IO_MODE=0) or in
  28.  4-bit IO port mode (LCD_IO_MODE=1). 8-bit IO port mode not supported.
  29.  
  30.  Memory mapped mode compatible with Kanda STK200, but supports also
  31.  generation of R/W signal through A8 address line.
  32.        
  33.  @author Peter Fleury pfleury@gmx.ch http://jump.to/fleury
  34.  
  35.  @see The chapter <a href="http://homepage.sunrise.ch/mysunrise/peterfleury/avr-lcd44780.html" target="_blank">Interfacing a HD44780 Based LCD to an AVR</a>
  36.       on my home page.
  37.  
  38.  CHANGES: Added parameter ANIMATION_OUTPUT that slows output down but makes it a bit nicer. /noddan 07-10-02
  39.  
  40. */
  41.  
  42. /*@{*/
  43.  
  44. #if (__GNUC__ * 100 + __GNUC_MINOR__) < 303
  45. #error "This library requires AVR-GCC 3.3 or later, update to newer AVR-GCC compiler !"
  46. #endif
  47.  
  48. #include <inttypes.h>
  49. #include <avr/pgmspace.h>
  50.  
  51. #include <config.h>
  52.  
  53. #define LED_ON PORTD &= ~(1<<PD3);
  54. #define LED_OFF PORTD |= (1<<PD3);
  55.  
  56. /**
  57.  *  @name  Definitions for MCU Clock Frequency
  58.  *  Adapt the MCU clock frequency in Hz to your target.
  59.  */
  60.  
  61. /**
  62.  * @name  Definition for LCD controller type
  63.  * Use 0 for HD44780 controller, change to 1 for displays with KS0073 controller.
  64.  */
  65. #define LCD_CONTROLLER_KS0073 0  /**< Use 0 for HD44780 controller, 1 for KS0073 controller */
  66.  
  67. /**
  68.  *  @name  Definitions for Display Size
  69.  *  Change these definitions to adapt setting to your display
  70.  */
  71. #define LCD_LINES           4     /**< number of visible lines of the display */
  72. #define LCD_DISP_LENGTH    20     /**< visibles characters per line of the display */
  73. #define LCD_LINE_LENGTH  0x40     /**< internal line length of the display    */
  74. #define LCD_START_LINE1  0x00     /**< DDRAM address of first char of line 1 */
  75. #define LCD_START_LINE2  0x40     /**< DDRAM address of first char of line 2 */
  76. #define LCD_START_LINE3  0x14     /**< DDRAM address of first char of line 3 */
  77. #define LCD_START_LINE4  0x54     /**< DDRAM address of first char of line 4 */
  78. #define LCD_WRAP_LINES      0     /**< 0: no wrap, 1: wrap at end of visibile line */
  79.  
  80. #define LCD_IO_MODE      1         /**< 0: memory mapped mode, 1: IO port mode */
  81. #if LCD_IO_MODE
  82. /**
  83.  *  @name Definitions for 4-bit IO mode
  84.  *  Change LCD_PORT if you want to use a different port for the LCD pins.
  85.  *
  86.  *  The four LCD data lines and the three control lines RS, RW, E can be on the
  87.  *  same port or on different ports.
  88.  *  Change LCD_RS_PORT, LCD_RW_PORT, LCD_E_PORT if you want the control lines on
  89.  *  different ports.
  90.  *
  91.  *  Normally the four data lines should be mapped to bit 0..3 on one port, but it
  92.  *  is possible to connect these data lines in different order or even on different
  93.  *  ports by adapting the LCD_DATAx_PORT and LCD_DATAx_PIN definitions.
  94.  *  
  95.  */
  96.  
  97. #ifndef LCD_E
  98.  
  99. #define LCD_E       EXP_D
  100. #define LCD_RW      EXP_B
  101. #define LCD_RS      EXP_A
  102. #define LCD_DB4     EXP_J
  103. #define LCD_DB5     EXP_K
  104. #define LCD_DB6     EXP_L
  105. #define LCD_DB7     EXP_M
  106.  
  107. #endif
  108.  
  109.  
  110. #elif defined(__AVR_AT90S4414__) || defined(__AVR_AT90S8515__) || defined(__AVR_ATmega64__) || \
  111.       defined(__AVR_ATmega8515__)|| defined(__AVR_ATmega103__) || defined(__AVR_ATmega128__) || \
  112.       defined(__AVR_ATmega161__) || defined(__AVR_ATmega162__)
  113. /*
  114.  *  memory mapped mode is only supported when the device has an external data memory interface
  115.  */
  116. #define LCD_IO_DATA      0xC000    /* A15=E=1, A14=RS=1                 */
  117. #define LCD_IO_FUNCTION  0x8000    /* A15=E=1, A14=RS=0                 */
  118. #define LCD_IO_READ      0x0100    /* A8 =R/W=1 (R/W: 1=Read, 0=Write   */
  119. #else
  120. #error "external data memory interface not available for this device, use 4-bit IO port mode"
  121.  
  122. #endif
  123.  
  124.  
  125. /**
  126.  *  @name Definitions for LCD command instructions
  127.  *  The constants define the various LCD controller instructions which can be passed to the
  128.  *  function lcd_command(), see HD44780 data sheet for a complete description.
  129.  */
  130.  
  131. /* instruction register bit positions, see HD44780U data sheet */
  132. #define LCD_CLR               0      /* DB0: clear display                  */
  133. #define LCD_HOME              1      /* DB1: return to home position        */
  134. #define LCD_ENTRY_MODE        2      /* DB2: set entry mode                 */
  135. #define LCD_ENTRY_INC         1      /*   DB1: 1=increment, 0=decrement     */
  136. #define LCD_ENTRY_SHIFT       0      /*   DB2: 1=display shift on           */
  137. #define LCD_ON                3      /* DB3: turn lcd/cursor on             */
  138. #define LCD_ON_DISPLAY        2      /*   DB2: turn display on              */
  139. #define LCD_ON_CURSOR         1      /*   DB1: turn cursor on               */
  140. #define LCD_ON_BLINK          0      /*     DB0: blinking cursor ?          */
  141. #define LCD_MOVE              4      /* DB4: move cursor/display            */
  142. #define LCD_MOVE_DISP         3      /*   DB3: move display (0-> cursor) ?  */
  143. #define LCD_MOVE_RIGHT        2      /*   DB2: move right (0-> left) ?      */
  144. #define LCD_FUNCTION          5      /* DB5: function set                   */
  145. #define LCD_FUNCTION_8BIT     4      /*   DB4: set 8BIT mode (0->4BIT mode) */
  146. #define LCD_FUNCTION_2LINES   3      /*   DB3: two lines (0->one line)      */
  147. #define LCD_FUNCTION_10DOTS   2      /*   DB2: 5x10 font (0->5x7 font)      */
  148. #define LCD_CGRAM             6      /* DB6: set CG RAM address             */
  149. #define LCD_DDRAM             7      /* DB7: set DD RAM address             */
  150. #define LCD_BUSY              7      /* DB7: LCD is busy                    */
  151.  
  152. /* set entry mode: display shift on/off, dec/inc cursor move direction */
  153. #define LCD_ENTRY_DEC            0x04   /* display shift off, dec cursor move dir */
  154. #define LCD_ENTRY_DEC_SHIFT      0x05   /* display shift on,  dec cursor move dir */
  155. #define LCD_ENTRY_INC_           0x06   /* display shift off, inc cursor move dir */
  156. #define LCD_ENTRY_INC_SHIFT      0x07   /* display shift on,  inc cursor move dir */
  157.  
  158. /* display on/off, cursor on/off, blinking char at cursor position */
  159. #define LCD_DISP_OFF             0x08   /* display off                            */
  160. #define LCD_DISP_ON              0x0C   /* display on, cursor off                 */
  161. #define LCD_DISP_ON_BLINK        0x0D   /* display on, cursor off, blink char     */
  162. #define LCD_DISP_ON_CURSOR       0x0E   /* display on, cursor on                  */
  163. #define LCD_DISP_ON_CURSOR_BLINK 0x0F   /* display on, cursor on, blink char      */
  164.  
  165. /* move cursor/shift display */
  166. #define LCD_MOVE_CURSOR_LEFT     0x10   /* move cursor left  (decrement)          */
  167. #define LCD_MOVE_CURSOR_RIGHT    0x14   /* move cursor right (increment)          */
  168. #define LCD_MOVE_DISP_LEFT       0x18   /* shift display left                     */
  169. #define LCD_MOVE_DISP_RIGHT      0x1C   /* shift display right                    */
  170.  
  171. /* function set: set interface data length and number of display lines */
  172. #define LCD_FUNCTION_4BIT_1LINE  0x20   /* 4-bit interface, single line, 5x7 dots */
  173. #define LCD_FUNCTION_4BIT_2LINES 0x28   /* 4-bit interface, dual line,   5x7 dots */
  174. #define LCD_FUNCTION_8BIT_1LINE  0x30   /* 8-bit interface, single line, 5x7 dots */
  175. #define LCD_FUNCTION_8BIT_2LINES 0x38   /* 8-bit interface, dual line,   5x7 dots */
  176.  
  177.  
  178. #define LCD_MODE_DEFAULT     ((1<<LCD_ENTRY_MODE) | (1<<LCD_ENTRY_INC) )
  179.  
  180.  
  181.  
  182. /**
  183.  *  @name Functions
  184.  */
  185.  
  186.  
  187. /**
  188.  @brief    Initialize display and select type of cursor
  189.  @param    dispAttr \b LCD_DISP_OFF display off\n
  190.                     \b LCD_DISP_ON display on, cursor off\n
  191.                     \b LCD_DISP_ON_CURSOR display on, cursor on\n
  192.                     \b LCD_DISP_ON_CURSOR_BLINK display on, cursor on flashing            
  193.  @return  none
  194. */
  195. extern void lcd_init(uint8_t dispAttr);
  196.  
  197.  
  198. /**
  199.  @brief    Clear display and set cursor to home position
  200.  @return   none
  201. */
  202. extern void lcd_clrscr(void);
  203.  
  204.  
  205. /**
  206.  @brief    Set cursor to home position
  207.  @return   none
  208. */
  209. extern void lcd_home(void);
  210.  
  211.  
  212. /**
  213.  @brief    Set cursor to specified position
  214.  
  215.  @param    x horizontal position\n (0: left most position)
  216.  @param    y vertical position\n   (0: first line)
  217.  @return   none
  218. */
  219. extern void lcd_gotoxy(uint8_t x, uint8_t y);
  220.  
  221.  
  222. /**
  223.  @brief    Display character at current cursor position
  224.  @param    c character to be displayed                                      
  225.  @return   none
  226. */
  227. extern void lcd_putc(char c);
  228.  
  229.  
  230. /**
  231.  @brief    Display string without auto linefeed
  232.  @param    s string to be displayed                                        
  233.  @return   none
  234. */
  235. extern void lcd_puts(const char *s);
  236.  
  237.  
  238. /**
  239.  @brief    Display string from program memory without auto linefeed
  240.  @param    progmem_s string from program memory be be displayed                                        
  241.  @return   none
  242.  @see      lcd_puts_P
  243. */
  244. extern void lcd_puts_p(const char *progmem_s);
  245.  
  246.  
  247. /**
  248.  @brief    Send LCD controller instruction command
  249.  @param    cmd instruction to send to LCD controller, see HD44780 data sheet
  250.  @return   none
  251. */
  252. extern void lcd_command(uint8_t cmd);
  253.  
  254.  
  255. /**
  256.  @brief    Send data byte to LCD controller
  257.  
  258.  Similar to lcd_putc(), but without interpreting LF
  259.  @param    data byte to send to LCD controller, see HD44780 data sheet
  260.  @return   none
  261. */
  262. extern void lcd_data(uint8_t data);
  263.  
  264.  
  265. #ifdef ANIMATION_OUTPUT
  266. /**
  267.  @brief    Turn on/off LCD animation
  268.  @param    1 or 0, on or off
  269.  @return   none
  270. */
  271. extern void lcd_setanimation(uint8_t animation_set);
  272. #endif
  273.  
  274. // displays a horizontal progress bar at the current cursor location
  275. // <progress> is the value the bargraph should indicate
  276. // <maxprogress> is the value at the end of the bargraph
  277. // <length> is the number of LCD characters that the bargraph should cover
  278. void lcdProgressBar(uint16_t progress, uint16_t maxprogress, uint8_t length);
  279.  
  280.  
  281. //loads a custom character
  282. // <lcdCustomCharArray> is a pointer to a ROM array containing custom characters
  283. // <romCharNum> is the index of the character to load from lcdCustomCharArray
  284. // <lcdCharNum> is the RAM location in the LCD (legal value: 0-7)
  285. void lcdLoadCustomChar(uint8_t* lcdCustomCharArray, uint8_t romCharNum, uint8_t lcdCharNum);
  286.  
  287.  
  288.  
  289. /**
  290.  @brief macros for automatically storing string constant in program memory
  291. */
  292. #define lcd_puts_P(__s)         lcd_puts_p(PSTR(__s))
  293.  
  294. /*@}*/
  295. #endif //LCD_H
  296.