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

network input SNVT_count nvi_size = 20;
network input SNVT_count nvi_no_lighting = 1;
network input SNVT_switch nvi_lighting[4];

network output SNVT_count nvo_no_lighting;
network output SNVT_switch nvo_lighting[4];

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

eeprom unsigned int no_lighting = 1;

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[20])
{
	int chr_num;
	for(chr_num = 0; a[chr_num] != '~'; chr_num = chr_num + 1)
	{
		chr(a[chr_num]);
	}
}

void move(int a, int b)
{
	if(a == 1)
	{
		writecmd(127 + b);
	}
	else if(a == 2)
	{
		writecmd(128 + 63 + b);
	}
	else if(a == 3)
	{
		writecmd(128 + 19 + b);
	}
	else if(a == 4)
	{
		writecmd(128 + 83 + b);
	}
}

void name(int a)
{
	switch(a)
	{
		case 1:
			strcpy(display, " Lighting~");
			str(display);
			break;
		case 11:
			strcpy(display, " Lamp 1~");
			str(display);
			break;
		case 12:
			strcpy(display, " Lamp 2~");
			str(display);
			break;
		case 13:
			strcpy(display, " Lamp 3~");
			str(display);
			break;
		case 14:
			strcpy(display, " Lamp 4~");
			str(display);
			break;
		case 2:
			strcpy(display, " Blind~");
			str(display);
			break;
		case 3:
			strcpy(display, " Air Condition~");
			str(display);
			break;
		case 4:
			strcpy(display, " Other~");
			str(display);
			break;
	}
}

void dec_num(unsigned int a)
{
	unsigned int b, c, d;
	b = a / 200;
	if(b == 0)
	{
		c = a / 20;
		d = (a % 20) / 2;
	}
	else
	{
		c = 0;
		d = 0;
	}
	chr(48 + b);
	chr(48 + c);
	chr(48 + d);
	chr('%');
}

void menu(int a)
{
	int b;
	switch(a)
	{
		case 1:
			command(0);
			for(b = 1; b <= 4; b = b + 1)
			{
				move(b, 1);
				name(b);
			}
			move(1, 1);
			chr(126);
			cur_pos = 1;
			break;
		case 21:
			command(0);
			for(b = 1; b <= no_lighting; b = b + 1)
			{
				move(b, 1);
				name(b + 10);
				if(nvi_lighting[b - 1].state == 1)
				{
					move(b, nvi_size - 8);
					strcpy(display, " On~");
					str(display);
					move(b, nvi_size - 3);
					dec_num(nvi_lighting[b - 1].value);
				}
				else if(nvi_lighting[b - 1].state == 0)
				{
					move(b, nvi_size - 8);
					strcpy(display, "Off~");
					str(display);
					move(b, nvi_size - 3);
					strcpy(display, "000%~");
					str(display);
				}
			}
			if(mnu_pos != 31)
			{
				move(1, 1);
				chr(126);
				cur_pos = 1;
			}
			break;
		case 31:
			command(0);
			move(2, 8);
			strcpy(display, "Level~");
			str(display);
			move(3, 9);
			dec_num((cur_pos - 1) * 40);
	}
}

void roll_cur(int a)
{
	switch(a)
	{
		case 1:
			if(cur_pos > 1)
			{
				if(cur_pos == 2)
				{
					move(2, 1);
					chr(' ');
					move(1, 1);
					chr(126);
				}
				else if(cur_pos == 3)
				{
					move(3, 1);
					chr(' ');
					move(2, 1);
					chr(126);
				}
				else if(cur_pos == 4)
				{
					move(4, 1);
					chr(' ');
					move(3, 1);
					chr(126);
				}
				item = item - 1;
				cur_pos = cur_pos - 1;
			}
			break;
		case 2:
			if(cur_pos < 4)
			{
				if(cur_pos == 1)
				{
					move(1, 1);
					chr(' ');
					move(2, 1);
					chr(126);
				}
				else if(cur_pos == 2)
				{
					move(2, 1);
					chr(' ');
					move(3, 1);
					chr(126);
				}
				else if(cur_pos == 3)
				{
					move(3, 1);
					chr(' ');
					move(4, 1);
					chr(126);
				}
				item = item + 1;
				cur_pos = cur_pos + 1;
			}
			break;
		case 22:
			if(cur_pos < 4 && cur_pos < no_lighting)
			{
				if(cur_pos == 1)
				{
					move(1, 1);
					chr(' ');
					move(2, 1);
					chr(126);
				}
				else if(cur_pos == 2)
				{
					move(2, 1);
					chr(' ');
					move(3, 1);
					chr(126);
				}
				else if(cur_pos == 3)
				{
					move(3, 1);
					chr(' ');
					move(4, 1);
					chr(126);
				}
				item = item + 1;
				cur_pos = cur_pos + 1;
			}
			break;
		case 31:
			if(cur_pos < 6)
			{
				cur_pos = cur_pos + 1;
				menu(31);
			}
			break;
		case 32:
			if(cur_pos > 1)
			{
				cur_pos = cur_pos - 1;
				menu(31);
			}
			break;
	}
}
	
when(nv_update_occurs(nvi_no_lighting))
{
	no_lighting = nvi_no_lighting;
	nvo_no_lighting = nvi_no_lighting;
	if(mnu_pos == 21)
	{
		menu(21);
	}
}

when(nv_update_occurs(nvi_lighting))
{
	int a;
	if(mnu_pos == 21)
	{
		for(a = 0; a <= 3; a = a + 1)
		{
			if(a + 1 <= no_lighting)
			{
				if(nvi_lighting[a].state == 1)
				{
					move(a + 1, nvi_size - 8);
					strcpy(display, " On~");
					str(display);
					move(a + 1, nvi_size - 3);
					dec_num(nvi_lighting[a].value);
				}
				else if(nvi_lighting[a].state == 0)
				{
					move(a + 1, nvi_size - 8);
					strcpy(display, "Off~");
					str(display);
					move(a + 1, nvi_size - 3);
					strcpy(display, "000%~");
					str(display);
				}
			}
		}
	}
}

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
	nvo_no_lighting = no_lighting;
	menu(1);
}

when(io_changes(up)to 0)
{
	if(mnu_pos == 1 || mnu_pos == 21)
	{
		roll_cur(1);
	}
	else if(mnu_pos == 31)
	{
		roll_cur(31);
	}
}

when(io_changes(down)to 0)
{
	if(mnu_pos == 1)
	{
		roll_cur(2);
	}
	else if(mnu_pos == 21)
	{
		roll_cur(22);
	}
	else if(mnu_pos == 31)
	{
		roll_cur(32);
	}
}

when(io_changes(enter)to 0)
{
	if(mnu_pos == 1 && item == 1)
	{
		menu(21);
		cur_pos = 1;
		mnu_pos = 21;
	}
	else if(mnu_pos == 21)
	{
		cur_pos = (nvi_lighting[item - 1].value / 40) + 1;
		menu(31);
		mnu_pos = 31;
	}
	else if(mnu_pos == 31)
	{
		nvo_lighting[item - 1].value = (cur_pos - 1) * 40;
		if(nvo_lighting[item - 1].value == 0)
		{
			nvo_lighting[item - 1].state = 0;
		}
		else
		{
			nvo_lighting[item - 1].state = 1;
		}
		menu(21);
		move(item, 1);
		chr(126);
		cur_pos = item;
		mnu_pos = 21;
	}
}

when(io_changes(esc)to 0)
{
	if(mnu_pos == 21)
	{
		menu(1);
		cur_pos = 1;
		mnu_pos = 1;
		item = 1;
	}
	else if(mnu_pos == 31)
	{
		menu(21);
		move(item, 1);
		chr(126);
		cur_pos = item;
		mnu_pos = 21;
	}
}

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

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