// tab space = 4
/***********************************************************************/
/*                                                                     */
/*  FILE        :128x128NewHaven_lcd.c                                 */
/*  DATE        :April 8, 2011	                                       */
/*  DESCRIPTION :Functions to drive New Haven 128x128 LCD Display      */
/*                                                                     */
/*                                                                     */
/***********************************************************************/
                  
#include "128x128NewHaven_bsp.h"	// Defines and prototypes
//The next two includes are specific to the microprocessor being used to
//interface with the display
#include "sfr62p.h" 				// Special Function Registers defines for processor		
#include "62P_bsp.h"				// Defines and prototypes for the processor board


//Declare Variables
unsigned char vopcode;
unsigned char Ra_Rb;
int i, col; 
long page;
unsigned char g; //, a;
_far char *m;
char conv_buff[127];
unsigned char col_count;
char char_con[2];

// This list of const char is used to display comman ASCII charaters
const char char_space[] = {0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
const char char_pound[] = {0x07,0x00,0x28,0xFE,0x28,0xFE,0x28,0x00}; 
const char char_0[] = {0x07,0x00,0x7C,0xA2,0x92,0x8A,0x7C,0x00};
const char char_1[] = {0x07,0x00,0x00,0x84,0xFE,0x80,0x00,0x00};
const char char_2[] = {0x07,0x00,0x84,0xC2,0xA2,0x92,0x8C,0x00};
const char char_3[] = {0x07,0x00,0x42,0x82,0x8A,0x96,0x62,0x00};
const char char_4[] = {0x07,0x00,0x30,0x28,0x24,0xFE,0x20,0x00};
const char char_5[] = {0x07,0x00,0x4E,0x8A,0x8A,0x8A,0x72,0x00};
const char char_6[] = {0x07,0x00,0x78,0x94,0x92,0x92,0x60,0x00}; 
const char char_7[] = {0x07,0x00,0x02,0xE2,0x12,0x0A,0x06,0x00};
const char char_8[] = {0x07,0x00,0x6C,0x92,0x92,0x92,0x6C,0x00};
const char char_9[] = {0x07,0x00,0x0C,0x92,0x92,0x52,0x3C,0x00};
const char char_DQ[] = {0x05,0x00,0x0E,0x00,0x0E,0x00};
const char char_SQ[] = {0x04,0x00,0x0A,0x06,0x00};
const char char_COLN[] = {0x04,0x00,0x6C,0x6C,0x00};
const char char_DEG[] = {0x05,0x00,0x0E,0x0A,0x0E,0x00};
const char char_BS[] = {0x07,0x00,0x40,0x20,0x10,0x08,0x04,0x00};
const char char_BLK[] = {0x07,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0x00};
const char char_CHK[] = {0x07,0x00,0x40,0x80,0x60,0x18,0x04,0x00};
const char char_AMP[] = {0x07,0x00,0x64,0x92,0xF2,0x82,0x7C,0x00};
const char char_CLPREN[] = {0x05,0x00,0x82,0x44,0x38,0x00};
const char char_RA[] = {0x07,0x00,0x10,0x10,0x54,0x38,0x10,0x00};
const char char_A[] = {0x07,0x00,0xFC,0x22,0x22,0x22,0xFC,0x00};
const char char_B[] = {0x07,0x00,0xFE,0x92,0x92,0x92,0x6C,0x00};
const char char_C[] = {0x07,0x00,0x7C,0x82,0x82,0x82,0x44,0x00};
const char char_D[] = {0x07,0x00,0xFE,0x82,0x82,0x44,0x38,0x00};
const char char_E[] = {0x07,0x00,0xFE,0x92,0x92,0x92,0x82,0x00};
const char char_F[] = {0x07,0x00,0xFE,0x12,0x12,0x12,0x02,0x00};
const char char_G[] = {0x07,0x00,0x7C,0x82,0x92,0x92,0xF4,0x00};
const char char_H[] = {0x07,0x00,0xFE,0x10,0x10,0x10,0xFE,0x00};
const char char_I[] = {0x07,0x00,0x00,0x82,0xFE,0x82,0x00,0x00};
const char char_J[] = {0x07,0x00,0x40,0x80,0x82,0x7E,0x02,0x00};
const char char_K[] = {0x07,0x00,0xFE,0x10,0x28,0x44,0x82,0x00};
const char char_L[] = {0x07,0x00,0xFE,0x80,0x80,0x80,0x80,0x00};
const char char_M[] = {0x07,0x00,0xFE,0x04,0x18,0x04,0xFE,0x00};
const char char_N[] = {0x07,0x00,0xFE,0x08,0x10,0x20,0xFE,0x00};
const char char_O[] = {0x07,0x00,0x7C,0x82,0x82,0x82,0x7C,0x00};
const char char_P[] = {0x07,0x00,0xFE,0x12,0x12,0x12,0x0C,0x00};
const char char_Q[] = {0x07,0x00,0x7C,0x82,0xA2,0x42,0xBC,0x00};
const char char_R[] = {0x07,0x00,0xFE,0x12,0x32,0x52,0x8C,0x00};
const char char_S[] = {0x07,0x00,0x8C,0x92,0x92,0x92,0x62,0x00};
const char char_T[] = {0x07,0x00,0x02,0x02,0xFE,0x02,0x02,0x00};
const char char_U[] = {0x07,0x00,0x7E,0x80,0x80,0x80,0x7E,0x00};
const char char_V[] = {0x07,0x00,0x3E,0x40,0x80,0x40,0x3E,0x00};
const char char_W[] = {0x07,0x00,0x7E,0x80,0x70,0x80,0x7E,0x00};
const char char_X[] = {0x07,0x00,0xC6,0x28,0x10,0x28,0xC6,0x00};
const char char_Y[] = {0x07,0x00,0x0E,0x10,0xE0,0x10,0x0E,0x00};
const char char_Z[] = {0x07,0x00,0xC2,0xA2,0x92,0x8A,0x86,0x00};
const char char_a[] = {0x07,0x00,0x40,0xA8,0xA8,0xA8,0xF0,0x00};
const char char_b[] = {0x07,0x00,0xFE,0x90,0x88,0x88,0x70,0x00};
const char char_c[] = {0x07,0x00,0x70,0x88,0x88,0x88,0x40,0x00};
const char char_d[] = {0x07,0x00,0x70,0x88,0x88,0x90,0xFE,0x00};
const char char_e[] = {0x07,0x00,0x70,0xA8,0xA8,0xA8,0x30,0x00};
const char char_f[] = {0x07,0x00,0x10,0xfc,0x12,0x02,0x04,0x00};
const char char_g[] = {0x07,0x00,0x18,0xA4,0xA4,0xA4,0x7C,0x00};
const char char_h[] = {0x07,0x00,0xFE,0x10,0x08,0x08,0xF0,0x00};
const char char_i[] = {0x05,0x00,0x88,0xFA,0x80,0x00};
const char char_j[] = {0x06,0x00,0x40,0x80,0x88,0x7A,0x00};
const char char_k[] = {0x06,0x00,0xFE,0x20,0x50,0x88,0x00}; 
const char char_l[] = {0x05,0x00,0x82,0xFE,0x80,0x00};
const char char_m[] = {0x07,0x00,0xF8,0x08,0x30,0x08,0xF0,0x00};
const char char_n[] = {0x07,0x00,0xF8,0x10,0x08,0x08,0xF0,0x00};
const char char_o[] = {0x07,0x00,0x70,0x88,0x88,0x88,0x70,0x00};
const char char_p[] = {0x07,0x00,0xF8,0x28,0x28,0x28,0x10,0x00};
const char char_q[] = {0x07,0x00,0x10,0x28,0x28,0x30,0xF8,0x00};
const char char_r[] = {0x07,0x00,0xF8,0x10,0x08,0x08,0x10,0x00};
const char char_s[] = {0x07,0x00,0x90,0xA8,0xA8,0xA8,0x40,0x00};
const char char_t[] = {0x07,0x00,0x08,0x7E,0x88,0x80,0x40,0x00};
const char char_u[] = {0x07,0x00,0x78,0x80,0x80,0x40,0xF8,0x00};
const char char_v[] = {0x07,0x00,0x38,0x40,0x80,0x40,0x38,0x00};
const char char_w[] = {0x07,0x00,0x78,0x80,0x60,0x80,0x78,0x00};
const char char_x[] = {0x07,0x00,0x88,0x50,0x20,0x50,0x88,0x00};
const char char_y[] = {0x07,0x00,0x18,0xa0,0xa0,0xa0,0x78,0x00};
const char char_z[] = {0x07,0x00,0x88,0xC8,0xA8,0x98,0x88,0x00};
const char char_Exe[] = {0x03,0x00,0x9E,0x00};
const char char_Dol[] = {0x07,0x00,0x48,0x54,0xFE,0x54,0x24,0x00};
const char char_PerC[] = {0x07,0x00,0x46,0x26,0x10,0xC8,0xC4,0x00};
const char char_UpHat[] = {0x07,0x00,0x08,0x04,0x02,0x04,0x08,0x00};
const char char_Amp[] = {0x07,0x00,0x6C,0x92,0xAA,0x44,0x80,0x00};
const char char_Astr[] = {0x07,0x00,0x28,0x10,0x7C,0x10,0x28,0x00};
const char char_OpPren[] = {0x05,0x00,0x38,0x44,0x82,0x00};
const char char_Minus[] = {0x07,0x00,0x10,0x10,0x10,0x10,0x10,0x00};
const char char_UnScr[] = {0x07,0x00,0x80,0x80,0x80,0x80,0x80,0x00};
const char char_Plus[] = {0x07,0x00,0x10,0x10,0x7C,0x10,0x10,0x00};
const char char_Eq[] = {0x07,0x00,0x28,0x28,0x28,0x28,0x28,0x00};
const char char_Period[] = {0x04,0x00,0xC0,0xC0,0x00};
const char char_Ques[] = {0x07,0x00,0x04,0x02,0xA2,0x12,0x0C,0x00};
const char char_GRT[] = {0x06,0x00,0x82,0x44,0x28,0x10,0x00};
const char char_LT[] = {0x06,0x00,0x10,0x28,0x44,0x82,0x00};
const char char_Coma[] = {0x04,0x00,0xA0,0x60,0x00};
const char char_Semi[] = {0x04,0x00,0xAC,0x6C,0x00}; 
const char char_UP_DWN[] = {0x07,0x00,0x24,0x42,0xFF,0x42,0x24,0x00};


/*****************************************************************************
Name:          InitDisplay() 
Parameters:    none                   
Returns:       none 
Description:   Sets up I/O and performs a general hardware reset of the LCD   
*****************************************************************************/
void InitDisplay(void)
{
	Init_LCD_IO();

	WR_PIN = HIGH;					// Set Write pin as high
	RST_PIN = HIGH;					// end reset of display
	vopcode = 37;					//	adjust for contrast
	Ra_Rb=0x27;
	
	return;
}

  
/*****************************************************************************
Name:        	Wait(int time)   
Parameters:  	time		Long delay time
Returns:        none
Description:    This function provides a long pause based on "time"
*****************************************************************************/
void Wait(int time)
{
		unsigned long int i;
		unsigned long int j;
		for (i=0;i<time;i++)
			for (j=0;j<1000;j++)
			{;}
			
}
/*****************************************************************************
Name:        	LCD_write_Cntrl(unsigned char datum)   
Parameters:  	datum		data to be written to command mode of LCD
Returns:        none
Description:    This function writes control codes to LCD 
*****************************************************************************/
void LCD_write_Cntrl(unsigned char datum)
{
		DI_PIN = LOW;				// Select control
		EN_PIN = LOW;				// Enable LCD
		DATA_PORT = datum;		// Ouput control data
		DisplayDelay(0);
		WR_PIN = LOW;				// Enable write
		WR_PIN = HIGH;				// Disable Write
		EN_PIN = HIGH;				// Disable LCD
		return;
}

/*****************************************************************************
Name:        	Show_Display(_far char *lcd_string)   
Parameters:  	*lcd_string	(pointer to data to be displayed)
Returns:        none
Description:    This function displays a full screen image - the _far is 
				needed for the Renesas processor
*****************************************************************************/
void Show_Display(_far char *lcd_string, int nor_inv)
{
	unsigned char local_page;
	unsigned char col;
	
	for (local_page=0xb0;local_page < 0xc0; local_page++)
		{
			LCD_write_Cntrl(local_page);
			LCD_write_COLUMN(0);
			for(col=0;col<128;col++)
				{
					LCD_write_Data(*lcd_string++,nor_inv);
				}
		}
	return;
}

/*****************************************************************************
Name:        	PosSetUp()   
Parameters:  	page		Page to write on (P_0 - P_15) Pages are lines 0-15
			    column		Column to be written on display. 
Returns:        none
Description:    This function sets a write position on the LCD
*****************************************************************************/
void PosSetUp(unsigned char Page, unsigned char column)
{
		Page = 0xB0 + Page;				// convert page to control code page
		LCD_write_Cntrl(Page);			// write page to display
		LCD_write_COLUMN(column);		// write the column code to the display
		return;
}
	
/*****************************************************************************
Name:        	DisplayString()   
Parameters:  	position	Start Position on Line (Column0-127)
			    value		Character to be written to display.
				nor_inv		NORMAL or INVERTED  display
Returns:        none
Description:    This function controls LCD writes to the LCD.
                This function only controls the column to start at - page has 
				be setup before this function.  Use PosSetUp()
*****************************************************************************/
void DisplayString(unsigned char Start_Col, _far char *value, int nor_inv)
{
		
	int k =0; 								
	int j = 0;							
	int a, b;
	while (value[k] != NULL){
		char_conv(value + k);				//upon return m has the address of the bitmap of the character
		a = *m;								//get the count for colums for character
		for (b = 0;b != a;b++){				//output the data for each column of the character
			conv_buff[j++] = *(m+1+b);
			if(j > 127-1){
				conv_buff[j-1] = NULL;
				break;
			}
		}
 	k++;
	}
	OutputData(Start_Col,j, nor_inv);
	return;
}
/*****************************************************************************
Name:        	DisplayChar()
Parameters:  	position	Start Position on Line (Column0-127)
			    value		Single character to be written to display.
Returns:        none
Description:    This function writes a single ASCII chatacter to the LCD display.
				Page must be set up before calling with PosSetUp()
                
*****************************************************************************/
void DisplayChar(unsigned char Start_Col, char value, int nor_inv)
{
	char  n;								//buffer to return bit map data 
	unsigned char k =0;
	int j = 0;
	unsigned char b;

	char_con[0] = value;
	char_con[1] = NULL;
	while (char_con[k] != NULL){
		char_conv(char_con + k);			//upon return m has the address of the bitmap of the character
		col_count = *m;						//get the count for columns for character
		for (b = 0;b != col_count;b++){
			conv_buff[j++] = *(m+1+b);
		}
 	k++;
	}

	OutputData(Start_Col,j,nor_inv);
	return;
}

/*****************************************************************************
Name:        	OutputData()   
Parameters:  	position	Start Position on Line (Column0-127)
			    value		Single character to be written to display.
Returns:        none
Description:    This function outputs data to be displayed to the LCD
*****************************************************************************/
void OutputData(unsigned char Start_Col,int j,int nor_inv)
{
		unsigned char a,pass;
		
		col = Start_Col;					// set the starting column
		LCD_write_COLUMN(Start_Col);
		for( i=0;i<j;i++){					// point to data to output
			if (col == 128) {				// has the column count reached 128?
				LCD_write_COLUMN(0);
				LCD_write_Cntrl(++page);	
				col = 1;
			}
			g = conv_buff[i];
			DI_PIN = HIGH;
		
	/* Check for normal (1) or inverted (0), then write data byte to output */
			if (nor_inv == INVERTED){
				a = (char)((0xff) ^(g));
			}
			else {
				DisplayDelay(0);
				a = (char) g;				// Output data
				DisplayDelay(0);
			}
			for(pass=0;pass<4;pass++){
   				EN_PIN = LOW;          			// Enable chip
				DATA_PORT = a;					// Output Data
				DisplayDelay(0);
				WR_PIN = LOW;					// Set to Write
				WR_PIN = HIGH;					// Set to Write
   				EN_PIN = HIGH;          		// Disable chip (HIGH)
			}
		}		
		return;
}

/*****************************************************************************
Name:           LCD_write_Data()
Parameters:     value - the value to write

				nor_inv - normal display (dark characters on a white background
							or inverted display (light characters on a dark
							background
								1 = Normal
								0 = Inverted
Returns:        none
Description:    Writes data to display.   
*****************************************************************************/
void LCD_write_Data(unsigned char value, int nor_inv)
{
		unsigned char a, pass;
	
   		DI_PIN = 1;
		if (nor_inv == INVERTED){
			a = (char)((0xff) ^(value));
		}
		else {
			a = (char) value;			
		}
		for(pass = 0; pass<4;pass++){
 	  		EN_PIN = LOW;          					// Enable LCD
			DATA_PORT = a;							// Output data
			DisplayDelay(0);
			WR_PIN = LOW;							// Enable Write
			WR_PIN = HIGH;							// Disable Write
   			EN_PIN = HIGH;          				// Disable chip
		}
	return;
}

/*****************************************************************************
Name:           LCD_write_COLUMN()
Parameters:     value - the value to write

Returns:        none
Description:    Sets the location (column for the next write to display  
*****************************************************************************/
void LCD_write_COLUMN(long value)
{
	int a;
	
		a = value >> 4;							//shift upper nibble 4 places to the right
		a = (a & 0x0F) + 0x10;					//mask off upper nibble
		DI_PIN = LOW;
		EN_PIN = LOW;          					// Enable LCD
		DATA_PORT = (char) a;					// Output data	
		DisplayDelay(0);
		WR_PIN = LOW;							// Enable Write
		WR_PIN = HIGH;							// Disable Write
		EN_PIN = HIGH;          				// Disable LCD
		a = value & 0x0F;						// Mask off upper nibble
		EN_PIN = LOW;          					// Enable LCD
		DATA_PORT = (char) a;					// Output data	
		DisplayDelay(0);
		WR_PIN = LOW;							// Enable Write
		WR_PIN = HIGH;							// Diasble Write
		EN_PIN = HIGH;          				// Disable LCD
	return;
}

/*****************************************************************************
Name:          clear_display()
Parameters:    start line (0-15) and end line (0-15)                   
Returns:       none 
Description:   Routine to clear the LCD display.  
				To clear the entire display	 - clear_display(0,15).  
				i.e. To just clear lines 4 and 5 - clear_display(4,5) 
*****************************************************************************/
void Clear_Display(int start, int end)
{
	int page;
	int col;
	
	if (end >15)							// end page cannot be more than 15
		end = 15;
	if (start > end)						// start page cannot be less than end page
		start = end;
	page = start;							// set the start page
	col = 0;								// set the column counter to 0
	PosSetUp(page, col);
	do{
		if (col == 128) {					// has the column count reacted 128?
			col = 0;						// if so then set col counter to 0
			page++;							// increment page counter
			if (page  > end)				// if gone past last page, break
				break;
			else 
			PosSetUp(page, col);	// else point LCD to next page, col

		}
		LCD_write_Data(SPACE, NORMAL);		// write a space to current LCD location
		col++;								// increment column counter
	}while (page < end+1);					// as long as we have not gone past the end page
	
	return;	
}

/*****************************************************************************
Name:           Display_Grapic()
Parameters:     pointer to string which contains formatting info and data.
				The data is 8 pixels high, with the LSB at the top and the MSB
				at the bottom of the column of 8 pixels.  This example is of a
				pirate ship and requires 4 lines (each 8 pixels high) and is 40
				pixels (columns) wide.
const char pirate_ship [] =
{
	0x04,									// number of lines of data
	12,60,40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	        0x00,0x00,0x00,0x00,0x00,0x00,0xc0,0xc0,
			0xc0,0x40,0xc0,0xc0,0xc0,0xc0,0xc0,0x40,
			0xc0,0xc0,0xc0,0xc0,0xc0,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	
	13,60,40,0x00,0x00,0xc0,0xc0,0xc0,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x7f,
			0x7f,0x5f,0x6e,0x75,0x7b,0xf5,0x6e,0x5f,
			0x7f,0x7f,0x7f,0x7f,0x7f,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	14,60,40,0x00,0x00,0x00,0x00,0x3f,0xc2,0x0a,0x02,
			0x0a,0x02,0x4a,0x02,0x04,0x48,0x10,0x10,	
			0x50,0x10,0x10,0x50,0x10,0x1f,0x50,0x10,
			0x10,0x50,0x10,0x10,0x50,0x10,0x90,0x50,
			0x30,0x10,0x08,0x04,0x00,0x00,0x00,0x00,
	15,60,40,0x02,0x02,0x02,0x02,0x02,0x02,0x03,0x02,
			0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
			0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
			0x02,0x02,0x02,0x02,0x02,0x03,0x02,0x02,
			0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02
};
					 
Returns:        none
Description:    Displays graphics anywhere on the display  
*****************************************************************************/
void Display_Graphic(_far char *lcd_string)
{
	int x,y,z,a,b,c,d;
	x = z = 0;
	y = lcd_string[x++];		// number of lines
	do{
		a = lcd_string[x++];		//get page number
		b = lcd_string[x++];		//get start column number
	PosSetUp(a,b);
		c = lcd_string[x++];		//get number of data bytes
		for (d = 0;d<c;d++)
		{
			LCD_write_Data(lcd_string[x++],NORMAL);		
		}
		z++;	
	}	
	while(z <y);

	return;
}

/*****************************************************************************
Name:           LCD_Init()
Parameters:     nonw
Returns:        none
Description:    Initilizes the display.  
*****************************************************************************/
void LCD_Init(void){
        LCD_write_Cntrl(0xA2);        //ICON  OFF;
        LCD_write_Cntrl(0xAE);        //Display OFF

        LCD_write_Cntrl(0x48);        //Set Duty ratio
        LCD_write_Cntrl(0x80);        //No operation
        LCD_write_Cntrl(0xa0);        //Set scan direction
        LCD_write_Cntrl(0xc8);        //SHL  select
        LCD_write_Cntrl(0x40);        //Set START LINE
        LCD_write_Cntrl(0x00);
        LCD_write_Cntrl(0xab);        //OSC on

        LCD_write_Cntrl(0x64);        //3x
        DisplayDelay(20);
        LCD_write_Cntrl(0x65);        //4x
        DisplayDelay(20);
        LCD_write_Cntrl(0x66);        //5x
        DisplayDelay(20);
        LCD_write_Cntrl(0x67);        //6x
        DisplayDelay(20);

        LCD_write_Cntrl(Ra_Rb);       //RESISTER SET
        LCD_write_Cntrl(0x81);        //Set electronic volume register
        LCD_write_Cntrl(vopcode);     //n=0~3f


        LCD_write_Cntrl(0x57);        //1/12bias
        LCD_write_Cntrl(0x92);        //FRC and pwm


        LCD_write_Cntrl(0x2C);        
        DisplayDelay(200);				//200ms
        LCD_write_Cntrl(0x2E);        
        DisplayDelay(200);				//200ms
        LCD_write_Cntrl(0x2F);        
        DisplayDelay(200);				//200ms


        LCD_write_Cntrl(0x92);        //frc and pwm
        LCD_write_Cntrl(0x38);        //external  mode
        LCD_write_Cntrl(0x75);        
	//start settings for 16-level grayscale
        LCD_write_Cntrl(0x97);        //3frc,45pwm

        LCD_write_Cntrl(0x80);
        LCD_write_Cntrl(0x00);
        LCD_write_Cntrl(0x81);
        LCD_write_Cntrl(0x00);
        LCD_write_Cntrl(0x82);
        LCD_write_Cntrl(0x00);
        LCD_write_Cntrl(0x83);
        LCD_write_Cntrl(0x00);

        LCD_write_Cntrl(0x84);
        LCD_write_Cntrl(0x06);
        LCD_write_Cntrl(0x85);
        LCD_write_Cntrl(0x06);
        LCD_write_Cntrl(0x86);
        LCD_write_Cntrl(0x06);
        LCD_write_Cntrl(0x87);
        LCD_write_Cntrl(0x06);

        LCD_write_Cntrl(0x88);
        LCD_write_Cntrl(0x0b);
        LCD_write_Cntrl(0x89);
        LCD_write_Cntrl(0x0b);
        LCD_write_Cntrl(0x8a);
        LCD_write_Cntrl(0x0b);
        LCD_write_Cntrl(0x8b);
        LCD_write_Cntrl(0x0b);

        LCD_write_Cntrl(0x8c);
        LCD_write_Cntrl(0x10);
        LCD_write_Cntrl(0x8d);
        LCD_write_Cntrl(0x10);
        LCD_write_Cntrl(0x8e);
        LCD_write_Cntrl(0x10);
        LCD_write_Cntrl(0x8f);
        LCD_write_Cntrl(0x10);

        LCD_write_Cntrl(0x90);
        LCD_write_Cntrl(0x15);
        LCD_write_Cntrl(0x91);
        LCD_write_Cntrl(0x15);
        LCD_write_Cntrl(0x92);
        LCD_write_Cntrl(0x15);
        LCD_write_Cntrl(0x93);
        LCD_write_Cntrl(0x15);

        LCD_write_Cntrl(0x94);
        LCD_write_Cntrl(0x1a);
        LCD_write_Cntrl(0x95);
        LCD_write_Cntrl(0x1a);
        LCD_write_Cntrl(0x96);
        LCD_write_Cntrl(0x1a);
        LCD_write_Cntrl(0x97);
        LCD_write_Cntrl(0x1a);

        LCD_write_Cntrl(0x98);
        LCD_write_Cntrl(0x1e);
        LCD_write_Cntrl(0x99);
        LCD_write_Cntrl(0x1e);
        LCD_write_Cntrl(0x9a);
        LCD_write_Cntrl(0x1e);
        LCD_write_Cntrl(0x9b);
        LCD_write_Cntrl(0x1e);

        LCD_write_Cntrl(0x9c);
        LCD_write_Cntrl(0x23);
        LCD_write_Cntrl(0x9d);
        LCD_write_Cntrl(0x23);
        LCD_write_Cntrl(0x9e);
        LCD_write_Cntrl(0x23);
        LCD_write_Cntrl(0x9f);
        LCD_write_Cntrl(0x23);

        LCD_write_Cntrl(0xa0);
        LCD_write_Cntrl(0x27);
        LCD_write_Cntrl(0xa1);
        LCD_write_Cntrl(0x27);
        LCD_write_Cntrl(0xa2);
        LCD_write_Cntrl(0x27);
        LCD_write_Cntrl(0xa3);
        LCD_write_Cntrl(0x27);

        LCD_write_Cntrl(0xa4);
        LCD_write_Cntrl(0x2b);
        LCD_write_Cntrl(0xa5);
        LCD_write_Cntrl(0x2b);
        LCD_write_Cntrl(0xa6);
        LCD_write_Cntrl(0x2b);
        LCD_write_Cntrl(0xa7);
        LCD_write_Cntrl(0x2b);

        LCD_write_Cntrl(0xa8);
        LCD_write_Cntrl(0x2f);
        LCD_write_Cntrl(0xa9);
        LCD_write_Cntrl(0x2f);
        LCD_write_Cntrl(0xaa);
        LCD_write_Cntrl(0x2f);
        LCD_write_Cntrl(0xab);
        LCD_write_Cntrl(0x2f);

        LCD_write_Cntrl(0xac);
        LCD_write_Cntrl(0x32);
        LCD_write_Cntrl(0xad);
        LCD_write_Cntrl(0x32);
        LCD_write_Cntrl(0xae);
        LCD_write_Cntrl(0x32);
        LCD_write_Cntrl(0xaf);
        LCD_write_Cntrl(0x32);

        LCD_write_Cntrl(0xb0);
        LCD_write_Cntrl(0x35);
        LCD_write_Cntrl(0xb1);
        LCD_write_Cntrl(0x35);
        LCD_write_Cntrl(0xb2);
        LCD_write_Cntrl(0x35);
        LCD_write_Cntrl(0xb3);
        LCD_write_Cntrl(0x35);

        LCD_write_Cntrl(0xb4);
        LCD_write_Cntrl(0x38);
        LCD_write_Cntrl(0xb5);
        LCD_write_Cntrl(0x38);
        LCD_write_Cntrl(0xb6);
        LCD_write_Cntrl(0x38);
        LCD_write_Cntrl(0xb7);
        LCD_write_Cntrl(0x38);

        LCD_write_Cntrl(0xb8);
        LCD_write_Cntrl(0x3a);
        LCD_write_Cntrl(0xb9);
        LCD_write_Cntrl(0x3a);
        LCD_write_Cntrl(0xba);
        LCD_write_Cntrl(0x3a);
        LCD_write_Cntrl(0xbb);
        LCD_write_Cntrl(0x3a);

        LCD_write_Cntrl(0xbc);
        LCD_write_Cntrl(0x3c);
        LCD_write_Cntrl(0xbd);
        LCD_write_Cntrl(0x3c);
        LCD_write_Cntrl(0xbe);
        LCD_write_Cntrl(0x3c);
        LCD_write_Cntrl(0xbf);
        LCD_write_Cntrl(0x3c);
	//end settings for 16-level grayscale
        LCD_write_Cntrl(0x38);
        LCD_write_Cntrl(0x74);     
        LCD_write_Cntrl(0xaf);     //Display ON

	return;
}

/*****************************************************************************
Name:          char_conv() 
Parameters:    ASCII Character to convert to bit map                   
Returns:       pointer to string 
Description:   Converts ASCII into bitmap for LCD display   
*****************************************************************************/

_far char * char_conv( _far char *conv_strng)
{
	switch(*conv_strng){ 
		case ' ':
			m = char_space;
			break;
		case '#':
			m = char_pound;
			break;
		case '0':
			m = char_0;
			break;
		case '1':
			m = char_1;
			break;
		case '2':
			m = char_2;
			break;
		case '3':
			m = char_3;
			break;
		case '4':
			m = char_4;
			break;
		case '5':
			m = char_5;
			break;
		case '6':
			m = char_6;
			break;
		case '7':
			m = char_7;
			break;
		case '8':
			m = char_8;
			break;
		case '9':
			m = char_9;
			break;
		case 0x22 :						// " symbol
			m = char_DQ;
			break;
		case 0x27:
			m = char_SQ;				// ' symbol
			break;
		case ':':
			m = char_COLN;
			break;
		case 's':
			m = char_s;
			break;
		case 0xDF:
			m = char_DEG; 				//degree symbol
			break;
		case '/':
			m = char_BS;
			break;
		case 0xFF:
			m = char_BLK; 				//block symbol
			break;
		case 0x02:
			m = char_CHK; 				//check mark
			break;
		case '@':
			m = char_AMP;
			break;
		case 0x7E:
			m = char_RA; 				//invert video (right arrow)
			break;
		case 'A':
			m = char_A;
			break;
		case 'B':
			m = char_B;
			break;
		case 'C':
			m = char_C;
			break;
		case 'D':
			m = char_D;
			break;
		case 'E':
			m = char_E;
			break;
		case 'F':
			m = char_F;
			break;
		case 'G':
			m = char_G;
			break;
		case 'H':
			m = char_H;
			break;
		case 'I':
			m = char_I;
			break;
		case 'J':
			m = char_J;
			break;
		case 'K':
			m = char_K;
			break;
		case 'L':
			m = char_L;
			break;
		case 'M':
			m = char_M;
			break;
		case 'N':
			m = char_N;
			break;
		case 'O':
			m = char_O;
			break;
		case 'P':
			m = char_P;
			break;
		case 'Q':
			m = char_Q;
			break;
		case 'R':
			m = char_R;
			break;
		case 'S':
			m = char_S;
			break;
		case 'T':
			m = char_T;
			break;
		case 'U':
			m = char_U;
			break;
		case 'V':
			m = char_V;
			break;
		case 'W':
			m = char_W;
			break;
		case 'X':
			m = char_X;
			break;
		case 'Y':
			m = char_Y;
			break;
		case 'Z':
			m = char_Z;
			break;
		case 'a':
			m = char_a;
			break;
		case 'b':
			m = char_b;
			break;
		case 'c':
			m = char_c;
			break;
		case 'd':
			m = char_d;
			break;
		case 'e':
			m = char_e;
			break;
		case 'f':
			m = char_f;
			break;
		case 'g':
			m = char_g;
			break;
		case 'h':
			m = char_h;
			break;
		case 'i':
			m = char_i;
			break;
		case 'j':
			m = char_j;
			break;
		case 'k':
			m = char_k;
			break;
		case 'l':
			m = char_l;
			break;
		case 'm':
			m = char_m;
			break;
		case 'n':
			m = char_n;
			break;
		case 'o':
			m = char_o;
			break;
		case 'p':
			m = char_p;
			break;
		case 'q':
			m = char_q;
			break;
		case 'r':
			m = char_r;
			break;
//		case 's':
//			m = char_s; 					//defined earlier
//			break;
		case 't':
			m = char_t;
			break;
		case 'u':
			m = char_u;
			break;
		case 'v':
			m = char_v;
			break;
		case 'w':
			m = char_w;
			break;
		case 'x':
			m = char_x;
			break;
		case 'y':
			m = char_y;
			break;
		case 'z':
			m = char_z;
			break;
		case '!':
			m = char_Exe;
			break;
		case '$':
			m = char_Dol;
			break;
		case '%':
			m = char_PerC;
			break;
		case '^':
			m = char_UpHat;
			break;
		case '&':
			m = char_Amp;
			break;
		case '*':
			m = char_Astr;
			break;
		case '(':
			m = char_OpPren;
			break;
		case ')':
			m = char_CLPREN;
			break;
		case '-':
			m = char_Minus;
			break;
		case '_':
			m = char_UnScr;
			break;
		case '=':
			m = char_Eq;
			break;
		case '+':
			m = char_Plus;
			break;
		case '.':
			m = char_Period;
			break;
		case '?':
			m = char_Ques;
			break;
		case '>':
			m = char_GRT;
			break;
		case '<':
			m = char_LT;
			break;
		case ',':
			m = char_Coma;
			break;
		case ';':
			m = char_Semi;
			break;
		case 0x84:
			m = char_UP_DWN;
	}
	return(m);
}

/*****************************************************************************
Name:          Init_LCD_IO() 
Parameters:    None
Returns:       None
Description:   Sets up I/O ports for LCD Display   
		These commands are specific to the microprocessor used.  They are placed
		seperate so to ease the transistion to another processor.
*****************************************************************************/

void Init_LCD_IO(void)
{
	// Setup for a Renesas M16C/M62P  M30622F8P Processor
	prc2 = HIGH;								// Enable writes to protected registers

	DATA_PORT = DATA_PORT_DDR_VALUE_OUT; 		// output 0xff to dataport
	prc2 = HIGH;								// Enable wirtes to protected registers
	DATA_PORT_DDR = DATA_PORT_DDR_VALUE_OUT;	// setup data port of output
	
	EN_PIN_DDR = OUTPUT;						// Set Enable pin to output
	EN_PIN = HIGH;								// Set Enable pin to high
	RST_PIN_DDR = OUTPUT;						// Set Reset pin to output
	RST_PIN = HIGH;								// Set Reset pin to high
	DisplayDelay(20);							// 20ms delay
	RST_PIN = LOW;								// Set Reset to low
	DSP_PWM_DDR	= OUTPUT;						// Set display backlight PWM to output
	DSP_PWM	= HIGH;								// Set display backlight to high
	

	DI_DDR = OUTPUT;							// Set Data/Instruction pin to output
	DI_PIN=HIGH;								// Set D/I pin as high
	WR_PIN_DDR = OUTPUT;						// Set Write pin to output
	
	return;

}


/*****************************************************************************
Name:          DisplayDelay() 
Parameters:    1ms  = 1  10ms = 10                   
Returns:       none 
Description:   DisplayDelay routine for LCD display.   
*****************************************************************************/
void DisplayDelay(unsigned long int units){

	unsigned long int counter = units * 0x100;
	while(counter--){
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
		_asm ("NOP");
	}
	return;
}
