www.pudn.com > netbox-ii_ks0108_192x64.rar > lcd19264.c


/**************************************************************************** 
*                   	     Emtronix Inc. 
*                       5# Gaopen Road, Chengdu 
*                        Sichuan 610041, P.R.C 
* 
*            Copyright (c) 2001 Emtronix, All Rights Reserved 
*            Copyright (c) 2001 Cheng Shi, All Rights Reserved 
*****************************************************************************/ 
#include  
#include  
#include "lcd19264.h" 
 
unsigned char VerticalFont[128][8] = 
    { {	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000 }, 
      { 0x07e,0x081,0x095,0x0b1,0x0b1,0x095,0x081,0x07e }, 
      {	0x07e,0x0ff,0x0eb,0x0cf,0x0cf,0x0eb,0x0ff,0x07e }, 
      {	0x00e,0x01f,0x03f,0x07e,0x03f,0x01f,0x00e,0x000 }, 
      {	0x008,0x01c,0x03e,0x07f,0x03e,0x01c,0x008,0x000 }, 
      {	0x018,0x0ba,0x0ff,0x0ff,0x0ff,0x0ba,0x018,0x000 }, 
      {	0x010,0x0b8,0x0fc,0x0ff,0x0fc,0x0b8,0x010,0x000 }, 
      {	0x000,0x000,0x018,0x03c,0x03c,0x018,0x000,0x000 }, 
      {	0x0ff,0x0ff,0x0e7,0x0c3,0x0c3,0x0e7,0x0ff,0x0ff }, 
      {	0x000,0x03c,0x066,0x042,0x042,0x066,0x03c,0x000 }, 
      {	0x0ff,0x0c3,0x099,0x0bd,0x0bd,0x099,0x0c3,0x0ff }, 
      {	0x070,0x0f8,0x088,0x088,0x0fd,0x07f,0x007,0x00f }, 
      {	0x000,0x04e,0x05f,0x0f1,0x0f1,0x05f,0x04e,0x000 }, 
      {	0x0c0,0x0e0,0x0ff,0x07f,0x005,0x005,0x007,0x007 }, 
      {	0x0c0,0x0ff,0x07f,0x005,0x015,0x075,0x07f,0x02f }, 
      {	0x099,0x05a,0x03c,0x0e7,0x0e7,0x03c,0x05a,0x099 }, 
      {	0x07f,0x03e,0x03e,0x01c,0x01c,0x008,0x008,0x000 }, 
      {	0x008,0x008,0x01c,0x01c,0x03e,0x03e,0x07f,0x000 }, 
      {	0x000,0x024,0x066,0x0ff,0x0ff,0x066,0x024,0x000 }, 
      {	0x000,0x05f,0x05f,0x000,0x000,0x05f,0x05f,0x000 }, 
      {	0x006,0x00f,0x009,0x07f,0x07f,0x001,0x07f,0x07f }, 
      {	0x040,0x0da,0x0bf,0x0a5,0x0fd,0x059,0x003,0x002 }, 
      {	0x000,0x070,0x070,0x070,0x070,0x070,0x070,0x000 }, 
      {	0x080,0x094,0x0b6,0x0ff,0x0ff,0x0b6,0x094,0x080 }, 
      {	0x000,0x004,0x006,0x07f,0x07f,0x006,0x004,0x000 }, 
      {	0x000,0x010,0x030,0x07f,0x07f,0x030,0x010,0x000 }, 
      {	0x008,0x008,0x008,0x02a,0x03e,0x01c,0x008,0x000 }, 
      {	0x008,0x01c,0x03e,0x02a,0x008,0x008,0x008,0x000 }, 
      {	0x03c,0x03c,0x020,0x020,0x020,0x020,0x020,0x000 }, 
      {	0x008,0x01c,0x03e,0x008,0x008,0x03e,0x01c,0x008 }, 
      {	0x030,0x038,0x03c,0x03e,0x03e,0x03c,0x038,0x030 }, 
      {	0x006,0x00e,0x01e,0x03e,0x03e,0x01e,0x00e,0x006 }, 
      {	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000 }, 
      {	0x000,0x006,0x05f,0x05f,0x006,0x000,0x000,0x000 }, 
      {	0x000,0x007,0x007,0x000,0x007,0x007,0x000,0x000 }, 
      {	0x014,0x07f,0x07f,0x014,0x07f,0x07f,0x014,0x000 }, 
      {	0x024,0x02e,0x06b,0x06b,0x03a,0x012,0x000,0x000 }, 
      {	0x046,0x066,0x030,0x018,0x00c,0x066,0x062,0x000 }, 
      {	0x030,0x07a,0x04f,0x05d,0x037,0x07a,0x048,0x000 }, 
      {	0x004,0x007,0x003,0x000,0x000,0x000,0x000,0x000 }, 
      {	0x000,0x01c,0x03e,0x063,0x041,0x000,0x000,0x000 }, 
      {	0x000,0x041,0x063,0x03e,0x01c,0x000,0x000,0x000 }, 
      {	0x008,0x02a,0x03e,0x01c,0x01c,0x03e,0x02a,0x008 }, 
      {	0x008,0x008,0x03e,0x03e,0x008,0x008,0x000,0x000 }, 
      {	0x000,0x080,0x0e0,0x060,0x000,0x000,0x000,0x000 }, 
      {	0x008,0x008,0x008,0x008,0x008,0x008,0x000,0x000 }, 
      {	0x000,0x000,0x060,0x060,0x000,0x000,0x000,0x000 }, 
      {	0x060,0x030,0x018,0x00c,0x006,0x003,0x001,0x000 }, 
      {	0x03e,0x07f,0x071,0x059,0x04d,0x07f,0x03e,0x000 }, 
      {	0x040,0x042,0x07f,0x07f,0x040,0x040,0x000,0x000 }, 
      {	0x062,0x073,0x059,0x049,0x06f,0x066,0x000,0x000 }, 
      {	0x022,0x063,0x049,0x049,0x07f,0x036,0x000,0x000 }, 
      {	0x018,0x01c,0x016,0x053,0x07f,0x07f,0x050,0x000 }, 
      {	0x027,0x067,0x045,0x045,0x07d,0x039,0x000,0x000 }, 
      {	0x03c,0x07e,0x04b,0x049,0x079,0x030,0x000,0x000 }, 
      {	0x003,0x003,0x071,0x079,0x00f,0x007,0x000,0x000 }, 
      {	0x036,0x07f,0x049,0x049,0x07f,0x036,0x000,0x000 }, 
      {	0x006,0x04f,0x049,0x069,0x03f,0x01e,0x000,0x000 }, 
      {	0x000,0x000,0x066,0x066,0x000,0x000,0x000,0x000 }, 
      {	0x000,0x080,0x0e6,0x066,0x000,0x000,0x000,0x000 }, 
      {	0x008,0x01c,0x036,0x063,0x041,0x000,0x000,0x000 }, 
      {	0x024,0x024,0x024,0x024,0x024,0x024,0x000,0x000 }, 
      {	0x000,0x041,0x063,0x036,0x01c,0x008,0x000,0x000 }, 
      {	0x002,0x003,0x051,0x059,0x00f,0x006,0x000,0x000 }, 
      {	0x03e,0x07f,0x041,0x05d,0x05d,0x01f,0x01e,0x000 }, 
      {	0x07c,0x07e,0x013,0x013,0x07e,0x07c,0x000,0x000 }, 
      {	0x041,0x07f,0x07f,0x049,0x049,0x07f,0x036,0x000 }, 
      {	0x01c,0x03e,0x063,0x041,0x041,0x063,0x022,0x000 }, 
      {	0x041,0x07f,0x07f,0x041,0x063,0x03e,0x01c,0x000 }, 
      {	0x041,0x07f,0x07f,0x049,0x05d,0x041,0x063,0x000 }, 
      {	0x041,0x07f,0x07f,0x049,0x01d,0x001,0x003,0x000 }, 
      {	0x01c,0x03e,0x063,0x041,0x051,0x073,0x072,0x000 }, 
      {	0x07f,0x07f,0x008,0x008,0x07f,0x07f,0x000,0x000 }, 
      {	0x000,0x041,0x07f,0x07f,0x041,0x000,0x000,0x000 }, 
      {	0x030,0x070,0x040,0x041,0x07f,0x03f,0x001,0x000 }, 
      {	0x041,0x07f,0x07f,0x008,0x01c,0x077,0x063,0x000 }, 
      {	0x041,0x07f,0x07f,0x041,0x040,0x060,0x070,0x000 }, 
      {	0x07f,0x07f,0x00e,0x01c,0x00e,0x07f,0x07f,0x000 }, 
      {	0x07f,0x07f,0x006,0x00c,0x018,0x07f,0x07f,0x000 }, 
      {	0x01c,0x03e,0x063,0x041,0x063,0x03e,0x01c,0x000 }, 
      {	0x041,0x07f,0x07f,0x049,0x009,0x00f,0x006,0x000 }, 
      {	0x01e,0x03f,0x021,0x071,0x07f,0x05e,0x000,0x000 }, 
      {	0x041,0x07f,0x07f,0x009,0x019,0x07f,0x066,0x000 }, 
      {	0x026,0x06f,0x04d,0x059,0x073,0x032,0x000,0x000 }, 
      {	0x003,0x041,0x07f,0x07f,0x041,0x003,0x000,0x000 }, 
      {	0x07f,0x07f,0x040,0x040,0x07f,0x07f,0x000,0x000 }, 
      {	0x01f,0x03f,0x060,0x060,0x03f,0x01f,0x000,0x000 }, 
      {	0x07f,0x07f,0x030,0x018,0x030,0x07f,0x07f,0x000 }, 
      {	0x043,0x067,0x03c,0x018,0x03c,0x067,0x043,0x000 }, 
      {	0x007,0x04f,0x078,0x078,0x04f,0x007,0x000,0x000 }, 
      {	0x047,0x063,0x071,0x059,0x04d,0x067,0x073,0x000 }, 
      {	0x000,0x07f,0x07f,0x041,0x041,0x000,0x000,0x000 }, 
      {	0x001,0x003,0x006,0x00c,0x018,0x030,0x060,0x000 }, 
      {	0x000,0x041,0x041,0x07f,0x07f,0x000,0x000,0x000 }, 
      {	0x008,0x00c,0x006,0x003,0x006,0x00c,0x008,0x000 }, 
      {	0x080,0x080,0x080,0x080,0x080,0x080,0x080,0x080 }, 
      {	0x000,0x000,0x003,0x007,0x004,0x000,0x000,0x000 }, 
      {	0x020,0x074,0x054,0x054,0x03c,0x078,0x040,0x000 }, 
      {	0x041,0x07f,0x03f,0x048,0x048,0x078,0x030,0x000 }, 
      {	0x038,0x07c,0x044,0x044,0x06c,0x028,0x000,0x000 }, 
      {	0x030,0x078,0x048,0x049,0x03f,0x07f,0x040,0x000 }, 
      {	0x038,0x07c,0x054,0x054,0x05c,0x018,0x000,0x000 }, 
      {	0x048,0x07e,0x07f,0x049,0x003,0x002,0x000,0x000 }, 
      {	0x098,0x0bc,0x0a4,0x0a4,0x0f8,0x07c,0x004,0x000 }, 
      {	0x041,0x07f,0x07f,0x008,0x004,0x07c,0x078,0x000 }, 
      {	0x000,0x044,0x07d,0x07d,0x040,0x000,0x000,0x000 }, 
      {	0x060,0x0e0,0x080,0x080,0x0fd,0x07d,0x000,0x000 }, 
      {	0x041,0x07f,0x07f,0x010,0x038,0x06c,0x044,0x000 }, 
      {	0x000,0x041,0x07f,0x07f,0x040,0x000,0x000,0x000 }, 
      {	0x07c,0x07c,0x018,0x038,0x01c,0x07c,0x078,0x000 }, 
      {	0x07c,0x07c,0x004,0x004,0x07c,0x078,0x000,0x000 }, 
      {	0x038,0x07c,0x044,0x044,0x07c,0x038,0x000,0x000 }, 
      {	0x084,0x0fc,0x0f8,0x0a4,0x024,0x03c,0x018,0x000 }, 
      {	0x018,0x03c,0x024,0x0a4,0x0f8,0x0fc,0x084,0x000 }, 
      {	0x044,0x07c,0x078,0x04c,0x004,0x01c,0x018,0x000 }, 
      {	0x048,0x05c,0x054,0x054,0x074,0x024,0x000,0x000 }, 
      {	0x000,0x004,0x03e,0x07f,0x044,0x024,0x000,0x000 }, 
      {	0x03c,0x07c,0x040,0x040,0x03c,0x07c,0x040,0x000 }, 
      {	0x01c,0x03c,0x060,0x060,0x03c,0x01c,0x000,0x000 }, 
      {	0x03c,0x07c,0x070,0x038,0x070,0x07c,0x03c,0x000 }, 
      {	0x044,0x06c,0x038,0x010,0x038,0x06c,0x044,0x000 }, 
      {	0x09c,0x0bc,0x0a0,0x0a0,0x0fc,0x07c,0x000,0x000 }, 
      {	0x04c,0x064,0x074,0x05c,0x04c,0x064,0x000,0x000 }, 
      {	0x008,0x008,0x03e,0x077,0x041,0x041,0x000,0x000 }, 
      {	0x000,0x000,0x000,0x077,0x077,0x000,0x000,0x000 }, 
      {	0x041,0x041,0x077,0x03e,0x008,0x008,0x000,0x000 }, 
      {	0x002,0x003,0x001,0x003,0x002,0x003,0x001,0x000 }, 
      {	0x070,0x078,0x04c,0x046,0x04c,0x078,0x070,0x000 }, }; 
 
/*///////////////////////////////////// 
   DOT LCD support routines 
/////////////////////////////////////*/ 
// return  = 0: LCD Ok! 
//        != 0: LCD not existed 
int LCDIsExisted( ) 
{ 
   unsigned char status; 
 
   status = inportb( CmdPort1 ) & 0x0f; 
   if( status != 0 ) return -1; 
   status = inportb( CmdPort0 ) & 0x0f; 
   if( status != 0 ) return -1; 
   return 0; 
} 
 
// return = 0x8000: LCD Controller[] unexisted 
//        = 0x0080: LCD Controller[] existed, Busy 
//        = 0x0020: LCD Controller[] existed, in OFF 
//        = 0x0010: LCD Controller[] existed, in RESET 
//        = 0x0000: LCD Controller[] ready 
//        = 0xffff: wrong input paramter 
unsigned int ControllerStatus( int Idx ) 
{ 
   unsigned char State, ss; 
   unsigned int  CmdPort[2]; 
 
   if( (Idx<0) || (Idx>1) ) return 0xffff; 
 
   CmdPort[0] = CmdPort0; 
   CmdPort[1] = CmdPort1; 
   CmdPort[2] = CmdPort2; 
   State = inportb( CmdPort[Idx] ); 
 
   if( State == 0x00 ) return 0; 
 
   ss = State & 0x80; 
   if( ss != 0 )  return 0x0080;	// in Busy! 
 
   ss = State & 0x20; 
   if( ss != 0 )  return 0x0020;	// in turn off! 
 
   ss = State & 0x10; 
   if( ss != 0 )  return 0x0010;	// in reset! 
 
   return 0x8000;			// unexisted! 
} 
 
// return = 0x8000: LCD unexisted 
//        = 0x0080: LCD existed, Busy 
//        = 0x0020: LCD turn off 
//        = 0x0010: LCD in reset 
//        = 0x0000: LCD ready 
unsigned int CheckStatus( ) 
{ 
   unsigned int  i, status; 
 
   for( i=0; i<3; i++ )    //change 3 
      { 
      status = ControllerStatus( i ); 
      if( status != 0 ) return status; 
      } 
 
   return 0; 
} 
 
// return =  0: ok 
//        = -1: LCD error 
int WaitUntilReady( int Idx ) 
{ 
   unsigned int i, status; //CmdPort[3]; 
 
   //CmdPort[0] = CmdPort0; 
   //CmdPort[1] = CmdPort1; 
   //CmdPort[2] = CmdPort2; 
   for( i=0; i<10000; i++ ) 
      { 
      status = ControllerStatus( Idx ); 
      switch( status ) 
	 { 
	 case 0x8000: 		// LCD unexisted 
	      return -1; 
	 case 0x0020: 		// LCD turn off 
	      //outp( CmdPort[Idx], 0x3f ); 
	 case 0x0000: 		// LCD ready 
	      return 0; 
	 default: 
	      break; 
	 } 
      } 
 
   return -1; 
} 
 
// return =  0: init ok 
//        = -1: init fail 
int InitLCD( ) 
{ 
   int i, k; 
   unsigned int status, CmdPort[3]; 
 
   // wait LCD ready 
   for( i=0; i<11; i++ ) 
      { 
      //DelayCall( 1, Milliseconds ); 
      status = CheckStatus( ); 
      if( status == 0x8000 ) return -1; 
      if( status == 0x0080 ) continue; 
      break; 
      } 
   if( i >= 11 ) return -1; 
 
   CmdPort[0] = CmdPort0; 
   CmdPort[1] = CmdPort1; 
   CmdPort[2] = CmdPort2; 
   for( k=0; k<3; k++ )   //change 3 
      { 
      // turn on LCD 
      if( WaitUntilReady( k ) != 0 ) return -1; 
      outportb( CmdPort[k], 0x3f ); 
 
      // set display start row 
      if( WaitUntilReady( k ) != 0 ) return -1; 
      outportb( CmdPort[k], 0xc0 ); 
 
      // set active page => page 0 
      if( WaitUntilReady( k ) != 0 ) return -1; 
      outportb( CmdPort[k], 0xb8 ); 
 
      // set active column => column 0 
      if( WaitUntilReady( k ) != 0 ) return -1; 
      outportb( CmdPort[k], 0x40 ); 
      } 
 
   return ClearScreen( ); 
} 
 
// return =  0: init ok 
//        = -1: LCD fail 
//        = -2: Not Init yet! 
int ClearScreen( ) 
{ 
   unsigned int  i, PgIdx, k; 
   unsigned int  CmdPort[2], DatPort[2]; 
 
   CmdPort[0] = CmdPort0; 
   DatPort[0] = DatPort0; 
   DatPort[1] = DatPort1; 
   CmdPort[1] = CmdPort1; 
   DatPort[2] = DatPort2; 
   CmdPort[2] = CmdPort2; 
   for( k=0; k<3; k++ ) //change 3 
      { 
      for( PgIdx=0; PgIdx<8; PgIdx++ ) 
	 { 
	 if( WaitUntilReady( k ) != 0 ) return -1; 
	 outportb( CmdPort[k], 0xb8+PgIdx ); 
	 if( WaitUntilReady( k ) != 0 ) return -1; 
	 outportb( CmdPort[k], 0x40 ); 
	 for( i=0; i191) )        return -2;        //change 191 
   if( (PgIdx<0) || (PgIdx>7) )  return -2; 
 
   CmdPort[0] = CmdPort0; 
   DatPort[0] = DatPort0; 
   CmdPort[1] = CmdPort1; 
   DatPort[1] = DatPort1; 
   CmdPort[2] = CmdPort2; 
   DatPort[2] = DatPort2; 
 
   Idx = x / ColBankSize; 
   ColNum = x % ColBankSize; 
 
   // goto (x, y) 
   if( WaitUntilReady( Idx ) != 0 ) return -1; 
   outportb( CmdPort[Idx], 0xb8+PgIdx ); 
   if( WaitUntilReady( Idx ) != 0 ) return -1; 
   outportb( CmdPort[Idx], 0x40+ColNum ); 
 
   // write data to (x, PgIdx) 
   if( WaitUntilReady( Idx ) != 0 ) return -1; 
   outportb( DatPort[Idx], aByte ); 
 
   return 0; 
} 
 
// input x     = 0 -- 191 
//       PgIdx = 0,1,...,7 
// return =  0: ok 
//        = -1: LCD fail 
int ReadByte( int x, int PgIdx ) 
{ 
   unsigned int  Idx, ColNum; 
   unsigned int  CmdPort[3], DatPort[3]; 
   unsigned char abyte; 
 
   CmdPort[0] = CmdPort0; 
   DatPort[0] = DatPort0; 
   CmdPort[1] = CmdPort1; 
   DatPort[1] = DatPort1; 
   CmdPort[2] = CmdPort2; 
   DatPort[2] = DatPort2; 
 
   Idx = x / ColBankSize; 
   ColNum = x % ColBankSize; 
 
   // goto (x, y) 
   if( WaitUntilReady( Idx ) != 0 ) return -1; 
   outportb( CmdPort[Idx], 0xb8+PgIdx ); 
   if( WaitUntilReady( Idx ) != 0 ) return -1; 
   outportb( CmdPort[Idx], 0x40+ColNum ); 
 
   // write data to (x, PgIdx) 
   if( WaitUntilReady( Idx ) != 0 ) return -1; 
   abyte = inportb( DatPort[Idx] ); 
   if( WaitUntilReady( Idx ) != 0 ) return -1;	//MUST BE twice to get 
   abyte = inportb( DatPort[Idx] );		//correct data 
 
   return abyte; 
} 
 
// function: scroll up 1 character row (8 pixel lines) 
// return = new show start line 
unsigned int ScrollUp( unsigned int OldStartLine ) 
{ 
   unsigned int  i, k, CmdPort[3], DatPort[3]; 
 
   CmdPort[0] = CmdPort0; 
   DatPort[0] = DatPort0; 
   CmdPort[1] = CmdPort1; 
   DatPort[1] = DatPort1; 
   CmdPort[2] = CmdPort2; 
   DatPort[2] = DatPort2; 
 
   // clear start row 
   for( k=0; k<3; k++ ) //change 3 
      { 
      if( WaitUntilReady( k ) != 0 ) return -1; 
      outportb( CmdPort[k], 0xb8+(OldStartLine/8) ); 
      if( WaitUntilReady( k ) != 0 ) return -1; 
      outportb( CmdPort[k], 0x40 ); 
      for( i=0; i= 8 ) NewStartLine = OldStartLine - 8; 
   else                    NewStartLine = OldStartLine + 64 - 8; 
 
   // update start line 
   for( k=0; k<3; k++ ) //chang 3 
      { 
      if( WaitUntilReady( k ) != 0 ) return -1; 
      outportb( CmdPort[k], 0xc0+NewStartLine ); 
      } 
 
   // clear start row 
   for( k=0; k<3; k++ ) //change 3 
      { 
      if( WaitUntilReady( k ) != 0 ) return -1; 
      outportb( CmdPort[k], 0xb8+(NewStartLine/8) ); 
      if( WaitUntilReady( k ) != 0 ) return -1; 
      outportb( CmdPort[k], 0x40 ); 
      for( i=0; i=192) )        return -2; 
   if( (PgIdx<0) || (PgIdx>=8) )  return -2; 
 
   CmdPort[0] = CmdPort0; 
   DatPort[0] = DatPort0; 
   CmdPort[1] = CmdPort1; 
   DatPort[1] = DatPort1; 
   CmdPort[2] = CmdPort2; 
   DatPort[2] = DatPort2; 
 
   Idx = x / ColBankSize; 
   ColNum = x % ColBankSize; 
 
   // goto (x, y) 
   if( WaitUntilReady( Idx ) != 0 ) return -1; 
   outportb( CmdPort[Idx], 0xb8+PgIdx ); 
   if( WaitUntilReady( Idx ) != 0 ) return -1; 
   outportb( CmdPort[Idx], 0x40+ColNum ); 
 
   for( i=0; i<8; i++ ) 
      { 
      abyte = VerticalFont[aChar][i]; 
      //abyte = abyte ^ XORByte; 
      if( WaitUntilReady( Idx ) != 0 ) return -1; 
      outportb( DatPort[Idx], abyte ); 
      /* 
      ColNum++; 
      if( ColNum >= 64 ) 
	 { 
	 ColNum = 0; 
	 Idx = (Idx+1)%3; 
	 if( WaitUntilReady( Idx ) != 0 ) return -1; 
	 outportb( CmdPort[Idx], 0xb8+PgIdx ); 
	 if( WaitUntilReady( Idx ) != 0 ) return -1; 
	 outportb( CmdPort[Idx], 0x40+ColNum ); 
	 } 
      */ 
      } 
 
   return 0; 
} 
 
// input x = 0 -- 192 
//       PgIdx = 0,1,..,7 
int  WriteString( int x, int PgIdx, char* aStr, int TxtColor ) 
{ 
   int i, len, status; 
 
   len = strlen( aStr ); 
   for( i=0; i