/*-----------------------------------------------------------*/
/* LCD Display Ansteuerungsleitungen                         */
/*-----------------------------------------------------------*/
#define LCD_E 	P3_6
#define LCD_RS  P3_7
#define LCD_P	P1

#define	SWAPBYTE

/*-----------------------------------------------------------*/
/* Byte verdrehen				             */
/*-----------------------------------------------------------*/
#ifdef SWAPBYTE
unsigned char swapbyte( unsigned char x)
{
  unsigned char i;
  unsigned char res=0;

  for(i=0;i<8;i++)
  {
    res = res << 1;
    res |= (x & 0x01);
    x = x >> 1;
  }
  return res;
}
#endif

/*-----------------------------------------------------------*/
/* Warten   					             */
/*-----------------------------------------------------------*/
void lcd_wait(unsigned int wielange)
{
  unsigned int i;
 
  for(i=0;i<wielange;i++)
    ;
}

/*-----------------------------------------------------------*/
/* LCD Daten auf Datenport ausgeben	                     */
/*-----------------------------------------------------------*/
void lcd_write_data(unsigned char d)
{
  LCD_RS = 1;
#ifdef SWAPBYTE
  LCD_P = swapbyte(d);
#else
  LCD_P = d;
#endif
  LCD_E = 1; 
  lcd_wait(10);
  LCD_E = 0;
  lcd_wait(10);
}

/*-----------------------------------------------------------*/
/* LCD Daten auf Steuerport ausgeben	                     */
/*-----------------------------------------------------------*/
void lcd_write_contr(unsigned char d)
{
  LCD_RS = 0;
#ifdef SWAPBYTE
  LCD_P = swapbyte(d);
#else
  LCD_P = d;
#endif
  LCD_E = 1;
  lcd_wait(10);
  LCD_E = 0;
  lcd_wait(10);
}

/*-----------------------------------------------------------*/
/* LCD Schirm loeschen			                     */
/*-----------------------------------------------------------*/
void lcd_clr( void )
{
  lcd_write_contr(0x01);
  lcd_wait(1000);
}

/*-----------------------------------------------------------*/
/* LCD Display initialisieren		                     */
/*-----------------------------------------------------------*/
void lcd_init( void )
{
  lcd_wait(3000);
  LCD_E = 0;
  LCD_RS = 0;
  LCD_P = 0;
  lcd_write_contr( 0x38 );	/* Interface einstellen	     */
  lcd_wait(1000);
  lcd_write_contr( 0x0C );	/* Cursor einstellen	     */
  lcd_wait(1000);
  lcd_clr();
}

/*-----------------------------------------------------------*/
/* LCD Display string ausgeben		                     */
/*-----------------------------------------------------------*/
void lcd_puts( char *str )
{
  while(*str)
    lcd_write_data(*(str++));
}

/*-----------------------------------------------------------*/
/* LCD Display string ausgeben		                     */
/*-----------------------------------------------------------*/
void lcd_cursor(unsigned char x,unsigned char y )
{
  lcd_write_contr(0x80 + x + y * 0x40);
}



