From Tim's website
Revision as of 22:12, 22 February 2010 by Tim (talk | contribs) (New page: <pre> →‎Define the hardware connections: const int LCD_EN_LINE = PIN_B1; →‎Enable device, high-low to clock data: const int LCD_RW_LINE = PIN_B2; /* Read / Write line ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
/* Define the hardware connections */
const int LCD_EN_LINE  = PIN_B1; /* Enable device, high-low to clock data     */
const int LCD_RW_LINE  = PIN_B2; /* Read / Write line                         */
const int LCD_RS_LINE  = PIN_B3; /* Register Select line command or character */
const int LCD_BUSY_PIN = PIN_C7; /* DB7 (connected to C7 shows busy flag      */

/* Define some commands */
const int LCD_CLEAR      = 0x01; /* Clear the display and send cursor to Home */
const int LCD_CUR_HOME   = 0x02; /* Send the cursor to Home but keep display  */

const int LCD_CUR_DEC    = 0x04; /* Set cursor to move left after character   */
const int LCD_CUR_INC    = 0x06; /* Set cursor to move right after character  */
const int LCD_CUR_SCR_R  = 0x05; /* Set display to move right after character */
const int LCD_CUR_SCR_L  = 0x07; /* Set display to move left after character  */

const int LCD_DISP_OFF   = 0x08; /* Turn off the LCD display                  */
const int LCD_DISP_ON    = 0x0C; /* Turn on display, no cursor                */
const int LCD_DISP_CUR   = 0x0E; /* Turn on display, with cursor              */
const int LCD_DISP_BLINK = 0x0F; /* Turn on display, with blinking cursor     */

const int LCD_SHFT_CUR_L = 0x10; /* Decrement the cursor (move left)          */
const int LCD_SHFT_CUR_R = 0x14; /* Increment the cursor (move right)         */
const int LCD_SHFT_SCR_L = 0x18; /* Shift the display (scroll) to the left    */
const int LCD_SHFT_SCR_R = 0x1C; /* Shift the display (scroll) to the right   */

const int LCD_SIZE_LARGE = 0x34; /* Large cursor, one line, 8 bit interface   */
const int LCD_SIZE_SMALL = 0x38; /* Small cursor, two lines, 8 bit interface  */

/* Address is added to the following two values, up to 0F for each line       */
const int LCD_DD_ADD_L1  = 0x80; /* First Display RAM address on Line 1       */
const int LCD_DD_ADD_L2  = 0xC0; /* First Display RAM address on Line 2       */

/* Line number for dots is added to each of the following addresses, up to 07 */
const int LCD_CG_ADD_C0  = 0x40; /* First Character Generator RAM address     */
const int LCD_CG_ADD_C1  = 0x48; /* Second Character Generator RAM address    */
const int LCD_CG_ADD_C2  = 0x50; /* Third Character Generator RAM address     */
const int LCD_CG_ADD_C3  = 0x58; /* Fourth Character Generator RAM address    */
const int LCD_CG_ADD_C4  = 0x60; /* Fifth Character Generator RAM address     */
const int LCD_CG_ADD_C5  = 0x68; /* Sixth Character Generator RAM address     */
const int LCD_CG_ADD_C6  = 0x70; /* Seventh Character Generator RAM address   */
const int LCD_CG_ADD_C7  = 0x78; /* Eighth Character Generator RAM address    */


/* Functions */
void lcd_command( char command );
void lcd_character( char character );
void lcd_number( int number );
void lcd_initialise( int size );


void lcd_command( char command )
/* Sends the given command to the LCD display. Most of the commands are       */
/*  defined in the header. This function will wait for the busy flag on the   */
/*  display to clear before sending the command and returning.                */
{
   output_low( LCD_RS_LINE );
   output_high( LCD_RW_LINE );

   /* Wait for busy flag */
   while( input( LCD_BUSY_PIN ) );

   output_low( LCD_RW_LINE );

   /* Send the command */
   output_c( command );
   output_low( LCD_EN_LINE );
   delay_us( 200 );
   output_high( LCD_EN_LINE );

   /* Impose a 5ms delay while the command is processed */
   delay_ms( 5 );
}


void lcd_character( char character )
/* Sends the given character to the LCD display. ASCII Numbers and letters    */
/*  are valid, and punctuation below 0x7F. Characters 0x00 to 0x0F are the    */
/*  Generated Characters, which will be blank initially. Other useful symbols */
/*  and Greek characters may be found in the range 0x80 to 0xFF               */
{
   output_low( LCD_RS_LINE );
   output_high( LCD_RW_LINE );

   /* Wait for busy flag */
   while( input( LCD_BUSY_PIN ) );

   output_low( LCD_RW_LINE );
   output_high( LCD_RS_LINE );

   /* Send the command */
   output_c( character );
   output_low( LCD_EN_LINE );
   delay_us( 200 );
   output_high( LCD_EN_LINE );
}


void lcd_number( int number )
/* Sends the given number to the LCD display, with up to 2 characters. This   */
/*  limits number to 100                                                      */
{
   int tens = 0;
   int units = 0;

   while( number > 9 )
   {
      tens++;
      number -= 10;
   }
   units = number;
   
   lcd_character( '0' + tens );
   lcd_character( '0' + units );
}


void lcd_initialise( int size )
/* Initialises the display incase the initialisation at startup failed or the */
/*  cursor size is to be changed. 'size' is LCD_SIZE_SMALL or LCD_SIZE_LARGE  */
{
   int i;
   
   delay_ms( 15 );
   output_low( LCD_RS_LINE );

   for( i = 0; i < 3; i++ )
   {
      output_c( size );

      output_low( LCD_EN_LINE );
      delay_us( 200 );
      output_high( LCD_EN_LINE );

      delay_ms( 5 );
   }

   lcd_command( size );
   lcd_command( LCD_DISP_OFF );
   lcd_command( LCD_DISP_ON );
   lcd_command( LCD_CUR_INC );
   lcd_command( LCD_CLEAR );
}