#pragma num_alias_table_entries	14
#pragma enable_multiple_baud

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 cursor = 1;

when(reset)
{
	//delay 15ms
	delay(600);
	//datainterface 1
	io_out(en, 1);
	io_out(io_com_data, 0b00000000UL);
	io_out(io_com_data, 0b00111000UL);
	io_out(en, 0);	
	delay(1);
	//delay 4.1ms
	delay(160);
	//datainterface 2
	io_out(en, 1);
	io_out(io_com_data, 0b00000000UL);
	io_out(io_com_data, 0b00111000UL);
	io_out(en, 0);
	delay(1);
	//delay 100us
	delay(2);
	//datainterface 3
	io_out(en, 1);
	io_out(io_com_data, 0b00000000UL);
	io_out(io_com_data, 0b00111000UL);
	io_out(en, 0);
	delay(1);
	//delay 4.1ms
	delay(160);
	//datainterface 4
	io_out(en, 1);
	io_out(io_com_data, 0b00000000UL);
	io_out(io_com_data, 0b00111000UL);
	io_out(en, 0);
	delay(1);
	//displayoff
	io_out(en, 1);
	io_out(io_com_data, 0b00000000UL);
	io_out(io_com_data, 0b00001000UL);
	io_out(en, 0);
	delay(1);
	//clear
	io_out(en, 1);
	io_out(io_com_data, 0b00000000UL);
	io_out(io_com_data, 0b00000001UL);
	io_out(en, 0);
	delay(1);
	delay(63);
	//displayon
	io_out(en, 1);
	io_out(io_com_data, 0b00000000UL);
	io_out(io_com_data, 0b00001110UL);
	io_out(en, 0);
	delay(1);
	//autoforward
	io_out(en, 1);
	io_out(io_com_data, 0b00000000UL);
	io_out(io_com_data, 0b00000110UL);
	io_out(en, 0);
	delay(1);
}

when(io_changes(up)to 0)
{
	//cursor up
	if(cursor == 1)
	{
		cursor = 1;
		io_out(en, 1);
		io_out(io_com_data, 0b00000000UL);
		io_out(io_com_data, 0b10000000UL);
		io_out(en, 0);
		delay(1);
	}
	else if(cursor == 2)
	{
		cursor = cursor - 1;
		io_out(en, 1);
		io_out(io_com_data, 0b00000000UL);
		io_out(io_com_data, 0b10000000UL);
		io_out(en, 0);
		delay(1);
	}
	else if(cursor == 3)
	{
		cursor = cursor - 1;
		io_out(en, 1);
		io_out(io_com_data, 0b00000000UL);
		io_out(io_com_data, 0b11000000UL);
		io_out(en, 0);
		delay(1);
	}
	else if(cursor == 4)
	{
		cursor = cursor - 1;
		io_out(en, 1);
		io_out(io_com_data, 0b00000000UL);
		io_out(io_com_data, 0b10010100UL);
		io_out(en, 0);
		delay(1);
	}
}

when(io_changes(down)to 0)
{
	//cursor down
	if(cursor == 1)
	{
		cursor = cursor + 1;
		io_out(en, 1);
		io_out(io_com_data, 0b00000000UL);
		io_out(io_com_data, 0b11000000UL);
		io_out(en, 0);
		delay(1);
	}
	else if(cursor == 2)
	{
		cursor = cursor + 1;
		io_out(en, 1);
		io_out(io_com_data, 0b00000000UL);
		io_out(io_com_data, 0b10010100UL);
		io_out(en, 0);
		delay(1);
	}
	else if(cursor == 3)
	{
		cursor = cursor + 1;
		io_out(en, 1);
		io_out(io_com_data, 0b00000000UL);
		io_out(io_com_data, 0b11010100UL);
		io_out(en, 0);
		delay(1);
	}
	else if(cursor == 4)
	{
		io_out(en, 1);
		io_out(io_com_data, 0b00000000UL);
		io_out(io_com_data, 0b11010100UL);
		io_out(en, 0);
		delay(1);
	}
}

when(io_changes(enter)to 0)
{
	//add character "A"
	io_out(en, 1);
	io_out(io_com_data, 0b10000000UL);
	io_out(io_com_data, 0b01000001UL);
	io_out(en, 0);
	delay(1);
}

when(io_changes(esc)to 0)
{
	//cursor move to left
	io_out(en, 1);
	io_out(io_com_data, 0b00000000UL);
	io_out(io_com_data, 0b00010000UL);
	io_out(en, 0);
	delay(1);
	//delete character
	io_out(en, 1);
	io_out(io_com_data, 0b10000000UL);
	io_out(io_com_data, 0b00100000UL);
	io_out(en, 0);
	delay(1);
	//cursor move to left
	io_out(en, 1);
	io_out(io_com_data, 0b00000000UL);
	io_out(io_com_data, 0b00010000UL);
	io_out(en, 0);
	delay(1);
}

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

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