//=============================================================
//          WINSTAR Display Co.,Ltd
//    LCM     :WH1602C   
//    Author  :Brian lin
//    history : 
//==============================================================
#include	<reg51.h>
#include 	<stdio.h>          // define I/O functions
#include 	<INTRINS.H>        // KEIL FUNCTION
#define		Data_BUS P1
sbit		busy    =P1^7;
sbit		RS      =P3^0;
sbit	 	RW      =P3^7;
sbit		Enable1 =P3^4;
char bdata  flag;
sbit busy_f  = flag^0;
sbit mod_f   = flag^1;
char count_a,count_b;
char inf;

void init_8051();
void hard(char);
void CheckBusy();
void WriteIns(char);
void WriteData(char);
void WriteString(char,char *);
void Initial_ks0066();
void LCM_show(char);
void chip_select(char);
void clear_lcm();
//=================================
unsigned char code picture1[22][16] ={{"                "},
									  {"                "},
									  {"                "},
									  {"                "},
									  {"                "},
									  {"                "},
									  {"                "},
									  {"Our professional"},
									  {"sales staff and "},
									  {"highly skilled  "},
									  {"engineering tame"},
									  {"strive continual"},
									  {"ly to be the    "}, 
									  {"most customer   "},
									  {"friendly and    "},
									  {"resourceful     "}, 
									  {"supplier in     "}, 
									  {"today's market. "},
									  {"We look forword "},
									  {"to working with "},
									  {"you on your next"},
									  {"project. tks!!  "}};
									  
unsigned char code MSG1[] = "WINSTAR";
unsigned char code MSG2[94] ="                Tel:+886 4 24262208  FAX: +886 4 24262207   www.winstar.com.tw                ";

unsigned char code MSG3[11] ="TN Positive";
unsigned char code MSG4[16] ="B/L Yellow-Green";	
unsigned char code MSG5[12] ="STN Negative";
unsigned char code MSG6[9] ="B/L Green";
unsigned char code MSG7[13] ="FSTN Positive";
unsigned char code MSG8[9] ="B/L Amber";
unsigned char code MSG9[13] ="FSTN Positive";
unsigned char code MSGA[16] ="B/L Yellow-Green";

unsigned char code UserPattern[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x03,
					0x01,0x00,0x00,0x00,0x00,0x02,0x07,0x07,0x03,0x01,0x00,0x00,0x00,0x05,0x0f,0x0f,0x07,0x02,0x00,0x00,
					0x00,0x0a,0x1f,0x1f,0x0e,0x04,0x00,0x00};
void CheckBusy()
{
Data_BUS = 0xff;
  RS = 0;
  RW = 1;  
do
 {

  Enable1 = 1;
  busy_f = busy;
  Enable1 = 0;
 }while(busy_f);
}
//=================================
void WriteIns(char instruction)
{
 RS = 0;
 RW = 0;
 Data_BUS = instruction;
 Enable1 = 1;
 _nop_();
 Enable1 = 0;
 CheckBusy();
}
//=================================
//=================================
void WriteData(char data1)
{
 RS = 1;
 RW = 0;
 Data_BUS = data1;
 Enable1 = 1;
 _nop_();
 Enable1 = 0;
 CheckBusy(); 
}
//=================================
//=================================
void WriteString(count,MSG)
char count;
char *MSG;
{
	char i;
	for(i = 0; i<count;i++)
		WriteData(MSG[i]);
}
//=================================
//=================================
void Initial_ks0066()
{
WriteIns(0x38);
WriteIns(0x38);
WriteIns(0x38);
WriteIns(0x38);
WriteIns(0x08);
WriteIns(0x01);
WriteIns(0x0c);
WriteIns(0x06);
}
//========================================
//  CLEAR LCM
//=========================================
void clear_lcm()
{
char i;
 	WriteIns(0x80);
	for(i = 0; i<16;i++)
	    {WriteData(0x00);}
	WriteIns(0xc0);
   	for(i = 0; i<16;i++)
	    {WriteData(0x00);}
}





main()
{
char i,j,k,mode;		    					
Initial_ks0066();
init_8051();
WriteIns(0x40);			//write user pattern address "0"
WriteString(48,UserPattern);
k = 1;
mode = 0;
while(1)
{	
if(count_b == 0)
{
switch(mode)
{
		case 0:
				count_b = 20;
				WriteIns(0x80);
				LCM_show(k);
				WriteIns(0xc0);
				LCM_show(k+1);
				WriteIns(0x80);
				LCM_show(k+2);
				WriteIns(0xc0);
				LCM_show(k+3);
				WriteIns(0x80);
				LCM_show(k+4);
				WriteIns(0xc0);
				LCM_show(k+5);
				WriteIns(0x80);
				LCM_show(k+6);
				WriteIns(0xc0);
				LCM_show(k+7);
				k++;
			    break;
		case 1:
				clear_lcm();
				mode = 2;
        		break;
        case 2:
        		count_b = 10;
				WriteIns(0x84);
				WriteString(11,MSG1);
				hard(0x81);
    			WriteIns(0xc0);
   				for(i = 0; i<16;i++)
					{WriteData(MSG2[j+i]);}
       			j++;
				break;
		case 3:
				clear_lcm();
				mode = 4;
				break;
	  	case 4:
	  			count_b = 250; 
	  			WriteIns(0x80);
	  			WriteString(13,MSG9);
	  			WriteIns(0xc0);
	  			WriteString(16,MSGA);	
	  			mode = 0;
	  			break; 	
				
}

if(k == 15)
{
count_b = 100;
k = 1;
mode = 1;
}
if(j== 79)
{
j=0;
count_b = 50;
mode = 3;
}					

}}}


//==================================
void LCM_show(char m)
{
char n;
	for(n = 0; n<16;n++)
		WriteData(picture1[m][n]);
}
//==================================
void init_8051()
{
TH1 = (65536 - 50000)/256;
TL1 = (65536 - 50000)%256;
TCON = 0x00;
TMOD = 0x10;
IE = 0x88;
TR1  = 1;
}
//=================================
void hard(char address)
{
if(count_a==0)
 {
	count_a = 10;
	WriteIns(address);
	WriteData(inf);	
	WriteIns(address+12);
	WriteData(inf);	
	inf++;
	if(inf == 6)
		inf = 0;
}} 	

//=======================================
//計數器 1  50ms 中斷一次
//=======================================
void t1_int(void) interrupt 3
{
 TH1 = (65536 - 50000)/256;
 TL1 = (65536 - 50000)%256;
 if(count_a != 0)
 	count_a--;
 if(count_b != 0)
 	count_b--;
}

