#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;

int item = 1;
int cur_pos = 2;
char welcom1[20], welcom2[20], header1[10], header2[10];
char menu1[20], menu2[20], menu3[20], menu4[20], menu5[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 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 menu(int a)
{
	if(a == 0)
	{
		strcpy(welcom1, "71301-3D~");
		strcpy(welcom2, "Final Year Project~");
		strcpy(header1, "Control~");
		strcpy(header2, "Edit~");
		strcpy(menu1, "Lighting~");
		strcpy(menu2, "Air Condition~");
		strcpy(menu3, "Menu 3~");
		strcpy(menu4, "Menu 4~");
		strcpy(menu5, "Menu 5~");
}
	else if(a == 101)
	{
		command(0);
		posit(10);
		str(header1);
		posit(21);
		str(menu1);
		posit(31);
		str(menu2);
		posit(41);
		str(menu3);
	}
	else if(a == 102)
	{
		command(0);
		posit(10);
		str(header1);
		posit(21);
		str(menu2);
		posit(31);
		str(menu3);
		posit(41);
		str(menu4);
	}
	else if(a == 103)
	{
		command(0);
		posit(10);
		str(header1);
		posit(21);
		str(menu3);
		posit(31);
		str(menu4);
		posit(41);
		str(menu5);
	}
}

void roll(int a, int b, int c)	//a:Up/Down, b:item, c:cursor position
{
	switch(a)
	{
		case 1:					//up case
			if(b == 5 && c == 4)
			{
				posit(40);		//point to bit 1 at line 4
				chr(' ');
				posit(30);		//point to bit 1 at line 3
				syb(0);			//display cursor
			}
			else if(b == 4 && c == 4)
			{
				posit(40);		//point to bit 1 at line 4
				chr(' ');
				posit(30);		//point to bit 1 at line 3
				syb(0);			//display cursor
			}
			else if(b == 4 && c == 3)
			{
				posit(30);		//point to bit 1 at line 3
				chr(' ');
				posit(20);		//point to bit 1 at line 2
				syb(0);			//display cursor
			}
			else if(b == 3 && c == 4)
			{
				posit(40);		//point to bit 1 at line 4
				chr(' ');
				posit(30);		//point to bit 1 at line 3
				syb(0);			//display cursor
			}
			else if(b == 3 && c == 3)
			{
				posit(30);		//point to bit 1 at line 3
				chr(' ');
				posit(20);		//point to bit 1 at line 2
				syb(0);			//display cursor
			}
			else if(b == 3 && c == 2)
			{
				menu(102);
				posit(20);		//point to bit 1 at line 2
				syb(0);			//display cursor
			}
			else if(b == 2 && c == 3)
			{
				posit(30);
				chr(' ');
				posit(20);
				syb(0);
			}
			else if(b == 2 && c == 2)
			{
				menu(101);
				posit(20);		//point to bit 1 at line 2
				syb(0);			//display cursor
			}
			break;
		case 2:					//down case
			if(b == 1 && c == 2)
			{
				posit(20);	//point to bit 1 at line 2
				chr(' ');
				posit(30);	//point to bit 1 at line 3
				syb(0);		//display cursor
			}
			else if(b == 2 && c == 2)
			{
				posit(20);		//point to bit 1 at line 2
				chr(' ');
				posit(30);		//point to bit 1 at line 3
				syb(0);			//display cursor
			}
			else if(b == 2 && c == 3)
			{
				posit(30);		//point to bit 1 at line 3
				chr(' ');
				posit(40);		//point to bit 1 at line 4
				syb(0);			//display cursor
			}
			else if(b == 3 && c == 2)
			{
				posit(20);		//point to bit 1 at line 2
				chr(' ');
				posit(30);		//point to bit 1 at line 3
				syb(0);			//display cursor
			}
			else if(b == 3 && c == 3)
			{
				posit(30);		//point to bit 1 at line 3
				chr(' ');
				posit(40);		//point to bit 1 at line 4
				syb(0);			//display cursor
			}
			else if(b == 3 && c == 4)
			{
				menu(102);
				posit(40);		//point to bit 1 at line 2
				syb(0);			//display cursor
			}
			else if(b == 4 && c == 3)
			{
				posit(30);		//point to bit 1 at line 3
				chr(' ');
				posit(40);		//point to bit 1 at line 4
				syb(0);			//display cursor
			}
			else if(b == 4 && c == 4)
			{
				menu(103);
				posit(40);		//point to bit 1 at line 2
				syb(0);			//display cursor
			}
			break;
	}
}

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
	command(31);		//autoforward
	menu(0);
	posit(1);
	str(welcom1);
	posit(2);
	str(welcom2);
	delay(33333);
	command(0);
	menu(101);
	posit(20);
	syb(0);
}

when(io_changes(up)to 0)
{
	roll(1, item, cur_pos);
	if(item >= 1 && cur_pos > 2)
	{
		if(cur_pos > 2)
		{
			cur_pos = cur_pos - 1;
		}
		item = item - 1;
	}
}

when(io_changes(down)to 0)
{
	roll(2, item, cur_pos);
	if(item > 6 && cur_pos >= 4)
	{
		if(cur_pos > 4)
		{
			cur_pos = cur_pos + 1;
		}
		item = item + 1;
	}
}

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

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