#pragma num_alias_table_entries	14
#pragma enable_multiple_baud
#include 

IO_0 output bitshift numbits(8) clockedge(+) io_com_data;
IO_2 output bit en;

IO_5 input bit up;
IO_6 input bit down;
IO_7 input bit enter;
IO_8 input bit esc;

eeprom int item = 1;
int cur_pos = 20;
int mnu_pos = 1;
int con_sta = 1;
int pge_sta = 1;
char display[20];

void writecmd(int a)
{
	io_out(en, 1);
	io_out(io_com_data, 0b00000000UL);
	io_out(io_com_data, a);
	io_out(en, 0);
	delay(0);
}

void command(int a)
{
	io_out(en, 1);
	io_out(io_com_data, 0b00000000UL);
	switch(a)
	{
		case 0:
			io_out(io_com_data, 0b00000001UL);	//clear
			break;
		case 1:
			io_out(io_com_data, 0b00111000UL);	//datainterface
			break;
		case 20:
			io_out(io_com_data, 0b00001000UL);	//displayoff
			break;
		case 21:
			io_out(io_com_data, 0b00001100UL);	//displayon
			break;
		case 30:
			io_out(io_com_data, 0b00000100UL);	//autoforward off
			break;
		case 31:
			io_out(io_com_data, 0b00000110UL);	//autoforward on
			break;
	}
	io_out(en, 0);
	if(a == 2)
	{
		delay(63);
	}
	else
	{
		delay(0);
	}
}

void chr(char a)
{
	io_out(en, 1);
	io_out(io_com_data, 0b10000000UL);
	io_out(io_com_data, a);
	io_out(en, 0);
	delay(0);
}

void str(char a[])
{
	int chr_num;
	for(chr_num = 0; a[chr_num] != '~'; chr_num = chr_num + 1)
	{
		chr(a[chr_num]);
	}
}

void syb(int a)	//symbol
{
	switch(a)
	{
		case 0:
			chr(0b01111110UL);		//symbol ->
			break;
		case 1:
			chr(0b00111111UL);		//symbol ?
			break;
		case 2:
			chr(0b00100001UL);		//symbol !
			break;
	}
}

void posit(int a)	//cursor position
{
	switch(a)
	{
		case 1:
			writecmd(0b11000110UL);	//cursor go to central of line 2
			break;
		case 2:
			writecmd(0b10010101UL);	//cursor go to central of line 3
			break;
		case 3:
			writecmd(0b11000010UL);	//cursor go to third bit of line 2
			break;
		case 4:
			writecmd(0b10010110UL);	//cursor go to third bit of line 3
			break;
		case 5:
			writecmd(0b11000011UL);	//"Control Page" location
			break;
		case 6:
			writecmd(0b10010111UL);	//"Config Page" location
			break;
		case 10:
			writecmd(0b10000000UL);	//cursor go to first bit of line 1
			break;
		case 11:
			writecmd(0b10000001UL);	//cursor go to second bit of line 1
			break;
		case 20:
			writecmd(0b11000000UL);	//cursor go to first bit of line 2
			break;
		case 21:
			writecmd(0b11000001UL);	//cursor go to second bit of line 2
			break;
		case 30:
			writecmd(0b10010100UL);	//cursor go to first bit of line 3
			break;
		case 31:
			writecmd(0b10010101UL);	//cursor go to second bit of line 3
			break;
		case 40:
			writecmd(0b11010100UL);	//cursor go to first bit of line 4
			break;
		case 41:
			writecmd(0b11010101UL);	//cursor go to second bit of line 4
			break;
	}
}

void name(int a)
{
	switch(a)
	{
		case 1:
			strcpy(display, "71301-3D~");
			str(display);
			break;
		case 2:
			strcpy(display, "Final Year Project~");
			str(display);
			break;
		case 3:
			strcpy(display, "Control~");
			str(display);
			break;
		case 4:
			strcpy(display, "Config~");
			str(display);
			break;
		case 5:
			strcpy(display, "Control Page~");
			str(display);
			break;
		case 6:
			strcpy(display, "Config Page~");
			str(display);
			break;
		case 7:
			strcpy(display, "Disable this item?~");
			str(display);
			break;
		case 8:
			strcpy(display, "Yes~");
			str(display);
			break;
		case 9:
			strcpy(display, "No~");
			str(display);
			break;
		case 10:
			strcpy(display, "Item is disabled~");
			str(display);
			break;
		case 101:
			strcpy(display, "Lighting~");
			str(display);
			break;
		case 102:
			strcpy(display, "Air Conditioner~");
			str(display);
			break;
		case 103:
			strcpy(display, "Menu 3~");
			str(display);
			break;
		case 104:
			strcpy(display, "Menu 4~");
			str(display);
			break;
		case 105:
			strcpy(display, "Menu 5~");
			str(display);
			break;
	}
}

void menu(int a, int b)	//a:page state, b:menu position
{
	if(a == 0)
	{
		command(0);
		posit(5);
		name(5);
		posit(6);
		name(6);
		if(con_sta == 0)
		{	
			posit(4);
		}
		else
		{
			posit(3);
		}
		syb(0);
	}
	else if(a == 1)
	{
		command(0);
		posit(10);
		if(con_sta == 0)
		{
			name(4);
		}
		if(con_sta == 1)
		{
			name(3);
		}
		if(b < 10)
		{
			b = b + 100;
			posit(21);
			name(b);
			posit(31);
			name(b + 1);
			posit(41);
			name(b + 2);
		}
	}
	else if(a == 98)
	{
		command(0);
		posit(3);
		name(10);
		delay(20000);
	}
	else if(a == 99)
	{
		command(0);
		posit(10);
		name(7);
		posit(21);
		name(8);
		posit(31);
		name(9);
		posit(20);
		syb(0);
	}
}

void roll_cur(int a, int b)	//a:page state, b:up/down
{
	if(a == 0)
	{
		if(b == 1)
		{
			if(cur_pos == 4)
			{
				posit(4);
				chr(' ');
				posit(3);
				syb(0);
				cur_pos = 3;
			}
		}
		else
		{
			if(cur_pos == 3)
			{
				posit(3);
				chr(' ');
				posit(4);
				syb(0);
				cur_pos = 4;
			}
		}
	}
	else if(a == 1)
	{
		if(b == 1)
		{
			if(cur_pos > 20)
			{
				posit(cur_pos);
				chr(' ');
				posit(cur_pos - 10);
				syb(0);
				cur_pos = cur_pos - 10;
			}
			else
			{
				posit(cur_pos);
				syb(0);
			}
		}
		else if(b == 2)
		{
			if(cur_pos < 40)
			{
				posit(cur_pos);
				chr(' ');
				posit(cur_pos + 10);
				syb(0);
				cur_pos = cur_pos + 10;
			}
			else
			{
				posit(cur_pos);
				syb(0);
			}
		}
	}
	else if(a == 99)
	{
		if(b == 1)
		{
			posit(30);
			chr(' ');
			posit(20);
			syb(0);
			cur_pos = 20;
		}
		else if(b == 2)
		{
			posit(20);
			chr(' ');
			posit(30);
			syb(0);
			cur_pos = 30;
		}
	}
}

void roll_menu(int a, int b)	//a:page state, b:up/down
{
	if(a == 1)
	{
		if(b == 1)
		{
			if(cur_pos == 20)
			{
				if(mnu_pos > 1)
				{
					mnu_pos = mnu_pos - 1;
				}
				menu(1, mnu_pos);
			}
		}
		else if(b == 2)
		{
			if(cur_pos == 40)
			{
				if(mnu_pos < 3)
				{
					mnu_pos = mnu_pos + 1;
				}
				menu(1, mnu_pos);
			}
		}
	}
}

when(reset)
{
	delay(600);			//delay 15ms
	command(1);			//datainterface 1
	delay(160);			//delay 4.1ms
	command(1);			//datainterface 2
	delay(2);			//delay 100us
	command(1);			//datainterface 3
	delay(160);			//delay 4.1ms
	command(1);			//datainterface 4
	command(20);		//displayoff
	command(0);			//clear
	command(21);		//displayon
	posit(1);			//point to central at line 2
	name(1);			//"71301-3D"
	posit(2);			//point to central at line 3
	name(2);			//"Final Year Project"
	delay(33333);
	command(0);			//clear
	menu(1, 1);
	posit(20);			//point to bit 1 at line 2
	syb(0);				//display cursor
}

when(io_changes(up)to 0)
{
	if(pge_sta == 0)
	{
		roll_cur(pge_sta, 1);	
	}
	else if(pge_sta == 1)
	{
		roll_menu(pge_sta, 1);
		roll_cur(pge_sta, 1);
	}
	else if(pge_sta == 99)
	{
		roll_cur(pge_sta, 1);
	}
}

when(io_changes(down)to 0)
{
	if(pge_sta == 0)
	{
		roll_cur(pge_sta, 2);	
	}
	else if(pge_sta == 1)
	{
		roll_menu(pge_sta, 2);
		roll_cur(pge_sta, 2);
	}
	else if(pge_sta == 99)
	{
		roll_cur(pge_sta, 2);
	}
}

when(io_changes(enter)to 0)
{
	if(pge_sta == 0)
	{
		if(cur_pos == 3)
		{
			con_sta = 1;
			pge_sta = 1;
			cur_pos = 20;
			menu(pge_sta, 1);
			posit(20);
			syb(0);
		}
		else if(cur_pos == 4)
		{
			con_sta = 0;
			pge_sta = 1;
			cur_pos = 20;
			menu(pge_sta, 1);
			posit(20);
			syb(0);
		}
	}
	else if(con_sta == 0 && pge_sta == 1)
	{
		pge_sta = 99;
		menu(pge_sta, 0);
	}
	else if(con_sta == 0 && pge_sta == 99)
	{
		if(cur_pos == 20)
		{
			pge_sta = 98;
			menu(pge_sta, 1);
			pge_sta =1;
			cur_pos =20;
			menu(pge_sta, 1);
			posit(20);
			syb(0);
		}
		else if(cur_pos == 30)
		{
			pge_sta = 1;
			cur_pos = 20;
			menu(pge_sta, 1);
			posit(20);
			syb(0);
		}
	}
}

when(io_changes(esc)to 0)
{
	if(pge_sta == 1)
	{
		pge_sta = 0;
		menu(pge_sta, 1);
		if(con_sta == 1)
		{
			cur_pos = 3;
		}
		else if(con_sta == 0)
		{
			cur_pos = 4;
		}
	}
}

    Source: geocities.com/hk/cloud_fan_school/Program/LCD/20031105

               ( geocities.com/hk/cloud_fan_school/Program/LCD)                   ( geocities.com/hk/cloud_fan_school/Program)                   ( geocities.com/hk/cloud_fan_school)                   ( geocities.com/hk)